From 3787477133f34cc55fddb9b685b64314b8469781 Mon Sep 17 00:00:00 2001 From: aarifullin Date: Mon, 13 Nov 2023 15:35:30 +0300 Subject: [PATCH] [#189] client: Make PrmDial fields public Signed-off-by: Airat Arifullin --- client/client.go | 68 +++++++++++++++++++++++-------------------- client/client_test.go | 5 ++-- client/common.go | 1 - pool/pool.go | 22 +++++++------- 4 files changed, 51 insertions(+), 45 deletions(-) diff --git a/client/client.go b/client/client.go index c111f02..014b3d7 100644 --- a/client/client.go +++ b/client/client.go @@ -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 - } - } else { - prm.timeoutDial = 5 * time.Second + if prm.DialTimeout <= 0 { + prm.DialTimeout = defaultDialTimeout } - - 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 } diff --git a/client/client_test.go b/client/client_test.go index 1f05544..d2e5274 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -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 diff --git a/client/common.go b/client/common.go index c67084a..11cec28 100644 --- a/client/common.go +++ b/client/common.go @@ -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") diff --git a/pool/pool.go b/pool/pool.go index da8f099..a656332 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -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()