From d240e2dc87ed4e5b47c8784bbdd5d285822fb98c Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 12 Oct 2022 20:12:35 +0300 Subject: [PATCH] [#1713] morph: Allow switch interval configuration Signed-off-by: Pavel Karpy --- pkg/morph/client/constructor.go | 17 +++++++++++++++-- pkg/morph/client/multi.go | 4 +--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index 45de2e906..8db9ff523 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -44,11 +44,14 @@ type cfg struct { singleCli *rpcclient.WSClient // neo-go client for single client mode inactiveModeCb Callback + + switchInterval time.Duration } const ( - defaultDialTimeout = 5 * time.Second - defaultWaitInterval = 500 * time.Millisecond + defaultDialTimeout = 5 * time.Second + defaultWaitInterval = 500 * time.Millisecond + defaultSwitchInterval = 2 * time.Minute ) func defaultConfig() *cfg { @@ -60,6 +63,7 @@ func defaultConfig() *cfg { signer: &transaction.Signer{ Scopes: transaction.Global, }, + switchInterval: defaultSwitchInterval, } } @@ -289,3 +293,12 @@ func WithConnLostCallback(cb Callback) Option { c.inactiveModeCb = cb } } + +// WithSwitchInterval returns a client constructor option +// that specifies a wait interval b/w attempts to reconnect +// to an RPC node with the highest priority. +func WithSwitchInterval(i time.Duration) Option { + return func(c *cfg) { + c.switchInterval = i + } +} diff --git a/pkg/morph/client/multi.go b/pkg/morph/client/multi.go index 711c73f97..8d9e0bd5b 100644 --- a/pkg/morph/client/multi.go +++ b/pkg/morph/client/multi.go @@ -133,9 +133,7 @@ func (c *Client) notificationLoop() { } func (c *Client) switchToMostPrioritized() { - const period = 2 * time.Minute - - t := time.NewTicker(period) + t := time.NewTicker(c.cfg.switchInterval) defer t.Stop() defer c.switchIsActive.Store(false)