Open ostronom opened 7 years ago
I saw the question at http://stackoverflow.com/q/43805316/4690866 , but I don't think we have enough Scala experience to answer. Context uses ThreadLocal to propagate state. IIRC Scala has some feature that can dispatch work to other threads, which could break the ThreadLocal. You either need to teach Scala how to copy the ThreadLocal or maybe use Scala's equivalent of InheritableThreadLocal that auto-propagates to other threads.
Note that InheritableThreadLocal itself isn't a great solution for Java, but the equivalent in Scala may work better. If you reference some docs I could maybe help determine if a solution would work well.
Thank you for your reply. I will investigate ThreadLocal solution. Right now, i've made this work using two interceptors: first is the one in the starting post, which propagates value to next listener via headers, second one is synchronous and it moves the value from headers to context.
I want to try to implement explicit context, that will travel with request as one of the listener/handler parameters. Maybe it isn't too hard and will solve this kind of problems.
@ejona86 So how can I intercept gRPC calls and asynchronously validate their header tokens ?
You can capture the Context in your Future. As Context is immutable it is safe to pass around.
In the code below prevCtx
is captured from the interceptor thread and made available in the Future thread.
abstract class FutureListener[Q](implicit ec: ExecutionContext) extends Listener[Q] {
protected val delegate: Future[Listener[Q]]
private val eventually = delegate.foreach _
override def onComplete(): Unit = eventually { _.onComplete() }
override def onCancel(): Unit = eventually { _.onCancel() }
override def onMessage(message: Q): Unit = eventually { _ onMessage message }
override def onHalfClose(): Unit = eventually { _.onHalfClose() }
override def onReady(): Unit = eventually { _.onReady() }
}
object Keys {
val AUTH_META_KEY: Metadata.Key[String] = of("auth-key", Metadata.ASCII_STRING_MARSHALLER)
val ACCOUNT_CTX_KEY: Context.Key[String] = key("account")
}
class AuthorizationInterceptor(implicit ec: ExecutionContext) extends ServerInterceptor {
override def interceptCall[Q, R](
call: ServerCall[Q, R],
headers: Metadata,
next: ServerCallHandler[Q, R]
): Listener[Q] = {
val prevCtx = Context.current
val id = headers get AUTH_META_KEY take 6
new FutureListener[Q] {
protected val delegate = Future {
val nextCtx = prevCtx withValue (ACCOUNT_CTX_KEY, s"user-$id")
Contexts.interceptCall(nextCtx, call, headers, next)
}
}
}
}
This solution actually does nothing but harm -- it causes most of the requests to be cancelled with "half-closed" exception.
I want to pass some values from interceptor to rpc handler. I've read that this can be done with contexts. But the problem is, that my interceptor is asynchronous, i.e. it "waits" for the future to resolve before calling next listener. The context is lost in this situation. My code is in Scala:
The problem is that BOTID_CONTEXT_KEY.get is
null
in RPC handler, even when the future was resolved and the not-null value was set.