Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: optimize tcp power usage for websockets #3

Merged
merged 2 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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