Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: add binary-grpc-generic-call #1242

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

tksky1
Copy link

@tksky1 tksky1 commented Feb 2, 2024

What type of PR is this?

feat

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level. User docs repo

(Optional) Translate the PR title into Chinese.

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en: add BinaryGrpcGeneric type for generic client to support grpc in unary generic call.
users can now use something like
genericclient.NewClient("a.b.c", generic.BinaryGrpcGeneric("package.service"), ...)
to create generic client that supports grpc.
zh(optional):

(Optional) Which issue(s) this PR fixes:

(optional) The PR that updates user documentation:

@tksky1 tksky1 requested review from a team as code owners February 2, 2024 09:37
Copy link

codecov bot commented Feb 2, 2024

Codecov Report

Attention: 35 lines in your changes are missing coverage. Please review.

Comparison is base (34b88b2) 68.46% compared to head (df6692c) 68.35%.

Files Patch % Lines
pkg/generic/generic_grpc.go 0.00% 17 Missing ⚠️
pkg/remote/codec/grpc/grpc.go 0.00% 10 Missing ⚠️
pkg/remote/trans/nphttp2/client_handler.go 0.00% 4 Missing and 1 partial ⚠️
client/genericclient/client.go 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1242      +/-   ##
===========================================
- Coverage    68.46%   68.35%   -0.11%     
===========================================
  Files          284      285       +1     
  Lines        20618    20654      +36     
===========================================
+ Hits         14117    14119       +2     
- Misses        5319     5351      +32     
- Partials      1182     1184       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -42,6 +42,11 @@ func NewClientWithServiceInfo(destService string, g generic.Generic, svcInfo *se
options = append(options, client.WithDestService(destService))
options = append(options, opts...)

if g.PayloadCodecType() == serviceinfo.Protobuf {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里别通过service info来判断grpc,因为thrift协议场景也可以走pb序列化
建议直接判断是不是grpc generic那个结构体?

return binaryG.grpcSvcName
}

func (g *binaryGrpcGeneric) Framed() bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

framed为啥是true?可以看看这个调用是哪里用到的。framed一般是指thrift协议的特性

}

func (g *binaryGrpcGeneric) PayloadCodec() remote.PayloadCodec {
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

现在返回nil是因为你复用了grpcCodec没单独搞个payload吧?后面觉得可能要把grpcCodec的代码适当拆分下?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不过作为demo来看,现在这样搞也还行

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要搞成和thrift那边一样加payloadCodec,可能你的grpcCodec那里原来那个switch的分支都要改改

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okok先这样

@@ -42,6 +42,11 @@ func NewClientWithServiceInfo(destService string, g generic.Generic, svcInfo *se
options = append(options, client.WithDestService(destService))
options = append(options, opts...)

if g.PayloadCodecType() == serviceinfo.Protobuf {
svcInfo.Extra["grpcGeneric"] = true
svcInfo.ServiceName = generic.GetGrpcSvcName(g)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得可以把extra去掉?泛化场景都默认尝试填充service name
thrift场景之前没有填也没有传递,所以就算是空的也不影响?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个grpcGeneric的字段是我新加的,主要是原来的逻辑直接通过设service name为“$genericCall”来判断是泛化场景的,grpc要求加了service name就只能加一个新的字段了

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯。以后可能 thrift 场景也需要。demo 就先这样吧

)

// BinaryGrpcGeneric raw grpc binary Generic
func BinaryGrpcGeneric(grpcSvcName string) Generic {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不对,我又想了下,service name还是需要每次随着call的时候传入,泛化grpc client需要有每次调用不同service.mehtod的能力

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那可能需要改造一下GenericCall(),或者加个callOption?看看怎么搞和thrift比较兼容

@@ -132,6 +133,15 @@ func (c *grpcCodec) Encode(ctx context.Context, message remote.Message, out remo
payload, err = proto.Marshal(t)
case protobuf.ProtobufMsgCodec:
payload, err = t.Marshal(nil)
case *generic.Args:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

编码不一定是 Protobuf,也可能是 thrift 或者任意数据

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更通用的方式是,generic.Args 定义为接口,只要实现了这个接口的类型都可以在这里用

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例如

type Args interface {
    Marshal(ctx context.Context) ([]byte, error)
}

Copy link
Contributor

@felix021 felix021 Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有应该要提供一种方式,可以让client修改 content-type,目前默认是

application/grpc

对于 thrift,则是按照 grpc 的规范增加了个 contentSubtype(可以在代码中搜这个变量名)

application/grpc+thrift

@@ -194,6 +204,9 @@ func (c *grpcCodec) Decode(ctx context.Context, message remote.Message, in remot
return proto.Unmarshal(d, t)
case protobuf.ProtobufMsgCodec:
return t.Unmarshal(d)
case *generic.Result:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上,编码不一定是 Protobuf,也可能是 thrift 或者任意数据

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants