Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit c5f3e78

Browse files
committed
fix: Retry RTC connection attempts multiple times
1 parent 99b3008 commit c5f3e78

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

wsnet/listen.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"cdr.dev/coder-cli/coder-sdk"
1919
)
2020

21-
var keepAliveInterval = 5 * time.Second
21+
var connectionRetryInterval = time.Second
2222

2323
// Listen connects to the broker proxies connections to the local net.
2424
// Close will end all RTC connections.
@@ -41,8 +41,19 @@ func Listen(ctx context.Context, broker string) (io.Closer, error) {
4141
// If we hit an EOF, then the connection to the broker
4242
// was interrupted. We'll take a short break then dial
4343
// again.
44-
time.Sleep(time.Second)
45-
ch, err = l.dial(ctx)
44+
ticker := time.NewTicker(connectionRetryInterval)
45+
for {
46+
select {
47+
case <-ticker.C:
48+
ch, err = l.dial(ctx)
49+
case <-ctx.Done():
50+
err = ctx.Err()
51+
}
52+
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
53+
break
54+
}
55+
}
56+
ticker.Stop()
4657
}
4758
if err != nil {
4859
l.acceptError = err
@@ -79,7 +90,6 @@ func (l *listener) dial(ctx context.Context) (<-chan error, error) {
7990
l.ws = conn
8091
nconn := websocket.NetConn(ctx, conn, websocket.MessageBinary)
8192
config := yamux.DefaultConfig()
82-
config.KeepAliveInterval = keepAliveInterval
8393
config.LogOutput = io.Discard
8494
session, err := yamux.Server(nconn, config)
8595
if err != nil {

wsnet/listen_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ import (
1313

1414
func TestListen(t *testing.T) {
1515
t.Run("Reconnect", func(t *testing.T) {
16-
keepAliveInterval = 50 * time.Millisecond
16+
connectionRetryInterval = 10 * time.Millisecond
1717

1818
var (
19-
connCh = make(chan interface{})
19+
connCh = make(chan *websocket.Conn)
2020
mux = http.NewServeMux()
2121
srv = http.Server{
2222
Handler: mux,
2323
}
2424
)
2525
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
26-
_, err := websocket.Accept(w, r, nil)
26+
ws, err := websocket.Accept(w, r, nil)
2727
if err != nil {
2828
t.Error(err)
2929
return
3030
}
31-
connCh <- struct{}{}
31+
connCh <- ws
3232
})
3333

3434
listener, err := net.Listen("tcp4", "127.0.0.1:0")
@@ -47,8 +47,15 @@ func TestListen(t *testing.T) {
4747
t.Error(err)
4848
return
4949
}
50-
<-connCh
50+
conn := <-connCh
5151
_ = listener.Close()
52+
// We need to close the connection too... closing a TCP
53+
// listener does not close active local connections.
54+
_ = conn.Close(websocket.StatusGoingAway, "")
55+
56+
// At least a few retry attempts should be had...
57+
time.Sleep(connectionRetryInterval * 5)
58+
5259
listener, err = net.Listen("tcp4", addr.String())
5360
if err != nil {
5461
t.Error(err)

0 commit comments

Comments
 (0)