Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add msg queuing for all messages for public agents #1007

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,6 @@ impl Discoverer for Public {
let msg = FromWorker::<AGN::Output>::unpack(&data);
match msg {
FromWorker::WorkerLoaded => {
// TODO(#944): Send `Connected` message
REMOTE_AGENTS_LOADED.with(|loaded| {
let _ = loaded.borrow_mut().insert(TypeId::of::<AGN>());
});
Expand Down Expand Up @@ -645,7 +644,9 @@ impl Discoverer for Public {
}),
};
let launched = RemoteAgent::new(worker, slab);
entry.insert(launched).create_bridge(callback)
let bridge = entry.insert(launched).create_bridge(callback);
bridge.send_message(ToWorker::Connected(bridge.id));
bridge
}
}
});
Expand Down Expand Up @@ -689,6 +690,15 @@ impl<AGN: Agent> PublicBridge<AGN> {
}
});
}

/// Send a message to the worker, queuing it up if necessary
fn send_message(&self, msg: ToWorker<AGN::Input>) {
if self.worker_is_loaded() {
send_to_remote::<AGN>(&self.worker, msg);
} else {
self.msg_to_queue(msg.pack());
}
}
}

fn send_to_remote<AGN: Agent>(
Expand All @@ -713,11 +723,7 @@ fn send_to_remote<AGN: Agent>(
impl<AGN: Agent> Bridge<AGN> for PublicBridge<AGN> {
fn send(&mut self, msg: AGN::Input) {
let msg = ToWorker::ProcessInput(self.id, msg);
if self.worker_is_loaded() {
send_to_remote::<AGN>(&self.worker, msg);
} else {
self.msg_to_queue(msg.pack());
}
self.send_message(msg);
}
}

Expand All @@ -741,11 +747,11 @@ impl<AGN: Agent> Drop for PublicBridge<AGN> {
});

let disconnected = ToWorker::Disconnected(self.id);
send_to_remote::<AGN>(&self.worker, disconnected);
self.send_message(disconnected);

if terminate_worker {
let destroy = ToWorker::Destroy;
send_to_remote::<AGN>(&self.worker, destroy);
self.send_message(destroy);

REMOTE_AGENTS_LOADED.with(|loaded| {
loaded.borrow_mut().remove(&TypeId::of::<AGN>());
Expand Down