frostfs-api-go/v2/client/options.go
Leonard Lyubich 9253dac753 v2: Add cross-service client package
Add package with cross-service client utilities for working with transport
protocols and basic network types.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-09-18 10:43:36 +03:00

108 lines
1.6 KiB
Go

package client
import (
"context"
"net"
"google.golang.org/grpc"
)
type Option func(*cfg)
type cfg struct {
addr string
conn net.Conn
gRPC cfgGRPC
}
type cfgGRPC struct {
dialCtx context.Context
dialOpts []grpc.DialOption
conn *grpc.ClientConn
}
func defaultCfg() *cfg {
return &cfg{
gRPC: cfgGRPC{
dialCtx: context.Background(),
dialOpts: []grpc.DialOption{
grpc.WithInsecure(),
},
},
}
}
func NewGRPCClientConn(opts ...Option) (*grpc.ClientConn, error) {
cfg := defaultCfg()
for i := range opts {
opts[i](cfg)
}
var err error
if cfg.gRPC.conn == nil {
if cfg.conn != nil {
if cfg.addr == "" {
cfg.addr = cfg.conn.RemoteAddr().String()
}
cfg.gRPC.dialOpts = append(cfg.gRPC.dialOpts,
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return cfg.conn, nil
}),
)
}
cfg.gRPC.conn, err = grpc.DialContext(cfg.gRPC.dialCtx, cfg.addr, cfg.gRPC.dialOpts...)
if err != nil {
return nil, err
}
}
return cfg.gRPC.conn, err
}
func WithNetworkAddress(v string) Option {
return func(c *cfg) {
if v != "" {
c.addr = v
}
}
}
func WithNetConn(v net.Conn) Option {
return func(c *cfg) {
if v != nil {
c.conn = v
}
}
}
func WithGRPCDialContext(v context.Context) Option {
return func(c *cfg) {
if v != nil {
c.gRPC.dialCtx = v
}
}
}
func WithGRPCDialOpts(v []grpc.DialOption) Option {
return func(c *cfg) {
if len(v) > 0 {
c.gRPC.dialOpts = v
}
}
}
func WithGRPCConn(v *grpc.ClientConn) Option {
return func(c *cfg) {
if v != nil {
c.gRPC.conn = v
}
}
}