From c49e53ba9d4caea259867cb8f4b38a38c0b63174 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 15 Dec 2021 21:04:36 +0300 Subject: [PATCH] [#1031] morph: Add `maxConnPerHost` option Signed-off-by: Pavel Karpy --- cmd/neofs-node/morph.go | 1 + pkg/morph/client/constructor.go | 17 +++++++++++++++-- pkg/morph/client/multi.go | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 7190026f1..e1584131f 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -49,6 +49,7 @@ func initMorphComponents(c *cfg) { client.WithDialTimeout(dialTimeout), client.WithLogger(c.log), client.WithExtraEndpoints(addresses[1:]), + client.WithMaxConnectionPerHost(morphconfig.MaxConnPerHost(c.appCfg)), ) if err == nil { handler(cli) diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 18374dbc6..3ad89d556 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -32,12 +32,15 @@ type cfg struct { extraEndpoints []string + maxConnPerHost int + singleCli *client.Client // neo-go client for single client mode } const ( - defaultDialTimeout = 5 * time.Second - defaultWaitInterval = 500 * time.Millisecond + defaultDialTimeout = 5 * time.Second + defaultWaitInterval = 500 * time.Millisecond + defaultMaxConnPerHost = 10 ) func defaultConfig() *cfg { @@ -49,6 +52,7 @@ func defaultConfig() *cfg { signer: &transaction.Signer{ Scopes: transaction.Global, }, + maxConnPerHost: defaultMaxConnPerHost, } } @@ -166,6 +170,15 @@ func WithExtraEndpoints(endpoints []string) Option { } } +// WithMaxConnectionPerHost returns a client constructor +// option that specifies Neo client's maximum opened +// connection per one host. +func WithMaxConnectionPerHost(m int) Option { + return func(c *cfg) { + c.maxConnPerHost = m + } +} + // WithSingleClient returns a client constructor option // that specifies single neo-go client and forces Client // to use it and only it for requests. diff --git a/pkg/morph/client/multi.go b/pkg/morph/client/multi.go index 56c787a2e..56b08633a 100644 --- a/pkg/morph/client/multi.go +++ b/pkg/morph/client/multi.go @@ -25,7 +25,8 @@ type multiClient struct { // note: must be wrapped into mutex lock. func (x *multiClient) createForAddress(addr string) (*Client, error) { cli, err := client.New(x.cfg.ctx, addr, client.Options{ - DialTimeout: x.cfg.dialTimeout, + DialTimeout: x.cfg.dialTimeout, + MaxConnsPerHost: x.cfg.maxConnPerHost, }) if err != nil { return nil, err