diff --git a/pkg/morph/client/client.go b/pkg/morph/client/client.go index ee98aa53c..01620ffb6 100644 --- a/pkg/morph/client/client.go +++ b/pkg/morph/client/client.go @@ -543,10 +543,16 @@ func (c *Client) NotificationChannel() <-chan client.Notification { // inactiveMode switches Client to an inactive mode: // - notification channel is closed; -// - all the new RPC request would return ErrConnectionLost. -// -// Note: must be called only with held write switchLock. +// - all the new RPC request would return ErrConnectionLost; +// - inactiveModeCb is called if not nil. func (c *Client) inactiveMode() { + c.switchLock.Lock() + defer c.switchLock.Unlock() + close(c.notifications) c.inactive = true + + if c.cfg.inactiveModeCb != nil { + c.cfg.inactiveModeCb() + } } diff --git a/pkg/morph/client/constructor.go b/pkg/morph/client/constructor.go index cddfebe2c..5b11f0c26 100644 --- a/pkg/morph/client/constructor.go +++ b/pkg/morph/client/constructor.go @@ -19,6 +19,10 @@ import ( // Option is a client configuration change function. type Option func(*cfg) +// Callback is a function that is going to be called +// on certain Client's state. +type Callback func() + // groups the configurations with default values. type cfg struct { ctx context.Context // neo-go client context @@ -34,6 +38,8 @@ type cfg struct { extraEndpoints []string singleCli *client.WSClient // neo-go client for single client mode + + inactiveModeCb Callback } const ( @@ -217,3 +223,13 @@ func WithSingleClient(cli *client.WSClient) Option { c.singleCli = cli } } + +// WithConnLostCallback return a client constructor option +// that specifies a callback that is called when Client +// unsuccessfully tried to connect to all the specified +// endpoints. +func WithConnLostCallback(cb Callback) Option { + return func(c *cfg) { + c.inactiveModeCb = cb + } +}