Skip to content

Commit

Permalink
Easier way to configure a pool (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthulhu-rider authored Jul 3, 2023
2 parents 136c77a + e11a7d5 commit 4f36bc4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
34 changes: 34 additions & 0 deletions pool/example_pool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pool

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"

neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
)

func ExampleNew_easiestWay() {
// Signer generation, like example.
pk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
signer := neofsecdsa.SignerRFC6979(*pk)

pool, _ := New(NewFlatNodeParams([]string{"grpc://localhost:8080", "grpcs://localhost:8081"}), signer, DefaultOptions())
_ = pool

// ...
}

func ExampleNew_adjustingParameters() {
// Signer generation, like example.
pk, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
signer := neofsecdsa.SignerRFC6979(*pk)

opts := DefaultOptions()
opts.SetErrorThreshold(10)

pool, _ := New(NewFlatNodeParams([]string{"grpc://localhost:8080", "grpcs://localhost:8081"}), signer, opts)
_ = pool

// ...
}
52 changes: 52 additions & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,58 @@ const (
defaultStreamTimeout = 10 * time.Second
)

// DefaultOptions returns default option preset for Pool creation. It may be used like start point for configuration or
// like main configuration.
func DefaultOptions() InitParameters {
params := InitParameters{
sessionExpirationDuration: defaultSessionTokenExpirationDuration,
errorThreshold: defaultErrorThreshold,
clientRebalanceInterval: defaultRebalanceInterval,
healthcheckTimeout: defaultHealthcheckTimeout,
nodeDialTimeout: defaultDialTimeout,
nodeStreamTimeout: defaultStreamTimeout,
}

return params
}

// New creates connection pool using simple set of endpoints and parameters.
//
// See also [pool.DefaultOptions] and [pool.NewFlatNodeParams] for details.
//
// Returned errors:
// - [neofscrypto.ErrIncorrectSigner]
func New(endpoints []NodeParam, signer neofscrypto.Signer, options InitParameters) (*Pool, error) {
if len(endpoints) == 0 {
return nil, errors.New("empty endpoints")
}

options.nodeParams = endpoints
options.signer = signer

return NewPool(options)
}

// NewFlatNodeParams converts endpoints to appropriate NodeParam.
// It is useful for situations where all endpoints are equivalent.
func NewFlatNodeParams(endpoints []string) []NodeParam {
if len(endpoints) == 0 {
return nil
}

params := make([]NodeParam, 0, len(endpoints))

for _, addr := range endpoints {
params = append(params, NodeParam{
priority: 1,
address: addr,
weight: 1,
})
}

return params
}

// NewPool creates connection pool using parameters.
//
// Returned errors:
Expand Down
20 changes: 2 additions & 18 deletions pool/pool_aio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,7 @@ func TestPoolInterfaceWithAIO(t *testing.T) {

nodeAddr := "grpc://localhost:8080"

opts := InitParameters{
signer: signer,
nodeParams: []NodeParam{
{1, nodeAddr, 1},
},
clientRebalanceInterval: 30 * time.Second,
}

pool, err := NewPool(opts)
pool, err := New(NewFlatNodeParams([]string{nodeAddr}), signer, DefaultOptions())
require.NoError(t, err)
require.NoError(t, pool.Dial(ctx))

Expand Down Expand Up @@ -259,15 +251,7 @@ func TestPoolWaiterWithAIO(t *testing.T) {
account, signer, cont := testData(t)
var eaclTable eacl.Table

opts := InitParameters{
signer: signer,
nodeParams: []NodeParam{
{1, "grpc://localhost:8080", 1},
},
clientRebalanceInterval: 30 * time.Second,
}

pool, err := NewPool(opts)
pool, err := New(NewFlatNodeParams([]string{"grpc://localhost:8080"}), signer, DefaultOptions())
require.NoError(t, err)
require.NoError(t, pool.Dial(ctx))

Expand Down

0 comments on commit 4f36bc4

Please sign in to comment.