Skip to content

Commit

Permalink
sleep between unsuccessful proxy attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
apetruhin committed Sep 11, 2023
1 parent 8df6461 commit a874af9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
20 changes: 10 additions & 10 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ func (t *Tunnel) keepConnected(ctx context.Context) {
return
default:
t.gwConn, err = connect(t.address, t.serverName, t.token, t.config)
if err == nil {
err = proxy(ctx, t.gwConn)
_ = t.gwConn.Close()
}
if err != nil {
d := b.Duration()
klog.Errorln(err)
klog.Errorf("reconnecting to %s in %.0fs", t.address, d.Seconds())
d := b.Duration()
klog.Infof("reconnecting to %s in %.0fs", t.address, d.Seconds())
time.Sleep(d)
continue
}
b.Reset()
proxy(ctx, t.gwConn)
_ = t.gwConn.Close()
}
}
}
Expand Down Expand Up @@ -220,25 +222,23 @@ func connect(gwAddr, serverName, token string, config []byte) (net.Conn, error)
return gwConn, nil
}

func proxy(ctx context.Context, gwConn net.Conn) {
func proxy(ctx context.Context, gwConn net.Conn) error {
cfg := yamux.DefaultConfig()
cfg.KeepAliveInterval = time.Second
cfg.LogOutput = io.Discard
session, err := yamux.Server(gwConn, cfg)
if err != nil {
klog.Errorln("failed to start a TCP multiplexing server:", err)
return
return fmt.Errorf("failed to start a TCP multiplexing server: %s", err)
}
defer session.Close()
for {
select {
case <-ctx.Done():
return
return nil
default:
gwStream, err := session.Accept()
if err != nil {
klog.Errorf("failed to accept a stream: %s", err)
return
return fmt.Errorf("failed to accept a stream: %s", err)
}
go func(c net.Conn) {
defer c.Close()
Expand Down
7 changes: 3 additions & 4 deletions connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -83,7 +82,7 @@ func TestProxy(t *testing.T) {

gwConn, err := connect(addr, "", token, []byte("config_data"))
go func() {
proxy(context.Background(), gwConn)
require.NoError(t, proxy(context.Background(), gwConn))
}()

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

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

res, err = client.Get("http://any/-/healthy")
require.NoError(t, err)
data, err = ioutil.ReadAll(res.Body)
data, err = io.ReadAll(res.Body)
require.NoError(t, err)
res.Body.Close()
assert.Equal(t, "Pyroscope is Healthy.", string(data))
Expand Down

0 comments on commit a874af9

Please sign in to comment.