Closed liubin closed 3 years ago
For passing context to server side, we need accept context as an additional parameter.
This will need some modify in these parts( take sync as an example):
Add new parameter to generated code:
diff --git a/compiler/src/codegen.rs b/compiler/src/codegen.rs index da3d24e..64945f1 100644 --- a/compiler/src/codegen.rs +++ b/compiler/src/codegen.rs @@ -212,7 +212,7 @@ impl<'a> MethodGen<'a> { // Method signatures fn unary(&self, method_name: &str) -> String { format!( - "{}(&self, req: &{}, timeout_nano: i64) -> {}<{}>", + "{}(&self, req: &{}, timeout_nano: i64, metadata: ::ttrpc::TtrpcContext) -> {}<{}>", method_name, self.input(), fq_grpc("Result"), @@ -314,7 +314,7 @@ impl<'a> MethodGen<'a> { self.output() )); w.write_line(&format!( - "::ttrpc::client_request!(self, req, timeout_nano, \"{}.{}\", \"{}\", cres);", + "::ttrpc::client_request!(self, req, timeout_nano, metadata, \"{}.{}\", \"{}\", cres);", self.package_name, self.service_name, &self.proto.get_name(),
client_handler
Accept the parameter and set to Request:
Request
--- a/src/sync/utils.rs +++ b/src/sync/utils.rs @@ -70,11 +70,12 @@ macro_rules! request_handler { /// Send request through sync client. #[macro_export] macro_rules! client_request { - ($self: ident, $req: ident, $timeout_nano: ident, $server: expr, $method: expr, $cres: ident) => { + ($self: ident, $req: ident, $timeout_nano: ident, $metadata: ident, $server: expr, $method: expr, $cres: ident) => { let mut creq = ::ttrpc::Request::new(); creq.set_service($server.to_string()); creq.set_method($method.to_string()); creq.set_timeout_nano($timeout_nano); + creq.set_metadata($metadata);
For passing context to server side, we need accept context as an additional parameter.
This will need some modify in these parts( take sync as an example):
Add new parameter to generated code:
client_handler
macroAccept the parameter and set to
Request
: