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

Feature Request: Let's implement a client.AutoReconnect() method on lost connections. #75

Open
Muddz opened this issue Mar 13, 2020 · 5 comments · May be fixed by #148
Open

Feature Request: Let's implement a client.AutoReconnect() method on lost connections. #75

Muddz opened this issue Mar 13, 2020 · 5 comments · May be fixed by #148

Comments

@Muddz
Copy link

Muddz commented Mar 13, 2020

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.

@quarckster
Copy link

I faced with that issue as well.

@Muddz
Copy link
Author

Muddz commented May 3, 2020

@quarckster I solved this by implementing the suggested code above and it works perfectly

@koolay
Copy link

koolay commented Oct 14, 2022

+1

@yi-ge
Copy link

yi-ge commented Jan 21, 2023

code: #145

@pjcdawkins
Copy link

Interesting, I just noticed this after writing #147 and #148

I don't understand why the EOF special case is there by default (especially as the backoff will end silently without any error).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants