Skip to content

Commit

Permalink
Use healthcheck for outgoing websocket connections
Browse files Browse the repository at this point in the history
Signed-off-by: Vasyl Gello <[email protected]>
  • Loading branch information
basilgello committed Jul 22, 2024
1 parent 6c02e30 commit 0b05f88
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/core/link_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ type wsServer struct {
ctx context.Context
}

func (c *linkWSConn) CheckHealth(healthUrl string) {
for {
if c != nil {
resp, err := http.Get(healthUrl)
if err == nil {
resp.Body.Close()
}
time.Sleep(30 * time.Second)
} else {
return
}
}
}

func (l *linkWSListener) Accept() (net.Conn, error) {
qs := <-l.ch
if qs == nil {
Expand Down Expand Up @@ -94,10 +108,15 @@ func (l *linkWS) dial(ctx context.Context, url *url.URL, info linkInfo, options
if err != nil {
return nil, err
}
healthUrl := *url
healthUrl.Scheme = "http"
healthUrlStr := healthUrl.JoinPath("/h").String()
netconn := websocket.NetConn(ctx, wsconn, websocket.MessageBinary)
return &linkWSConn{
conn := &linkWSConn{
Conn: netconn,
}, nil
}
go conn.CheckHealth(healthUrlStr)
return conn, nil
}

func (l *linkWS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) {
Expand Down
25 changes: 23 additions & 2 deletions src/core/link_wss.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"time"

"github.com/Arceliar/phony"
"nhooyr.io/websocket"
Expand All @@ -19,6 +21,20 @@ type linkWSSConn struct {
net.Conn
}

func (c *linkWSSConn) CheckHealth(healthUrl string) {
for {
if c != nil {
resp, err := http.Get(healthUrl)
if err == nil {
resp.Body.Close()
}
time.Sleep(30 * time.Second)
} else {
return
}
}
}

func (l *links) newLinkWSS() *linkWSS {
lwss := &linkWSS{
links: l,
Expand All @@ -34,10 +50,15 @@ func (l *linkWSS) dial(ctx context.Context, url *url.URL, info linkInfo, options
if err != nil {
return nil, err
}
healthUrl := *url
healthUrl.Scheme = "https"
healthUrlStr := healthUrl.JoinPath("/h").String()
netconn := websocket.NetConn(ctx, wsconn, websocket.MessageBinary)
return &linkWSSConn{
conn := &linkWSSConn{
Conn: netconn,
}, nil
}
go conn.CheckHealth(healthUrlStr)
return conn, nil
}

func (l *linkWSS) listen(ctx context.Context, url *url.URL, _ string) (net.Listener, error) {
Expand Down

0 comments on commit 0b05f88

Please sign in to comment.