Skip to content

Commit

Permalink
Add msg queuing for all messages for public agents (yewstack#1007)
Browse files Browse the repository at this point in the history
* Add msg queuing for all messages for public agents

Fixes yewstack#944

* Send Disconnected and Destroy correctly

They now use the `send_message` so that they also
get queued in case the worker is not ready yet

* Remove todo about connected message
  • Loading branch information
TheNeikos authored and nicklaswj committed Mar 13, 2020
1 parent a548216 commit 73c43e6
Showing 1 changed file with 15 additions and 9 deletions.
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

0 comments on commit 73c43e6

Please sign in to comment.