-
Notifications
You must be signed in to change notification settings - Fork 136
fix(pb): simplify runner wf #3483
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
Closed
MasterPtato
wants to merge
1
commit into
11-18-fix_gas_fix_loop_forgotten_bug_due_to_concurrency
from
11-18-fix_pb_simplify_runner_wf
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
engine/packages/pegboard-runner/src/actor_event_demuxer.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| use std::collections::HashMap; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| use anyhow::Result; | ||
| use gas::prelude::*; | ||
| use rivet_runner_protocol as protocol; | ||
| use tokio::sync::mpsc; | ||
| use tokio::task::JoinHandle; | ||
|
|
||
| const GC_INTERVAL: Duration = Duration::from_secs(30); | ||
| const MAX_LAST_SEEN: Duration = Duration::from_secs(30); | ||
|
|
||
| struct Channel { | ||
| tx: mpsc::UnboundedSender<protocol::Event>, | ||
| handle: JoinHandle<()>, | ||
| last_seen: Instant, | ||
| } | ||
|
|
||
| pub struct ActorEventDemuxer { | ||
| ctx: StandaloneCtx, | ||
| channels: HashMap<Id, Channel>, | ||
| last_gc: Instant, | ||
| } | ||
|
|
||
| impl ActorEventDemuxer { | ||
| pub fn new(ctx: StandaloneCtx) -> Self { | ||
| Self { | ||
| ctx, | ||
| channels: HashMap::new(), | ||
| last_gc: Instant::now(), | ||
| } | ||
| } | ||
|
|
||
| /// Process an event by routing it to the appropriate actor's queue | ||
| #[tracing::instrument(skip_all)] | ||
| pub fn ingest(&mut self, actor_id: Id, event: protocol::Event) { | ||
| if let Some(channel) = self.channels.get(&actor_id) { | ||
| let _ = channel.tx.send(event); | ||
| } else { | ||
| let (tx, mut rx) = mpsc::unbounded_channel(); | ||
|
|
||
| let ctx = self.ctx.clone(); | ||
| let handle = tokio::spawn(async move { | ||
NathanFlurry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| loop { | ||
| let mut buffer = Vec::new(); | ||
|
|
||
| // Batch process events | ||
| if rx.recv_many(&mut buffer, 1024).await == 0 { | ||
| break; | ||
| } | ||
|
|
||
| if let Err(err) = dispatch_events(&ctx, actor_id, buffer).await { | ||
| tracing::error!(?err, "actor event processor failed"); | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| self.channels.insert( | ||
| actor_id, | ||
| Channel { | ||
| tx, | ||
| handle, | ||
| last_seen: Instant::now(), | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // Run gc periodically | ||
| if self.last_gc.elapsed() > GC_INTERVAL { | ||
| self.last_gc = Instant::now(); | ||
|
|
||
| self.channels.retain(|_, channel| { | ||
| let keep = channel.last_seen.elapsed() < MAX_LAST_SEEN; | ||
|
|
||
| if !keep { | ||
| // TODO: Verify aborting is safe here | ||
| channel.handle.abort(); | ||
| } | ||
|
|
||
| keep | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /// Shutdown all tasks and wait for them to complete | ||
| #[tracing::instrument(skip_all)] | ||
| pub async fn shutdown(self) { | ||
| tracing::debug!(channels=?self.channels.len(), "shutting down actor demuxer"); | ||
|
|
||
| // Drop all senders | ||
| let handles = self | ||
| .channels | ||
| .into_iter() | ||
| .map(|(_, channel)| channel.handle) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Await remaining tasks | ||
| for handle in handles { | ||
| let _ = handle.await; | ||
| } | ||
|
|
||
| tracing::debug!("actor demuxer shut down"); | ||
| } | ||
| } | ||
|
|
||
| async fn dispatch_events( | ||
| ctx: &StandaloneCtx, | ||
| actor_id: Id, | ||
| events: Vec<protocol::Event>, | ||
| ) -> Result<()> { | ||
| let res = ctx | ||
| .signal(pegboard::workflows::actor::Events { inner: events }) | ||
| .tag("actor_id", actor_id) | ||
| .graceful_not_found() | ||
| .send() | ||
| .await | ||
| .with_context(|| format!("failed to forward signal to actor workflow: {}", actor_id))?; | ||
| if res.is_none() { | ||
| tracing::warn!( | ||
| ?actor_id, | ||
| "failed to send signal to actor workflow, likely already stopped" | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.