[#1031] morph: Add maxConnPerHost option

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-12-15 21:04:36 +03:00 committed by Alex Vanin
parent 63e035bd8a
commit c49e53ba9d
3 changed files with 18 additions and 3 deletions

View file

@ -49,6 +49,7 @@ func initMorphComponents(c *cfg) {
client.WithDialTimeout(dialTimeout), client.WithDialTimeout(dialTimeout),
client.WithLogger(c.log), client.WithLogger(c.log),
client.WithExtraEndpoints(addresses[1:]), client.WithExtraEndpoints(addresses[1:]),
client.WithMaxConnectionPerHost(morphconfig.MaxConnPerHost(c.appCfg)),
) )
if err == nil { if err == nil {
handler(cli) handler(cli)

View file

@ -32,12 +32,15 @@ type cfg struct {
extraEndpoints []string extraEndpoints []string
maxConnPerHost int
singleCli *client.Client // neo-go client for single client mode singleCli *client.Client // neo-go client for single client mode
} }
const ( const (
defaultDialTimeout = 5 * time.Second defaultDialTimeout = 5 * time.Second
defaultWaitInterval = 500 * time.Millisecond defaultWaitInterval = 500 * time.Millisecond
defaultMaxConnPerHost = 10
) )
func defaultConfig() *cfg { func defaultConfig() *cfg {
@ -49,6 +52,7 @@ func defaultConfig() *cfg {
signer: &transaction.Signer{ signer: &transaction.Signer{
Scopes: transaction.Global, 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 // WithSingleClient returns a client constructor option
// that specifies single neo-go client and forces Client // that specifies single neo-go client and forces Client
// to use it and only it for requests. // to use it and only it for requests.

View file

@ -25,7 +25,8 @@ type multiClient struct {
// note: must be wrapped into mutex lock. // note: must be wrapped into mutex lock.
func (x *multiClient) createForAddress(addr string) (*Client, error) { func (x *multiClient) createForAddress(addr string) (*Client, error) {
cli, err := client.New(x.cfg.ctx, addr, client.Options{ 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 { if err != nil {
return nil, err return nil, err