Skip to content

Make endpoint handle function independent of other handlers #29

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

Merged
merged 1 commit into from
Oct 28, 2024
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
79 changes: 50 additions & 29 deletions src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,49 +333,70 @@ pub struct BidiStreamRunner {

impl BidiStreamRunner {
pub async fn handle(
mut self,
mut input_rx: InputReceiver,
self,
input_rx: InputReceiver,
output_tx: OutputSender,
) -> Result<(), Error> {
Self::init_loop_vm(&mut self.vm, &mut input_rx).await?;

// Retrieve the service from the Arc
let svc = self
.endpoint
.svcs
.get(&self.svc_name)
.expect("service must exist at this point");

// Initialize handler context
let (handler_state_tx, handler_state_rx) = HandlerStateNotifier::new();
let ctx = ContextInternal::new(
handle(
input_rx,
output_tx,
self.vm,
self.svc_name,
self.handler_name,
input_rx,
output_tx,
handler_state_tx,
);

// Start user code
let user_code_fut = InterceptErrorFuture::new(ctx.clone(), svc.handle(ctx.clone()));

// Wrap it in handler state aware future
HandlerStateAwareFuture::new(ctx.clone(), handler_state_rx, user_code_fut).await
svc,
)
.await
}
}

async fn init_loop_vm(vm: &mut CoreVM, input_rx: &mut InputReceiver) -> Result<(), ErrorInner> {
while !vm.is_ready_to_execute().map_err(ErrorInner::VM)? {
match input_rx.recv().await {
Some(Ok(b)) => vm.notify_input(b),
Some(Err(e)) => vm.notify_error(
"Error when reading the body".into(),
e.to_string().into(),
None,
),
None => vm.notify_input_closed(),
}
#[doc(hidden)]
pub async fn handle<S: Service<Future = BoxFuture<'static, Result<(), Error>>> + Send + Sync>(
mut input_rx: InputReceiver,
output_tx: OutputSender,
vm: CoreVM,
svc_name: String,
handler_name: String,
svc: &S,
) -> Result<(), Error> {
let mut vm = vm;
init_loop_vm(&mut vm, &mut input_rx).await?;

// Initialize handler context
let (handler_state_tx, handler_state_rx) = HandlerStateNotifier::new();
let ctx = ContextInternal::new(
vm,
svc_name,
handler_name,
input_rx,
output_tx,
handler_state_tx,
);

// Start user code
let user_code_fut = InterceptErrorFuture::new(ctx.clone(), svc.handle(ctx.clone()));

// Wrap it in handler state aware future
HandlerStateAwareFuture::new(ctx.clone(), handler_state_rx, user_code_fut).await
}

async fn init_loop_vm(vm: &mut CoreVM, input_rx: &mut InputReceiver) -> Result<(), ErrorInner> {
while !vm.is_ready_to_execute().map_err(ErrorInner::VM)? {
match input_rx.recv().await {
Some(Ok(b)) => vm.notify_input(b),
Some(Err(e)) => vm.notify_error(
"Error when reading the body".into(),
e.to_string().into(),
None,
),
None => vm.notify_input_closed(),
}
Ok(())
}
Ok(())
}
Loading