From 1a33802c09d3db74bf46833ff58d67601c499a2f Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Thu, 29 Jun 2023 12:01:25 +0400 Subject: [PATCH] pool: Add easier way to pool configuration close #432 Signed-off-by: Evgenii Baidakov --- pool/example_pool_test.go | 34 +++++++++++++++++++++++++ pool/pool.go | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 pool/example_pool_test.go diff --git a/pool/example_pool_test.go b/pool/example_pool_test.go new file mode 100644 index 00000000..7e881b4d --- /dev/null +++ b/pool/example_pool_test.go @@ -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 + + // ... +} diff --git a/pool/pool.go b/pool/pool.go index 25e0e266..7cbf70e9 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -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: