Skip to content

Commit

Permalink
pool: Add easier way to pool configuration
Browse files Browse the repository at this point in the history
close #432

Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Jun 30, 2023
1 parent a52a8e0 commit 1a33802
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 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

0 comments on commit 1a33802

Please sign in to comment.