Skip to content

Commit

Permalink
Merge pull request #3 from primalmotion/keepalives
Browse files Browse the repository at this point in the history
new: optimize tcp power usage for websockets
  • Loading branch information
primalmotion authored May 17, 2023
2 parents a348447 + 4b48f79 commit 224b106
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
15 changes: 15 additions & 0 deletions client/dialer_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build linux

package client

import (
"context"
"net"
)

func netDialContextFunc(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
KeepAlive: -1,
}
return dialer.DialContext(ctx, network, addr)
}
15 changes: 15 additions & 0 deletions client/dialer_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !linux

package client

import (
"context"
"net"
)

func netDialContextFunc(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := &net.Dialer{
KeepAlive: -1,
}
return dialer.DialContext(ctx, network, addr)
}
7 changes: 6 additions & 1 deletion client/subscribe_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ func SubscribeWS(ctx context.Context, url string, tlsConfig *tls.Config) (chan [
conn, resp, err := wsc.Connect(
ctx,
strings.Replace(url+"/subscribe/ws", "https", "wss", 1),
wsc.Config{TLSConfig: tlsConfig},
wsc.Config{
TLSConfig: tlsConfig,
NetDialContextFunc: netDialContextFunc, // this function is platform dependent.
PingPeriod: 15 * time.Minute,
PongWait: 20 * time.Minute,
},
)
if err != nil {
log.Printf("unable to connect to ws (retrying): %s", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
go.aporeto.io/tg v1.50.0
go.aporeto.io/wsc v1.51.0
go.aporeto.io/wsc v1.52.0
golang.design/x/clipboard v0.7.0
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ go.aporeto.io/tg v1.50.0 h1:WOtzxPJpPkIx3+OBmUuZIclTZgvTrIIEg7uzstWucTk=
go.aporeto.io/tg v1.50.0/go.mod h1:LkCXFWQN6LGrp4alU+qzNXoq/nkouQKsOzsjidoKbBc=
go.aporeto.io/wsc v1.51.0 h1:xbbsfRGtUWAqTiT9ubK6TKi2+nABNf656XxnEweynRM=
go.aporeto.io/wsc v1.51.0/go.mod h1:0gL/S3uPkS4f3q7/lXF5Hlt804T0d9EhgOSYJkHUbVo=
go.aporeto.io/wsc v1.51.1-0.20230516221449-e7ab6eb42d60 h1:amX4r9ehOpj1gQezNqoQINW/23jci5xQVwjS/hCMC4Q=
go.aporeto.io/wsc v1.51.1-0.20230516221449-e7ab6eb42d60/go.mod h1:0gL/S3uPkS4f3q7/lXF5Hlt804T0d9EhgOSYJkHUbVo=
go.aporeto.io/wsc v1.52.0 h1:iy2FR+P8rG7lG/c4pWXZJSDaj4jesgM4KfcbGQZB0kc=
go.aporeto.io/wsc v1.52.0/go.mod h1:0gL/S3uPkS4f3q7/lXF5Hlt804T0d9EhgOSYJkHUbVo=
go.etcd.io/etcd/api/v3 v3.5.6/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
go.etcd.io/etcd/client/pkg/v3 v3.5.6/go.mod h1:ggrwbk069qxpKPq8/FKkQ3Xq9y39kbFR4LnKszpRXeQ=
go.etcd.io/etcd/client/v2 v2.305.6/go.mod h1:BHha8XJGe8vCIBfWBpbBLVZ4QjOIlfoouvOwydu63E0=
Expand Down
6 changes: 5 additions & 1 deletion server/handle_subscribe_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"net/http"
"time"

"github.com/gorilla/websocket"
"go.aporeto.io/wsc"
Expand All @@ -26,7 +27,10 @@ func makeSubscribeWSHandler(dispatch *dispatcher) func(http.ResponseWriter, *htt
return
}

conn, err := wsc.Accept(r.Context(), ws, wsc.Config{})
conn, err := wsc.Accept(r.Context(), ws, wsc.Config{
PingPeriod: 15 * time.Minute,
PongWait: 20 * time.Minute,
})
if err != nil {
http.Error(
w,
Expand Down

0 comments on commit 224b106

Please sign in to comment.