go-kratos / gateway

A high-performance API Gateway with middlewares, supporting HTTP and gRPC protocols.
MIT License
686 stars 134 forks source link

transcoder 中间件 grpc 判断疑似有错误 #223

Closed kvii closed 8 months ago

kvii commented 9 months ago

https://github.com/go-kratos/gateway/blob/abf713bc415f6f2fd68e933c479f0dbe7a1fd12b/middleware/transcoder/transcoder.go#L47

这里的 strings.HasPrefix 是不是应该写成 !strings.HasPrefix 呢?感觉这里应该想写“如果配置不是 grpc 协议或者不是 grpc 响应头就直接返回“

dosubot[bot] commented 9 months ago

🤖

Hello @kvii!

I'm Dosu, a friendly bot here to assist you with any questions, bugs, or contribution-related matters. While we're waiting for a human maintainer, I'm at your service! 😊

您好,kvii。关于您提出的问题,strings.HasPrefix 在这里的使用是正确的,不需要改为 !strings.HasPrefix

这段代码的意图是,如果端点协议不是 gRPC 或者请求的内容类型已经是 "application/grpc",那么就跳过这个中间件,直接调用 next.RoundTrip(req) 进行下一步操作。

if endpoint.Protocol != config.Protocol_GRPC || strings.HasPrefix(contentType, "application/grpc") {
    return next.RoundTrip(req)
}

这里的逻辑是,对于非 gRPC 协议和已经是 gRPC 内容类型的请求,我们不需要进行转码操作,所以直接跳过。因为这个转码中间件的目的就是将非 gRPC 请求转换为 gRPC 格式,所以对于已经是 gRPC 格式的请求或者非 gRPC 协议,我们不需要这个中间件。

因此,将 strings.HasPrefix 改为 !strings.HasPrefix 会反转这个逻辑,导致对于非 gRPC 内容类型的请求跳过这个中间件,这并不是我们期望的行为。

参考资料:

希望这个解答能帮助到您,如果您还有其他问题,欢迎继续提问。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.