[#189] client: Make PrmDial fields public

Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
This commit is contained in:
Airat Arifullin 2023-11-13 15:35:30 +03:00
parent e91d40e250
commit 3787477133
4 changed files with 51 additions and 45 deletions

View file

@ -78,31 +78,22 @@ func (c *Client) Init(prm PrmInit) {
//
// See also Init / Close.
func (c *Client) Dial(ctx context.Context, prm PrmDial) error {
if prm.endpoint == "" {
if prm.Endpoint == "" {
return errorServerAddrUnset
}
if prm.timeoutDialSet {
if prm.timeoutDial <= 0 {
return errorNonPositiveTimeout
if prm.DialTimeout <= 0 {
prm.DialTimeout = defaultDialTimeout
}
} else {
prm.timeoutDial = 5 * time.Second
}
if prm.streamTimeoutSet {
if prm.streamTimeout <= 0 {
return errorNonPositiveTimeout
}
} else {
prm.streamTimeout = 10 * time.Second
if prm.StreamTimeout <= 0 {
prm.StreamTimeout = defaultStreamTimeout
}
c.c = *client.New(append(
client.WithNetworkURIAddress(prm.endpoint, prm.tlsConfig),
client.WithDialTimeout(prm.timeoutDial),
client.WithRWTimeout(prm.streamTimeout),
client.WithGRPCDialOptions(prm.dialOptions),
client.WithNetworkURIAddress(prm.Endpoint, prm.TLSConfig),
client.WithDialTimeout(prm.DialTimeout),
client.WithRWTimeout(prm.StreamTimeout),
client.WithGRPCDialOptions(prm.GRPCDialOptions),
)...)
c.setFrostFSAPIServer((*coreServer)(&c.c))
@ -185,21 +176,26 @@ func (x *PrmInit) SetResponseInfoCallback(f func(ResponseMetaInfo) error) {
x.ResponseInfoCallback = f
}
const (
defaultDialTimeout = 5 * time.Second
defaultStreamTimeout = 10 * time.Second
)
// PrmDial groups connection parameters for the Client.
//
// See also Dial.
type PrmDial struct {
endpoint string
Endpoint string
tlsConfig *tls.Config
TLSConfig *tls.Config
timeoutDialSet bool
timeoutDial time.Duration
// If DialTimeout is non-positive, then it's set to defaultDialTimeout.
DialTimeout time.Duration
streamTimeoutSet bool
streamTimeout time.Duration
// If StreamTimeout is non-positive, then it's set to defaultStreamTimeout.
StreamTimeout time.Duration
dialOptions []grpc.DialOption
GRPCDialOptions []grpc.DialOption
}
// SetServerURI sets server URI in the FrostFS network.
@ -215,33 +211,41 @@ type PrmDial struct {
// grpcs
//
// See also SetTLSConfig.
//
// Deprecated: Use PrmDial.Endpoint instead.
func (x *PrmDial) SetServerURI(endpoint string) {
x.endpoint = endpoint
x.Endpoint = endpoint
}
// SetTLSConfig sets tls.Config to open TLS client connection
// to the FrostFS server. Nil (default) means insecure connection.
//
// See also SetServerURI.
//
// Depreacted: Use PrmDial.TLSConfig instead.
func (x *PrmDial) SetTLSConfig(tlsConfig *tls.Config) {
x.tlsConfig = tlsConfig
x.TLSConfig = tlsConfig
}
// SetTimeout sets the timeout for connection to be established.
// MUST BE positive. If not called, 5s timeout will be used by default.
//
// Deprecated: Use PrmDial.DialTimeout instead.
func (x *PrmDial) SetTimeout(timeout time.Duration) {
x.timeoutDialSet = true
x.timeoutDial = timeout
x.DialTimeout = timeout
}
// SetStreamTimeout sets the timeout for individual operations in streaming RPC.
// MUST BE positive. If not called, 10s timeout will be used by default.
//
// Deprecated: Use PrmDial.StreamTimeout instead.
func (x *PrmDial) SetStreamTimeout(timeout time.Duration) {
x.streamTimeoutSet = true
x.streamTimeout = timeout
x.StreamTimeout = timeout
}
// SetGRPCDialOptions sets the gRPC dial options for new gRPC client connection.
//
// Deprecated: Use PrmDial.GRPCDialOptions instead.
func (x *PrmDial) SetGRPCDialOptions(opts ...grpc.DialOption) {
x.dialOptions = opts
x.GRPCDialOptions = opts
}

View file

@ -44,8 +44,9 @@ func TestClient_DialContext(t *testing.T) {
var c Client
// try to connect to any host
var prm PrmDial
prm.SetServerURI("localhost:8080")
prm := PrmDial{
Endpoint: "localhost:8080",
}
assert := func(ctx context.Context, errExpected error) {
// expect particular context error according to Dial docs

View file

@ -50,7 +50,6 @@ var (
errorMissingObject = errors.New("missing object")
errorAccountNotSet = errors.New("account not set")
errorServerAddrUnset = errors.New("server address is unset or empty")
errorNonPositiveTimeout = errors.New("non-positive timeout")
errorEACLTableNotSet = errors.New("eACL table not set")
errorMissingAnnouncements = errors.New("missing announcements")
errorZeroRangeLength = errors.New("zero range length")

View file

@ -341,11 +341,12 @@ func (c *clientWrapper) dial(ctx context.Context) error {
return err
}
var prmDial sdkClient.PrmDial
prmDial.SetServerURI(c.prm.address)
prmDial.SetTimeout(c.prm.dialTimeout)
prmDial.SetStreamTimeout(c.prm.streamTimeout)
prmDial.SetGRPCDialOptions(c.prm.dialOptions...)
prmDial := sdkClient.PrmDial{
Endpoint: c.prm.address,
DialTimeout: c.prm.dialTimeout,
StreamTimeout: c.prm.streamTimeout,
GRPCDialOptions: c.prm.dialOptions,
}
if err = cl.Dial(ctx, prmDial); err != nil {
c.setUnhealthyOnDial()
@ -379,11 +380,12 @@ func (c *clientWrapper) restartIfUnhealthy(ctx context.Context) (healthy, change
cl.Init(prmInit)
var prmDial sdkClient.PrmDial
prmDial.SetServerURI(c.prm.address)
prmDial.SetTimeout(c.prm.dialTimeout)
prmDial.SetStreamTimeout(c.prm.streamTimeout)
prmDial.SetGRPCDialOptions(c.prm.dialOptions...)
prmDial := sdkClient.PrmDial{
Endpoint: c.prm.address,
DialTimeout: c.prm.dialTimeout,
StreamTimeout: c.prm.streamTimeout,
GRPCDialOptions: c.prm.dialOptions,
}
if err := cl.Dial(ctx, prmDial); err != nil {
c.setUnhealthyOnDial()