Skip to content

Commit

Permalink
Merge pull request #283 from ozxcorp/master
Browse files Browse the repository at this point in the history
Add callback prior to reconnection attempt
  • Loading branch information
Al S-M committed Aug 29, 2019
2 parents 0dfc322 + 42cf089 commit 0cff61e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client.go
Expand Up @@ -375,6 +375,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 Down Expand Up @@ -74,6 +78,7 @@ type ClientOptions struct {
DefaultPublishHandler MessageHandler
OnConnect OnConnectHandler
OnConnectionLost ConnectionLostHandler
OnReconnecting ReconnectHandler
WriteTimeout time.Duration
MessageChannelDepth uint
ResumeSubs bool
Expand Down Expand Up @@ -300,6 +305,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

0 comments on commit 0cff61e

Please sign in to comment.