forked from TrueCloudLab/frostfs-api-go
30c6ca0714
Implement generic `Client` that can communicate with the remote server via protobuf `Message`'s. The client can uniformly execute any protobuf RPC on the remote server using any of the supported transport protocols. Currently only gRPC protocol is supported. Additionally implement helpful functions to transmit messages by one of the flow types: unary, client- or server-side stream. Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
60 lines
996 B
Go
60 lines
996 B
Go
package client
|
|
|
|
import (
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
// Option is a Client's option.
|
|
type Option func(*cfg)
|
|
|
|
type cfg struct {
|
|
addr string
|
|
|
|
dialTimeout time.Duration
|
|
|
|
conn *grpc.ClientConn
|
|
}
|
|
|
|
const defaultDialTimeout = 5 * time.Second
|
|
|
|
func defaultCfg() *cfg {
|
|
return &cfg{
|
|
dialTimeout: defaultDialTimeout,
|
|
}
|
|
}
|
|
|
|
// WithNetworkAddress returns option to specify
|
|
// network address of the remote server.
|
|
//
|
|
// Ignored if WithGRPCConn is provided.
|
|
func WithNetworkAddress(v string) Option {
|
|
return func(c *cfg) {
|
|
if v != "" {
|
|
c.addr = v
|
|
}
|
|
}
|
|
}
|
|
|
|
// WithDialTimeout returns option to specify
|
|
// dial timeout of the remote server connection.
|
|
//
|
|
// Ignored if WithGRPCConn is provided.
|
|
func WithDialTimeout(v time.Duration) Option {
|
|
return func(c *cfg) {
|
|
if v > 0 {
|
|
c.dialTimeout = v
|
|
}
|
|
}
|
|
}
|
|
|
|
// WithGRPCConn returns option to specify
|
|
// gRPC virtual connection.
|
|
func WithGRPCConn(v *grpc.ClientConn) Option {
|
|
return func(c *cfg) {
|
|
if v != nil {
|
|
c.conn = v
|
|
}
|
|
}
|
|
}
|