MCPcopy Create free account
hub / github.com/cargo-lambda/cargo-lambda / instance_monitor

Function instance_monitor

crates/cargo-lambda-watch/src/scheduler.rs:125–178  ·  view source on GitHub ↗

Background monitor that checks queue depths and spawns instances as needed

(
    subsys: SubsystemHandle,
    state: RuntimeState,
    cargo_options: CargoOptions,
    watcher_config: WatcherConfig,
    gc_tx: Sender<(String, InstanceId)>,
)

Source from the content-addressed store, hash-verified

123
124/// Background monitor that checks queue depths and spawns instances as needed
125async fn instance_monitor(
126 subsys: SubsystemHandle,
127 state: RuntimeState,
128 cargo_options: CargoOptions,
129 watcher_config: WatcherConfig,
130 gc_tx: Sender<(String, InstanceId)>,
131) -> Result<(), ServerError> {
132 let mut interval = tokio::time::interval(Duration::from_millis(100));
133
134 loop {
135 tokio::select! {
136 _ = interval.tick() => {
137 let function_names = state.req_cache.keys().await;
138
139 for function_name in function_names {
140 let queue_depth = state.req_cache.queue_depth(&function_name).await;
141
142 if queue_depth == 0 {
143 continue;
144 }
145
146 let mut pools = state.instance_pools.write().await;
147 let pool = pools
148 .entry(function_name.clone())
149 .or_insert_with(|| InstancePool::new(state.max_concurrency));
150 let pool_clone = pool.clone();
151 drop(pools);
152
153 if pool_clone.should_spawn_instance(queue_depth).await {
154 debug!(
155 function = ?function_name,
156 queue_depth,
157 "spawning additional instance"
158 );
159
160 spawn_function_instance(
161 &subsys,
162 &state,
163 &function_name,
164 &cargo_options,
165 &watcher_config,
166 &gc_tx,
167 )
168 .await?;
169 }
170 }
171 }
172 _ = subsys.on_shutdown_requested() => {
173 info!("terminating instance monitor");
174 return Ok(());
175 }
176 }
177 }
178}
179
180/// Spawn a new function instance
181async fn spawn_function_instance(

Callers 1

start_schedulerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected