gRPC Version: "1.2.0"
Java Version: "1.8.0_65"
I'm using gRPC with https://github.com/scalapb/ScalaPB SBT plugin.
It seems that in a Scala Future,
MY_CONTEXT_KEY.get
awlays returns null, this is probably because the context is still with the
original thread. Is there a way that I can somehow clone it to other
threads used by scala futures?
Here is my test code:
def ping(req: PingReq) = {
val keyVal = MY_CONTEXT_KEY.get
for {
accountInfo <- process(keyVal)
res1 <- bar1.ping(req)
res2 <- bar2.ping(req)
} yield {
println("=====FOO: " + accountInfo)
PingRes("AAA")
}
}
The above code works file, but the following doesn't
def ping(req: PingReq) = {
for {
keyVal = Future {MY_CONTEXT_KEY.get}
accountInfo <- process(keyVal)
res1 <- bar1.ping(req)
res2 <- bar2.ping(req)
} yield {
println("=====FOO: " + accountInfo)
PingRes("AAA")
}
}
In the above example, keyVal is always Future(null)。
Besides, An client interceptor I came up with, called
JwtClientInterceptor deosn't work either. This class is defined as:
class JwtClientInterceptor extends ClientInterceptor {
override def interceptCall[ReqT, RespT](
methodDescriptor: MethodDescriptor[ReqT, RespT],
callOptions: CallOptions,
channel: Channel
) = {
new ForwardingClientCall.SimpleForwardingClientCall[ReqT, RespT](
channel.newCall(methodDescriptor, callOptions)
) {
override def start(responseListener: ClientCall.Listener[RespT],
headers: Metadata) = {
val jwt = CTX_AUTH_JWT.get
if (jwt != null && jwt.nonEmpty) {
headers.put(AUTH_HEADER, CTX_AUTH_JWT.get)
}
super.start(responseListener, headers)
}
}
}
}
In the above exampole, 'val jwt' is always null BECAUSE a downstream RPC
call is invoked from inside of a scala Future - actually a
for-comprehension statement like follows:
val bar1 = ...
val bar2 = {
val channel2 = ManagedChannelBuilder
.forAddress("localhost", 12333)
.usePlaintext(true)
.intercept(new JwtClientInterceptor())
.intercept(new TraceIdClientInterceptor())
.build()
def someRpcMethod(req: MethodReq) = {
implicit val sub = getJwtSubject
for {
res1 <- bar1.ping(req)
res2 <- bar2.ping(req)
} yield {
MethodResp(res1 + res2)
}
}
}
*How can I access the same context from scala Futures??? I hope someone can
help me out, this is now THE road blocker for my project.*
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/fc8559cf-6484-4617-9236-26970dc0193d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.