Skip to content

Commit

Permalink
move out interceptor from the method
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Nov 20, 2024
1 parent c2e3075 commit 1901d84
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,18 +615,9 @@ func (ca *CoreAccessor) startGRPCClient(ctx context.Context) error {
if ca.tls != nil {
opts = append(opts, grpc.WithTransportCredentials(credentials.NewTLS(ca.tls)))
}
if ca.xtoken != "" {
authInterceptor := func(ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
ctx = metadata.AppendToOutgoingContext(ctx, "x-token", ca.xtoken)
return invoker(ctx, method, req, reply, cc, opts...)
}
opts = append(opts, grpc.WithUnaryInterceptor(authInterceptor))

if interceptor := authInterceptor(ca.xtoken); interceptor != nil {
opts = append(opts, grpc.WithUnaryInterceptor(interceptor))
}

client, err := grpc.NewClient(
Expand Down Expand Up @@ -704,3 +695,21 @@ func convertToSdkTxResponse(resp *user.TxResponse) *TxResponse {
Height: resp.Height,
}
}

func authInterceptor(xtoken string) grpc.UnaryClientInterceptor {
if xtoken == "" {
return nil
}
return func(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
ctx = metadata.AppendToOutgoingContext(ctx, "x-token", xtoken)
return invoker(ctx, method, req, reply, cc, opts...)
}

}

Check failure on line 715 in state/core_access.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

unnecessary trailing newline (whitespace)

0 comments on commit 1901d84

Please sign in to comment.