Skip to content

Commit

Permalink
✨ feat: support grpc custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing committed Jul 18, 2024
1 parent 687f4ed commit ff86fca
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/config/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ func (e *GRPC) Init(

type Clients map[string]ServerParams

func (cs Clients) makeClient(key string) *grpc.ClientConn {
func (cs Clients) makeClient(key string, opts ...grpc.DialOption) *grpc.ClientConn {
params, ok := cs[key]
if !ok {
return nil
}
opts := make([]grpc.DialOption, 0)
if opts == nil {
opts = make([]grpc.DialOption, 0)
}
if params.CertFile != "" && params.KeyFile != "" {
creds, err := credentials.NewClientTLSFromFile(params.CertFile, params.KeyFile)
if err != nil {
Expand All @@ -66,13 +68,13 @@ func (cs Clients) makeClient(key string) *grpc.ClientConn {
timeout.UnaryClientInterceptor(params.Timeout),
))
}
conn, err := grpc.Dial(params.Addr, opts...)
conn, err := grpc.NewClient(params.Addr, opts...)
if err != nil {
return nil
}
return conn
}

func (e *GRPC) GetGRPCClient(key string) *grpc.ClientConn {
return e.Clients.makeClient(key)
func (e *GRPC) GetGRPCClient(key string, opts ...grpc.DialOption) *grpc.ClientConn {
return e.Clients.makeClient(key, opts...)
}

0 comments on commit ff86fca

Please sign in to comment.