Skip to content

Commit a874af9

Browse files
committed
sleep between unsuccessful proxy attempts
1 parent 8df6461 commit a874af9

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

connect.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ func (t *Tunnel) keepConnected(ctx context.Context) {
5959
return
6060
default:
6161
t.gwConn, err = connect(t.address, t.serverName, t.token, t.config)
62+
if err == nil {
63+
err = proxy(ctx, t.gwConn)
64+
_ = t.gwConn.Close()
65+
}
6266
if err != nil {
63-
d := b.Duration()
6467
klog.Errorln(err)
65-
klog.Errorf("reconnecting to %s in %.0fs", t.address, d.Seconds())
68+
d := b.Duration()
69+
klog.Infof("reconnecting to %s in %.0fs", t.address, d.Seconds())
6670
time.Sleep(d)
6771
continue
6872
}
6973
b.Reset()
70-
proxy(ctx, t.gwConn)
71-
_ = t.gwConn.Close()
7274
}
7375
}
7476
}
@@ -220,25 +222,23 @@ func connect(gwAddr, serverName, token string, config []byte) (net.Conn, error)
220222
return gwConn, nil
221223
}
222224

223-
func proxy(ctx context.Context, gwConn net.Conn) {
225+
func proxy(ctx context.Context, gwConn net.Conn) error {
224226
cfg := yamux.DefaultConfig()
225227
cfg.KeepAliveInterval = time.Second
226228
cfg.LogOutput = io.Discard
227229
session, err := yamux.Server(gwConn, cfg)
228230
if err != nil {
229-
klog.Errorln("failed to start a TCP multiplexing server:", err)
230-
return
231+
return fmt.Errorf("failed to start a TCP multiplexing server: %s", err)
231232
}
232233
defer session.Close()
233234
for {
234235
select {
235236
case <-ctx.Done():
236-
return
237+
return nil
237238
default:
238239
gwStream, err := session.Accept()
239240
if err != nil {
240-
klog.Errorf("failed to accept a stream: %s", err)
241-
return
241+
return fmt.Errorf("failed to accept a stream: %s", err)
242242
}
243243
go func(c net.Conn) {
244244
defer c.Close()

connect_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212
"io"
13-
"io/ioutil"
1413
"net"
1514
"net/http"
1615
"net/http/httptest"
@@ -83,7 +82,7 @@ func TestProxy(t *testing.T) {
8382

8483
gwConn, err := connect(addr, "", token, []byte("config_data"))
8584
go func() {
86-
proxy(context.Background(), gwConn)
85+
require.NoError(t, proxy(context.Background(), gwConn))
8786
}()
8887

8988
session := <-sessionChan
@@ -107,7 +106,7 @@ func TestProxy(t *testing.T) {
107106

108107
res, err := client.Get("http://any/-/healthy")
109108
require.NoError(t, err)
110-
data, err := ioutil.ReadAll(res.Body)
109+
data, err := io.ReadAll(res.Body)
111110
require.NoError(t, err)
112111
res.Body.Close()
113112
assert.Equal(t, "Prometheus is Healthy.", string(data))
@@ -131,7 +130,7 @@ func TestProxy(t *testing.T) {
131130

132131
res, err = client.Get("http://any/-/healthy")
133132
require.NoError(t, err)
134-
data, err = ioutil.ReadAll(res.Body)
133+
data, err = io.ReadAll(res.Body)
135134
require.NoError(t, err)
136135
res.Body.Close()
137136
assert.Equal(t, "Pyroscope is Healthy.", string(data))

0 commit comments

Comments
 (0)