[#343] client: Accept context parameter in Dial
In previous implementation of `Client.Dial` there was no ability to specify parent context (e.g. global application context). Add `PrmDial.SetContext` method which accepts optional base dial context. Use the context to open client connection or fall back to using `context.Background()`. Upgraded version of `github.com/nspcc-dev/neofs-api-go/v2` module also fixes the problem when dial timeout didn't work properly. Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
This commit is contained in:
parent
1325b4f272
commit
452a50e9d5
4 changed files with 53 additions and 2 deletions
|
@ -1,8 +1,10 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
||||
|
@ -63,6 +65,10 @@ func (c *Client) Init(prm PrmInit) {
|
|||
// Returns an error describing failure reason. If failed, the Client
|
||||
// SHOULD NOT be used.
|
||||
//
|
||||
// Uses the context specified by SetContext if it was called with non-nil
|
||||
// argument, otherwise context.Background() is used. Dial returns context
|
||||
// errors, see context package docs for details.
|
||||
//
|
||||
// Panics if required parameters are set incorrectly, look carefully
|
||||
// at the method documentation.
|
||||
//
|
||||
|
@ -100,7 +106,13 @@ func (c *Client) Dial(prm PrmDial) error {
|
|||
c.setNeoFSAPIServer((*coreServer)(&c.c))
|
||||
|
||||
// TODO: (neofs-api-go#382) perform generic dial stage of the client.Client
|
||||
_, _ = rpc.Balance(&c.c, new(v2accounting.BalanceRequest))
|
||||
_, err := rpc.Balance(&c.c, new(v2accounting.BalanceRequest),
|
||||
client.WithContext(prm.parentCtx),
|
||||
)
|
||||
// return context errors since they signal about dial problem
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -174,6 +186,8 @@ type PrmDial struct {
|
|||
|
||||
streamTimeoutSet bool
|
||||
streamTimeout time.Duration
|
||||
|
||||
parentCtx context.Context
|
||||
}
|
||||
|
||||
// SetServerURI sets server URI in the NeoFS network.
|
||||
|
@ -214,3 +228,11 @@ func (x *PrmDial) SetStreamTimeout(timeout time.Duration) {
|
|||
x.streamTimeoutSet = true
|
||||
x.streamTimeout = timeout
|
||||
}
|
||||
|
||||
// SetContext allows to specify optional base context within which connection
|
||||
// should be established.
|
||||
//
|
||||
// Context SHOULD NOT be nil.
|
||||
func (x *PrmDial) SetContext(ctx context.Context) {
|
||||
x.parentCtx = ctx
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
|
@ -37,3 +38,31 @@ func newClient(server neoFSAPIServer) *Client {
|
|||
|
||||
return &c
|
||||
}
|
||||
|
||||
func TestClient_DialContext(t *testing.T) {
|
||||
var c Client
|
||||
|
||||
// try to connect to any host
|
||||
var prm PrmDial
|
||||
prm.SetServerURI("localhost:8080")
|
||||
|
||||
assert := func(ctx context.Context, errExpected error) {
|
||||
// use the particular context
|
||||
prm.SetContext(ctx)
|
||||
|
||||
// expect particular context error according to Dial docs
|
||||
require.ErrorIs(t, c.Dial(prm), errExpected)
|
||||
}
|
||||
|
||||
// create pre-abandoned context
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
assert(ctx, context.Canceled)
|
||||
|
||||
// create "pre-deadlined" context
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 0)
|
||||
defer cancel()
|
||||
|
||||
assert(ctx, context.DeadlineExceeded)
|
||||
}
|
||||
|
|
2
go.mod
2
go.mod
|
@ -9,7 +9,7 @@ require (
|
|||
github.com/mr-tron/base58 v1.2.0
|
||||
github.com/nspcc-dev/hrw v1.0.9
|
||||
github.com/nspcc-dev/neo-go v0.99.2
|
||||
github.com/nspcc-dev/neofs-api-go/v2 v2.13.2-0.20221004142957-5fc2644c680d
|
||||
github.com/nspcc-dev/neofs-api-go/v2 v2.13.2-0.20221005093543-3a91383f24a9
|
||||
github.com/nspcc-dev/neofs-contract v0.15.3
|
||||
github.com/nspcc-dev/tzhash v1.6.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
|
|
BIN
go.sum
BIN
go.sum
Binary file not shown.
Loading…
Reference in a new issue