Skip to content

Commit 126b3a1

Browse files
more commentary
1 parent 194fa77 commit 126b3a1

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

crates/core/src/host/module_host.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ impl CreateInstanceTimeMetric {
781781
}
782782

783783
impl<M: GenericModule> ModuleInstanceManager<M> {
784-
fn new(module: M, init_inst: M::Instance, database_identity: Identity) -> Self {
784+
fn new(module: M, init_inst: Option<M::Instance>, database_identity: Identity) -> Self {
785785
let host_type = module.host_type();
786786
let create_instance_time_metric = CreateInstanceTimeMetric {
787787
metric: WORKER_METRICS
@@ -791,9 +791,8 @@ impl<M: GenericModule> ModuleInstanceManager<M> {
791791
database_identity,
792792
};
793793

794-
// Add the first instance.
795794
let mut instances = VecDeque::new();
796-
instances.push_front(init_inst);
795+
instances.extend(init_inst);
797796

798797
Self {
799798
instances: Mutex::new(instances),
@@ -802,23 +801,6 @@ impl<M: GenericModule> ModuleInstanceManager<M> {
802801
}
803802
}
804803

805-
fn new_empty(module: M, database_identity: Identity) -> Self {
806-
let host_type = module.host_type();
807-
let create_instance_time_metric = CreateInstanceTimeMetric {
808-
metric: WORKER_METRICS
809-
.module_create_instance_time_seconds
810-
.with_label_values(&database_identity, &host_type),
811-
host_type,
812-
database_identity,
813-
};
814-
815-
Self {
816-
instances: Mutex::new(VecDeque::new()),
817-
module,
818-
create_instance_time_metric,
819-
}
820-
}
821-
822804
async fn with_instance<R>(&self, f: impl AsyncFnOnce(M::Instance) -> (R, M::Instance)) -> R {
823805
let inst = self.get_instance().await;
824806
let (res, inst) = f(inst).await;
@@ -1041,7 +1023,7 @@ impl ModuleHost {
10411023
init_inst,
10421024
} => {
10431025
info = module.info();
1044-
let instance_manager = ModuleInstanceManager::new(module, init_inst, database_identity);
1026+
let instance_manager = ModuleInstanceManager::new(module, Some(init_inst), database_identity);
10451027
Arc::new(ModuleHostInner::Wasm(WasmtimeModuleHost {
10461028
executor,
10471029
instance_manager,
@@ -1050,7 +1032,7 @@ impl ModuleHost {
10501032
ModuleWithInstance::Js { module, init_inst } => {
10511033
info = module.info();
10521034
let instance_lane = super::v8::JsInstanceLane::new(module.clone(), init_inst);
1053-
let procedure_instances = ModuleInstanceManager::new_empty(module.clone(), database_identity);
1035+
let procedure_instances = ModuleInstanceManager::new(module.clone(), None, database_identity);
10541036
Arc::new(ModuleHostInner::Js(V8ModuleHost {
10551037
module,
10561038
instance_lane,

crates/core/src/host/v8/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ impl JsInstanceEnv {
349349
/// and friends.
350350
#[derive(Clone)]
351351
pub struct JsInstance {
352+
/// Stable identifier for the underlying worker generation.
353+
///
354+
/// All clones of the same handle share the same `id`. The instance lane uses
355+
/// it to tell whether the currently active worker has already been replaced
356+
/// after a trap or disconnect.
352357
id: u64,
353358
request_tx: flume::Sender<JsWorkerRequest>,
354359
trapped: Arc<AtomicBool>,
@@ -519,44 +524,57 @@ type JsReplyTx<T> = oneshot::Sender<JsWorkerReply<T>>;
519524
/// executes the request there, and then has to send both the typed result and
520525
/// the worker's trapped-bit back to the async caller.
521526
enum JsWorkerRequest {
527+
/// See [`JsInstance::run_on_thread`].
528+
///
529+
/// This variant does not expect a [`JsWorkerReply`].
522530
RunFunction(Box<dyn FnOnce() -> LocalBoxFuture<'static, ()> + Send>),
531+
/// See [`JsInstance::update_database`].
523532
UpdateDatabase {
524533
reply_tx: JsReplyTx<anyhow::Result<UpdateDatabaseResult>>,
525534
program: Program,
526535
old_module_info: Arc<ModuleInfo>,
527536
policy: MigrationPolicy,
528537
},
538+
/// See [`JsInstance::call_reducer`].
529539
CallReducer {
530540
reply_tx: JsReplyTx<ReducerCallResult>,
531541
params: CallReducerParams,
532542
},
543+
/// See [`JsInstance::call_view`].
533544
CallView {
534545
reply_tx: JsReplyTx<ViewCommandResult>,
535546
cmd: ViewCommand,
536547
},
548+
/// See [`JsInstance::call_procedure`].
537549
CallProcedure {
538550
reply_tx: JsReplyTx<CallProcedureReturn>,
539551
params: CallProcedureParams,
540552
},
553+
/// See [`JsInstance::clear_all_clients`].
541554
ClearAllClients(JsReplyTx<anyhow::Result<()>>),
555+
/// See [`JsInstance::call_identity_connected`].
542556
CallIdentityConnected {
543557
reply_tx: JsReplyTx<Result<(), ClientConnectedError>>,
544558
caller_auth: ConnectionAuthCtx,
545559
caller_connection_id: ConnectionId,
546560
},
561+
/// See [`JsInstance::call_identity_disconnected`].
547562
CallIdentityDisconnected {
548563
reply_tx: JsReplyTx<Result<(), ReducerCallError>>,
549564
caller_identity: Identity,
550565
caller_connection_id: ConnectionId,
551566
},
567+
/// See [`JsInstance::disconnect_client`].
552568
DisconnectClient {
553569
reply_tx: JsReplyTx<Result<(), ReducerCallError>>,
554570
client_id: ClientActorId,
555571
},
572+
/// See [`JsInstance::init_database`].
556573
InitDatabase {
557574
reply_tx: JsReplyTx<anyhow::Result<Option<ReducerCallResult>>>,
558575
program: Program,
559576
},
577+
/// See [`JsInstance::call_scheduled_function`].
560578
CallScheduledFunction {
561579
reply_tx: JsReplyTx<CallScheduledFunctionResult>,
562580
params: ScheduledFunctionParams,
@@ -592,6 +610,11 @@ struct JsInstanceLaneState {
592610
}
593611

594612
#[derive(Clone)]
613+
/// A single serialized execution lane for JS module work.
614+
///
615+
/// Callers share one active [`JsInstance`] so hot requests stay on the same
616+
/// worker thread for locality. The lane only steps in on the rare path, where
617+
/// a trap or disconnect forces that active worker to be replaced.
595618
pub struct JsInstanceLane {
596619
module: JsModule,
597620
state: Arc<JsInstanceLaneState>,

0 commit comments

Comments
 (0)