0

我想覆盖(with ) 中的sendMessage,sendHeaders​​ , onMessage,onHalfClose方法:ServerInterceptorContext

val context = Context.current().withValue(TestConstants.CONTEXT_KEY, "testValue1")
val delegatedCall = object : SimpleForwardingServerCall<ReqT, RespT>(call) {

    override fun sendMessage(message: RespT) {
        this@SimpleServerInterceptor.sendMessage(message)
        super.sendMessage(message)
    }

    override fun sendHeaders(headers: Metadata) {
        this@SimpleServerInterceptor.sendHeaders(headers)
        super.sendHeaders(headers)
    }

    override fun close(status: Status, trailers: Metadata) {
        this@SimpleServerInterceptor.close(status, trailers)
        super.close(status, trailers)
    }
}
val delegatedListener: ServerCall.Listener<ReqT> =
    if (context === null)
        next.startCall(delegatedCall, headers)
    else
        Contexts.interceptCall(context, delegatedCall, headers, next)

return object : ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(delegatedListener) {

    override fun onMessage(message: ReqT) {
        this@SimpleServerInterceptor.onMessage(message, headers)
        super.onMessage(message)
    }

    override fun onHalfClose() {
        this@SimpleServerInterceptor.onHalfClose(headers)
        super.onHalfClose()
    }

    override fun onCancel() {
        this@SimpleServerInterceptor.onCancel(headers)
        super.onCancel()
    }

    override fun onComplete() {
        this@SimpleServerInterceptor.onComplete(headers)
        super.onComplete()
    }

    override fun onReady() {
        this@SimpleServerInterceptor.onReady(headers)
        super.onReady()
    }
}

这是输出:

>>>>intercept1
>>>>intercept2: testValue1
>>>>onReady1: null
>>>>onMessage1: null
>>>>onHalfClose1: null
HelloService3.hello: testValue1
>>>>sendHeaders2: testValue1
>>>>sendHeaders1: testValue1
>>>>sendMessage2: testValue1
>>>>sendMessage1: testValue1

您可以看到它对( , )Context有效,但对( , ) 无效。delegatedCallsendMessagesendHeadersdelegatedListeneronMessageonHalfClose

为什么以及如何解决这个问题?

4

1 回答 1

0

我认为问题在于该特定 SimpleForwardingServerCallListener内容没有看到更改,因为它是Contexts.interceptCall(). Listener 用于回调,因此它们由 gRPC 调用。gRPC 将调用返回的侦听器,该侦听器最终将调用delegatedListener并且delegatedListener是执行上下文调整的侦听器。Contexts.interceptCall()根本没有改变call,所以调用看到上下文的事实意味着监听器必须正常工作。

我建议制作一个ServerCallHandler执行所有调用SimpleServerInterceptor并将该处理程序传递给Contexts.interceptCall().

(只是一个草图,因为我对 Kotlin 不熟悉)

val context = Context.current().withValue(TestConstants.CONTEXT_KEY, "testValue1")
if (context === null) // Unclear how this will be null
    return next.startCall(delegatedCall, headers)
else
    return Contexts.interceptCall(context, delegatedCall, headers, new SimpleServerInterceptorHandler(next))

...

// Within SimpleServerInterceptorHandler
val delegatedCall = object : SimpleForwardingServerCall<ReqT, RespT>(call) {
  ...
}
val delegatedListener: ServerCall.Listener<ReqT> =
        next.startCall(delegatedCall, headers)
return object : ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(delegatedListener) {
  ...
}
于 2021-05-03T16:29:20.870 回答