登录
首页 >  Golang >  Go问答

如何通过流拦截器访问请求数据?

来源:stackoverflow

时间:2024-03-08 10:54:21 460浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《如何通过流拦截器访问请求数据?》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我有一个一元拦截器,其中包含以下代码:

func (m middlewares) locationinterceptor(c context.context, req interface{}, info *grpc.unaryserverinfo, handler grpc.unaryhandler) (interface{}, error) {
    ctx := c.(harmonycontext)
    location, ok := req.(interface{ getlocation() *corev1.location })
    if !ok {
        panic("location middleware used on message without a location")
    }
    
    ctx.location := location.getlocation()

    return handler(c, req)
}

如果我知道流肯定只会从服务器流到客户端,我如何才能将其转换为流拦截器?另外,有没有办法让它只在流开始的那一刻拦截?

func (m Middlewares) LocationInterceptorStream(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    wrappedStream := ss.(HarmonyWrappedServerStream)

    return handler(srv, wrappedStream)
}

解决方案


对于“服务器流式 rpc”,客户端发送一条消息(请求),服务器响应多条消息。在流拦截器中,需要调用一次 serverstream.recvmsg(...) 来获取客户端的请求。然后,您需要将“包装流”传递到 handler 中,稍后它将在第一次调用 recvmsg 时返回此消息。对于使用 protobuf 消息的 grpc,您可以执行以下操作。这个 serverstreamwrapper 将允许您在拦截器中调用 peekrequest(...) 来获取请求,然后将流传递给处理程序。

对于这个特定示例,您还可以在包装的 recvmsg(...) 函数本身中实现逻辑。 grpc 调用此函数来读取从客户端到服务器的请求。这可能比尝试存储请求更简单。

type serverStreamWrapper struct {
    peekedRequest proto.Message
    wrappedStream grpc.ServerStream
}

func newServerStreamWrapper(stream grpc.ServerStream) *serverStreamWrapper {
    return &serverStreamWrapper{nil, stream}
}

func (s *serverStreamWrapper) peekRequest(msg interface{}) error {
    protoMsg := msg.(proto.Message)
    if protoMsg == nil {
        panic("BUG: msg must not be nil")
    }
    if s.peekedRequest != nil {
        panic("BUG: Must only called peekRequest once")
    }

    err := s.wrappedStream.RecvMsg(protoMsg)
    if err == nil {
        s.peekedRequest = protoMsg
    }
    return err
}

func (s *serverStreamWrapper) RecvMsg(msg interface{}) error {
    if s.peekedRequest != nil {
        protoMsg := msg.(proto.Message)
        proto.Reset(protoMsg)
        proto.Merge(protoMsg, s.peekedRequest)
        s.peekedRequest = nil
        return nil
    }
    return s.wrappedStream.RecvMsg(msg)
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何通过流拦截器访问请求数据?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>