[#205] v2/client: Replace WithGRPCDialContext option with WithDialTimeout
There is a need to set dial timeout in v2 client that is used in case of internal connection opening. Add DialTimeout option constructor to support this feature. Remove unused and no longer needed WithGRPCDialContext function. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
20ede88fe7
commit
03ac6bedb4
1 changed files with 14 additions and 7 deletions
|
@ -3,6 +3,7 @@ package client
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
)
|
)
|
||||||
|
@ -15,24 +16,26 @@ type cfg struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
|
|
||||||
gRPC cfgGRPC
|
gRPC cfgGRPC
|
||||||
|
|
||||||
|
dialTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type cfgGRPC struct {
|
type cfgGRPC struct {
|
||||||
dialCtx context.Context
|
|
||||||
|
|
||||||
dialOpts []grpc.DialOption
|
dialOpts []grpc.DialOption
|
||||||
|
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultDialTimeout = 5 * time.Second
|
||||||
|
|
||||||
func defaultCfg() *cfg {
|
func defaultCfg() *cfg {
|
||||||
return &cfg{
|
return &cfg{
|
||||||
gRPC: cfgGRPC{
|
gRPC: cfgGRPC{
|
||||||
dialCtx: context.Background(),
|
|
||||||
dialOpts: []grpc.DialOption{
|
dialOpts: []grpc.DialOption{
|
||||||
grpc.WithInsecure(),
|
grpc.WithInsecure(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
dialTimeout: defaultDialTimeout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +61,11 @@ func NewGRPCClientConn(opts ...Option) (*grpc.ClientConn, error) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.gRPC.conn, err = grpc.DialContext(cfg.gRPC.dialCtx, cfg.addr, cfg.gRPC.dialOpts...)
|
dialCtx, cancel := context.WithTimeout(context.Background(), cfg.dialTimeout)
|
||||||
|
|
||||||
|
cfg.gRPC.conn, err = grpc.DialContext(dialCtx, cfg.addr, cfg.gRPC.dialOpts...)
|
||||||
|
|
||||||
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -83,10 +90,10 @@ func WithNetConn(v net.Conn) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithGRPCDialContext(v context.Context) Option {
|
func WithDialTimeout(v time.Duration) Option {
|
||||||
return func(c *cfg) {
|
return func(c *cfg) {
|
||||||
if v != nil {
|
if v > 0 {
|
||||||
c.gRPC.dialCtx = v
|
c.dialTimeout = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue