-
Notifications
You must be signed in to change notification settings - Fork 189
Description
I think it would be a great addition to the library if we could implement an auto reconnecting feature for the Client.go
Right now the Client will actually keep trying to connect with the amount of attempts decided by the backoff.Retry() method, but only IF the Client has been started with an unreachable stream (server) from start and then it will stop when it has established a connection with the stream
But the Client will not try again in the same way if it loses the connection from a stream it already was connected to, because the readLoop() method will detect this condition err == io.EOF and return nill to the erChan and thereby killing of the backoff.Retry()
An very simple idea to a solution for this would be something like this:
var AutoReconnect = false //[NEW] default should be false
func (c *Client) readLoop(reader *EventStreamReader, outCh chan *Event, erChan chan error) {
for {
// Read each new line and process the type of event
event, err := reader.ReadEvent()
if err != nil {
if err == io.EOF {
if !autoReconnect { //[NEW] Check if user has enabled autoReconnect
erChan <- nil
return
}
}
// run user specified disconnect function
if c.disconnectcb != nil {
c.disconnectcb(c)
}
erChan <- err
return
}
// If we get an error, ignore it.
if msg, err := c.processEvent(event); err == nil {
if len(msg.ID) > 0 {
c.EventID = string(msg.ID)
} else {
msg.ID = []byte(c.EventID)
}
// Send downstream
outCh <- msg
}
}
}
This works and I have tried it on a Live-server.