Skip to content

cmd/tap{,c}: add websocket.Client iterator#1319

Draft
davecheney wants to merge 1 commit intomainfrom
dfc/websocket-iterator
Draft

cmd/tap{,c}: add websocket.Client iterator#1319
davecheney wants to merge 1 commit intomainfrom
dfc/websocket-iterator

Conversation

@davecheney
Copy link
Contributor

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 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.
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 this pull request may close these issues.

1 participant