Draft
Conversation
This is an experimental branch to demonstrate the websocket.Client wrapper. The client iterator is modeled on the bufio.Scanner type, and numerous other iterator implementations. It is modeled on the idea that the client presents a constant iterator over the target url, if there is a disconnection, that is handled internally, along with reconnection and (at least on paper, ping/pong support -- more on that below). I forked event.HandleRepoEvent into HandleWebsocketStream and rewrote the latter, and its caller in cmd/tap/firehose.go, to use websocket.Client. The result is all of the connection state management, and retry logic that permiated the websocket consumers has been pushed down into the Client itself. With that said there are two big open questions before this idea could be moved forward with. 1. ping/pong support. The main logic is, wrap a 1 minute deadline around the underlying websocket. Ping the other side every 30 seconds, when they pong back you move the deadline forward another minute. If no pong, then the underlying websocket is closed and you fall back into the retry logic. Overall, the way that the pinger races with normal traffic on the socket feels wrong. I'm not sure that a net.Conn is thread safe, and i'm less sure that a wrapper around a net.Conn, which a web socket over a http connection is, is thread safe. More fundamentally, blindly pinging without reference to other traffic on the socket feels incorrect. The logic should be, wait in read for, say, 30 seconds, if nothing then send a ping, go back to reading, then either timeout or wakeup on the pong. I think that more fundamental surgery inside the gorrila/ws package might be needed to expose this concept. 2. What is, and is not, a reconnectable error is a bit yolo'd at the moment. context.Cancelled is use as the signal to stop the client, but care is needed to make sure that this isn't mistaken for other network errors. I have a foggy memory that net.Error from the stdlib does some magic to map its internal, unexpored, errors to satisfy errors.Is(..., context.Cancelled), and in this specific case, this is going to be exactly what we don't want. Lastly, this commuit includes a tiny cmd/tapc, which is a replacement for websocat, with the bonus that it reconnects when you bounce your tap instance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is an experimental branch to demonstrate the websocket.Client wrapper. The client iterator is modeled on the bufio.Scanner type, and numerous other iterator implementations. It is modeled on the idea that the client presents a constant iterator over the target url, if there is a disconnection, that is handled internally, along with reconnection and (at least on paper, ping/pong support -- more on that below).
I forked event.HandleRepoEvent into HandleWebsocketStream and rewrote the latter, and its caller in cmd/tap/firehose.go, to use websocket.Client. The result is all of the connection state management, and retry logic that permiated the websocket consumers has been pushed down into the Client itself.
With that said there are two big open questions before this idea could be moved forward with.
ping/pong support. The main logic is, wrap a 1 minute deadline around the underlying websocket. Ping the other side every 30 seconds, when they pong back you move the deadline forward another minute. If no pong, then the underlying websocket is closed and you fall back into the retry logic.
Overall, the way that the pinger races with normal traffic on the socket feels wrong. I'm not sure that a net.Conn is thread safe, and i'm less sure that a wrapper around a net.Conn, which a web socket over a http connection is, is thread safe.
More fundamentally, blindly pinging without reference to other traffic on the socket feels incorrect. The logic should be, wait in read for, say, 30 seconds, if nothing then send a ping, go back to reading, then either timeout or wakeup on the pong. I think that more fundamental surgery inside the gorrila/ws package might be needed to expose this concept.
What is, and is not, a reconnectable error is a bit yolo'd at the moment. context.Cancelled is use as the signal to stop the client, but care is needed to make sure that this isn't mistaken for other network errors. I have a foggy memory that net.Error from the stdlib does some magic to map its internal, unexpored, errors to satisfy errors.Is(..., context.Cancelled), and in this specific case, this is going to be exactly what we don't want.
Lastly, this commuit includes a tiny cmd/tapc, which is a replacement for websocat, with the bonus that it reconnects when you bounce your tap instance.