-
Couldn't load subscription status.
- Fork 147
add gRPC support #434
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
add gRPC support #434
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we need this builder pattern - can't we just create a |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| package grpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/keepalive" | ||
| ) | ||
|
|
||
| type authConfig struct { | ||
| bearerToken string | ||
| headers map[string]string | ||
| } | ||
|
|
||
| type keepaliveConfig struct { | ||
| time time.Duration | ||
| timeout time.Duration | ||
| permitWithoutStream bool | ||
| } | ||
|
|
||
| type clientOpts struct { | ||
| authority string | ||
| userAgent string | ||
| loadBalancingPolicy string | ||
| maxSendMsgBytes int | ||
| maxRecvMsgBytes int | ||
| keepalive keepaliveConfig | ||
| auth authConfig | ||
| } | ||
|
|
||
| func buildDialOptions(_ context.Context, useTLS bool, cfg clientOpts) ([]grpc.DialOption, error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come we have a ctx in the call signature if we aren't using it? |
||
| return newDialOptionsBuilder(useTLS, cfg). | ||
| withTransport(). | ||
| withAuthority(). | ||
| withUserAgent(). | ||
| withLBPolicy(). | ||
| withKeepalive(). | ||
| withMsgSizeOpts(). | ||
| withPerRPCCreds(). | ||
| build() | ||
| } | ||
|
|
||
| // dialOptionsBuilder constructs grpc.DialOptions using a fluent API for readability. | ||
| type dialOptionsBuilder struct { | ||
| useTLS bool | ||
| cfg clientOpts | ||
| opts []grpc.DialOption | ||
| callOpts []grpc.CallOption | ||
| } | ||
|
|
||
| func newDialOptionsBuilder(useTLS bool, cfg clientOpts) *dialOptionsBuilder { | ||
| return &dialOptionsBuilder{ | ||
| useTLS: useTLS, | ||
| cfg: cfg, | ||
| } | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withTransport() *dialOptionsBuilder { | ||
| if b.useTLS { | ||
| // Use default system roots; for advanced CA/mTLS provide a custom config downstream. | ||
| tlsCfg := &tls.Config{MinVersion: tls.VersionTLS12} | ||
| b.opts = append(b.opts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg))) | ||
|
Comment on lines
+66
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This whole tls setup is really confusing - seems like we use |
||
| } else { | ||
| b.opts = append(b.opts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withAuthority() *dialOptionsBuilder { | ||
| if b.cfg.authority != "" { | ||
| b.opts = append(b.opts, grpc.WithAuthority(b.cfg.authority)) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withUserAgent() *dialOptionsBuilder { | ||
| if b.cfg.userAgent != "" { | ||
| b.opts = append(b.opts, grpc.WithUserAgent(b.cfg.userAgent)) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withLBPolicy() *dialOptionsBuilder { | ||
| if b.cfg.loadBalancingPolicy != "" { | ||
| b.opts = append(b.opts, grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"loadBalancingPolicy":"%s"}`, b.cfg.loadBalancingPolicy))) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withKeepalive() *dialOptionsBuilder { | ||
| if b.cfg.keepalive.time > 0 || b.cfg.keepalive.timeout > 0 || b.cfg.keepalive.permitWithoutStream { | ||
| b.opts = append(b.opts, grpc.WithKeepaliveParams(keepalive.ClientParameters{ | ||
| Time: b.cfg.keepalive.time, | ||
| Timeout: b.cfg.keepalive.timeout, | ||
| PermitWithoutStream: b.cfg.keepalive.permitWithoutStream, | ||
| })) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withMsgSizeOpts() *dialOptionsBuilder { | ||
| if b.cfg.maxSendMsgBytes > 0 { | ||
| b.callOpts = append(b.callOpts, grpc.MaxCallSendMsgSize(b.cfg.maxSendMsgBytes)) | ||
| } | ||
| if b.cfg.maxRecvMsgBytes > 0 { | ||
| b.callOpts = append(b.callOpts, grpc.MaxCallRecvMsgSize(b.cfg.maxRecvMsgBytes)) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) withPerRPCCreds() *dialOptionsBuilder { | ||
| if b.cfg.auth.bearerToken != "" || len(b.cfg.auth.headers) > 0 { | ||
| b.opts = append(b.opts, grpc.WithPerRPCCredentials(headerCreds{ | ||
| token: b.cfg.auth.bearerToken, | ||
| headers: b.cfg.auth.headers, | ||
| secureOnly: true, | ||
| })) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func (b *dialOptionsBuilder) build() ([]grpc.DialOption, error) { | ||
| if len(b.callOpts) > 0 { | ||
| b.opts = append(b.opts, grpc.WithDefaultCallOptions(b.callOpts...)) | ||
| } | ||
| return b.opts, nil | ||
| } | ||
|
|
||
| type headerCreds struct { | ||
| token string | ||
| headers map[string]string | ||
| secureOnly bool | ||
| } | ||
|
|
||
| func (h headerCreds) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) { | ||
| md := map[string]string{} | ||
| if h.token != "" { | ||
| md["authorization"] = "Bearer " + h.token | ||
| } | ||
| for k, v := range h.headers { | ||
| md[k] = v | ||
| } | ||
| return md, nil | ||
| } | ||
|
|
||
| func (h headerCreds) RequireTransportSecurity() bool { return h.secureOnly } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we should pin these deps rather than using the latest tag