Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback prior to reconnection attempt #283

Merged
merged 4 commits into from Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions client.go
Expand Up @@ -345,6 +345,9 @@ func (c *client) reconnect() {
)

for rc != 0 && atomic.LoadUint32(&c.status) != disconnected {
if nil != c.options.OnReconnecting {
c.options.OnReconnecting(c, &c.options)
}
for _, broker := range c.options.Servers {
cm := newConnectMsgFromOptions(&c.options, broker)
DEBUG.Println(CLI, "about to write new connect msg")
Expand Down
12 changes: 12 additions & 0 deletions options.go
Expand Up @@ -46,6 +46,10 @@ type ConnectionLostHandler func(Client, error)
// at initial connection and on reconnection
type OnConnectHandler func(Client)

// ReconnectHandler is invoked prior to reconnecting after
// the initial connection is lost
type ReconnectHandler func(Client, *ClientOptions)

// ClientOptions contains configurable options for an Client.
type ClientOptions struct {
Servers []*url.URL
Expand All @@ -72,6 +76,7 @@ type ClientOptions struct {
DefaultPublishHandler MessageHandler
OnConnect OnConnectHandler
OnConnectionLost ConnectionLostHandler
OnReconnecting ReconnectHandler
WriteTimeout time.Duration
MessageChannelDepth uint
ResumeSubs bool
Expand Down Expand Up @@ -297,6 +302,13 @@ func (o *ClientOptions) SetConnectionLostHandler(onLost ConnectionLostHandler) *
return o
}

// SetReconnectingHandler sets the OnReconnecting callback to be executed prior
// to the client attempting a reconnect to the MQTT broker.
func (o *ClientOptions) SetReconnectingHandler(cb ReconnectHandler) *ClientOptions {
o.OnReconnecting = cb
return o
}

// SetWriteTimeout puts a limit on how long a mqtt publish should block until it unblocks with a
// timeout error. A duration of 0 never times out. Default 30 seconds
func (o *ClientOptions) SetWriteTimeout(t time.Duration) *ClientOptions {
Expand Down