From f18bd34c7f04e6da1c6f5ab2fc580f029bd8df11 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Mon, 15 Jun 2020 13:39:54 -0400 Subject: [PATCH 1/3] Guarantee conn count is decremented once on close --- netx/net.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/netx/net.go b/netx/net.go index b107c919..19ffa7e5 100644 --- a/netx/net.go +++ b/netx/net.go @@ -5,6 +5,7 @@ import ( "log" "net" "os" + "sync" "time" guuid "github.com/google/uuid" @@ -51,6 +52,7 @@ type Conn struct { net.Conn fp *os.File netinfo iface.NetInfo + once sync.Once } // Addr supports the net.Addr interface and allows mediated access to operations @@ -110,7 +112,7 @@ func (ln *Listener) Accept() (net.Conn, error) { // returned by LocalAddr and RemoteAddr should be released before calling Close. func (mc *Conn) Close() error { mc.fp.Close() - CurrentOpenConns.Dec() + mc.once.Do(CurrentOpenConns.Dec) return mc.Conn.Close() } From 48d6ea165a9c035138c2b5b552425e582891d439 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Mon, 15 Jun 2020 16:32:52 -0400 Subject: [PATCH 2/3] Define smaller websocket buffer size --- ndt7/handler/handler.go | 4 ++-- ndt7/spec/spec.go | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/ndt7/handler/handler.go b/ndt7/handler/handler.go index 86972e00..29166fda 100644 --- a/ndt7/handler/handler.go +++ b/ndt7/handler/handler.go @@ -126,8 +126,8 @@ func setupConn(writer http.ResponseWriter, request *http.Request) *websocket.Con CheckOrigin: func(r *http.Request) bool { return true // Allow cross origin resource sharing }, - ReadBufferSize: spec.MaxMessageSize, - WriteBufferSize: spec.MaxMessageSize, + ReadBufferSize: spec.DefaultWebsocketBufferSize, + WriteBufferSize: spec.DefaultWebsocketBufferSize, } conn, err := upgrader.Upgrade(writer, request, headers) if err != nil { diff --git a/ndt7/spec/spec.go b/ndt7/spec/spec.go index 6d47d812..f12533a9 100644 --- a/ndt7/spec/spec.go +++ b/ndt7/spec/spec.go @@ -22,6 +22,13 @@ const MaxMessageSize = 1 << 24 // a good compromise between Go and JavaScript as seen in cloud based tests. const MaxScaledMessageSize = 1 << 20 +// DefaultWebsocketBufferSize is the read and write buffer sizes used when +// creating a websocket connection. This size is independent of the websocket +// message sizes defined above (which may be larger) and used to optimize read +// and write operations. However, larger buffers will practically limit the +// total number of concurrent connections possible. +const DefaultWebsocketBufferSize = 1 << 20 + // ScalingFraction sets the threshold for scaling binary messages. When // the current binary message size is <= than 1/scalingFactor of the // amount of bytes sent so far, we scale the message. This is documented From fb7f4231cb020f643189ecfa1898f6bb42d48da5 Mon Sep 17 00:00:00 2001 From: Stephen Soltesz Date: Tue, 16 Jun 2020 11:28:09 -0400 Subject: [PATCH 3/3] Be explicit in comment about buffer size --- ndt7/spec/spec.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndt7/spec/spec.go b/ndt7/spec/spec.go index f12533a9..4c41d7aa 100644 --- a/ndt7/spec/spec.go +++ b/ndt7/spec/spec.go @@ -26,7 +26,7 @@ const MaxScaledMessageSize = 1 << 20 // creating a websocket connection. This size is independent of the websocket // message sizes defined above (which may be larger) and used to optimize read // and write operations. However, larger buffers will practically limit the -// total number of concurrent connections possible. +// total number of concurrent connections possible. We use 1MB as a balance. const DefaultWebsocketBufferSize = 1 << 20 // ScalingFraction sets the threshold for scaling binary messages. When