implement Config.HandleError
This commit is contained in:
parent
82f34d9777
commit
c609a55864
17
client.go
17
client.go
@ -99,6 +99,9 @@ type Config struct {
|
|||||||
// ReconnectDelay is the a duration of time to delay before attempting a
|
// ReconnectDelay is the a duration of time to delay before attempting a
|
||||||
// reconnection. Defaults to 10s (minimum of 10s).
|
// reconnection. Defaults to 10s (minimum of 10s).
|
||||||
ReconnectDelay time.Duration
|
ReconnectDelay time.Duration
|
||||||
|
// HandleError if supplied, is called when one is disconnected from the
|
||||||
|
// server, with a given error.
|
||||||
|
HandleError func(error)
|
||||||
|
|
||||||
// disableTracking disables all channel and user-level tracking. Useful
|
// disableTracking disables all channel and user-level tracking. Useful
|
||||||
// for highly embedded scripts with single purposes.
|
// for highly embedded scripts with single purposes.
|
||||||
@ -122,6 +125,8 @@ var ErrNotConnected = errors.New("client is not connected to server")
|
|||||||
// ErrAlreadyConnecting implies that a connection attempt is already happening.
|
// ErrAlreadyConnecting implies that a connection attempt is already happening.
|
||||||
var ErrAlreadyConnecting = errors.New("a connection attempt is already occurring")
|
var ErrAlreadyConnecting = errors.New("a connection attempt is already occurring")
|
||||||
|
|
||||||
|
var ErrDisconnected = errors.New("unexpectedly disconnected")
|
||||||
|
|
||||||
// ErrCalledAfterStop is used when one uses Client.Stop(), and subsequently
|
// ErrCalledAfterStop is used when one uses Client.Stop(), and subsequently
|
||||||
// attempts to use the client again.
|
// attempts to use the client again.
|
||||||
var ErrCalledAfterStop = errors.New("attempted use after stop has been called")
|
var ErrCalledAfterStop = errors.New("attempted use after stop has been called")
|
||||||
@ -413,7 +418,7 @@ func (c *Client) reconnect(remoteInvoked bool) (err error) {
|
|||||||
c.Quit()
|
c.Quit()
|
||||||
|
|
||||||
if c.Config.Retries < 1 && !remoteInvoked {
|
if c.Config.Retries < 1 && !remoteInvoked {
|
||||||
return errors.New("unexpectedly disconnected")
|
return ErrDisconnected
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delay so we're not slaughtering the server with a bunch of
|
// Delay so we're not slaughtering the server with a bunch of
|
||||||
@ -455,8 +460,14 @@ func (c *Client) readLoop(ctx context.Context) {
|
|||||||
c.state.conn.SetDeadline(time.Now().Add(300 * time.Second))
|
c.state.conn.SetDeadline(time.Now().Add(300 * time.Second))
|
||||||
event, err = c.state.reader.Decode()
|
event, err = c.state.reader.Decode()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// And attempt a reconnect (if applicable).
|
// Attempt a reconnect (if applicable). If it fails, send
|
||||||
c.reconnect(false)
|
// the error to c.Config.HandleError to be dealt with, if
|
||||||
|
// the handler exists.
|
||||||
|
err = c.reconnect(false)
|
||||||
|
if err != nil && c.Config.HandleError != nil {
|
||||||
|
c.Config.HandleError(err)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user