[#1170] pkg/morph: Add inactive mode callback

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-03-31 14:17:01 +03:00 committed by Alex Vanin
parent 424cc6d495
commit 2427593da6
2 changed files with 25 additions and 3 deletions

View file

@ -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()
}
}

View file

@ -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
}
}