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..4c41d7aa 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. We use 1MB as a balance. +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 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() }