forked from TrueCloudLab/frostfs-api-go
[#2] rpc/client: Allow to override low-level gRPC options
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
cc8da15242
commit
45358d4551
3 changed files with 31 additions and 17 deletions
|
@ -1,5 +1,10 @@
|
||||||
package client
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
)
|
||||||
|
|
||||||
// Client represents client for exchanging messages
|
// Client represents client for exchanging messages
|
||||||
// with a remote server using Protobuf RPC.
|
// with a remote server using Protobuf RPC.
|
||||||
type Client struct {
|
type Client struct {
|
||||||
|
@ -15,5 +20,9 @@ func New(opts ...Option) *Client {
|
||||||
opt(&c.cfg)
|
opt(&c.cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.tlsCfg != nil {
|
||||||
|
c.grpcDialOpts = append(c.grpcDialOpts, grpc.WithTransportCredentials(credentials.NewTLS(c.tlsCfg)))
|
||||||
|
}
|
||||||
|
|
||||||
return &c
|
return &c
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,6 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
grpcstd "google.golang.org/grpc"
|
grpcstd "google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials"
|
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var errInvalidEndpoint = errors.New("invalid endpoint options")
|
var errInvalidEndpoint = errors.New("invalid endpoint options")
|
||||||
|
@ -23,21 +21,10 @@ func (c *Client) openGRPCConn(ctx context.Context) error {
|
||||||
return errInvalidEndpoint
|
return errInvalidEndpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
var creds credentials.TransportCredentials
|
|
||||||
|
|
||||||
if c.tlsCfg != nil {
|
|
||||||
creds = credentials.NewTLS(c.tlsCfg)
|
|
||||||
} else {
|
|
||||||
creds = insecure.NewCredentials()
|
|
||||||
}
|
|
||||||
|
|
||||||
dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
|
dialCtx, cancel := context.WithTimeout(ctx, c.dialTimeout)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
c.conn, err = grpcstd.DialContext(dialCtx, c.addr,
|
c.conn, err = grpcstd.DialContext(dialCtx, c.addr, c.grpcDialOpts...)
|
||||||
grpcstd.WithTransportCredentials(creds),
|
|
||||||
grpcstd.WithBlock(),
|
|
||||||
)
|
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
"google.golang.org/grpc/keepalive"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -22,18 +24,27 @@ type cfg struct {
|
||||||
rwTimeout time.Duration
|
rwTimeout time.Duration
|
||||||
|
|
||||||
tlsCfg *tls.Config
|
tlsCfg *tls.Config
|
||||||
|
grpcDialOpts []grpc.DialOption
|
||||||
|
|
||||||
conn *grpc.ClientConn
|
conn *grpc.ClientConn
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultDialTimeout = 5 * time.Second
|
defaultDialTimeout = 5 * time.Second
|
||||||
|
defaultKeepAliveTimeout = 5 * time.Second
|
||||||
defaultRWTimeout = 1 * time.Minute
|
defaultRWTimeout = 1 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *cfg) initDefault() {
|
func (c *cfg) initDefault() {
|
||||||
c.dialTimeout = defaultDialTimeout
|
c.dialTimeout = defaultDialTimeout
|
||||||
c.rwTimeout = defaultRWTimeout
|
c.rwTimeout = defaultRWTimeout
|
||||||
|
c.grpcDialOpts = []grpc.DialOption{
|
||||||
|
grpc.WithBlock(),
|
||||||
|
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||||
|
grpc.WithKeepaliveParams(keepalive.ClientParameters{
|
||||||
|
Timeout: defaultKeepAliveTimeout,
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithNetworkAddress returns option to specify
|
// WithNetworkAddress returns option to specify
|
||||||
|
@ -115,3 +126,10 @@ func WithGRPCConn(v *grpc.ClientConn) Option {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithGRPCDialOptions returns an option to specify grpc.DialOption.
|
||||||
|
func WithGRPCDialOptions(opts []grpc.DialOption) Option {
|
||||||
|
return func(c *cfg) {
|
||||||
|
c.grpcDialOpts = append(c.grpcDialOpts, opts...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue