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 context to RPC impl on the server side #28

Open
minghuaw opened this issue Oct 25, 2021 · 2 comments
Open

Add context to RPC impl on the server side #28

minghuaw opened this issue Oct 25, 2021 · 2 comments

Comments

@minghuaw
Copy link
Owner

minghuaw commented Oct 25, 2021

The context could be used for

@minghuaw minghuaw changed the title Add some context to RPC impl on the server side Add context to RPC impl on the server side Oct 25, 2021
@minghuaw
Copy link
Owner Author

minghuaw commented Nov 9, 2021

Proposed Usage

A new type Context will be added, which (for now) will contain information such as client connection info and distributed tracing (if enabled). The proposed usage looks like below

#[export_impl]
impl Service for State {
  #[export_method]
  pub async fn(&self, arg: Arg) -> _ {
    let ctx: Arc<Context> = Context::current();
    // ....
  }
}

The implementation will likely use task_local!. While tokio provides a really nice method scope that allows allocating the initially value upon entering the scope of the task local value, async-std doesn't provide such thing and thus may require a rather complicated way. This will not be a big problem if the each Context is essentially scoped within a client connection because we would just need to change the value once. However, if we were to add request specific info to the Context, then the task_local value will need to be initialized for each request execution.

@minghuaw
Copy link
Owner Author

Another Proposition

Another way would be to allow user define the service with the Context as the second parameter, which will not appear on the generated client stub (the macro takes care of this)

#[export_impl]
impl Service for State {
  #[export_method]
  pub async fn(&self, ctx: Arc<Context>, arg: Arg) -> _ {
    // ....
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant