Skip to content

Commit

Permalink
Fix netx.Conn metrics and reduce ndt7 websocket buffer (#299)
Browse files Browse the repository at this point in the history
* Guarantee conn count is decremented once on close
* Define smaller websocket buffer size
* Be explicit in comment about buffer size
  • Loading branch information
stephen-soltesz committed Jun 16, 2020
1 parent ce43f7a commit 5daf100
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ndt7/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions ndt7/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion netx/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net"
"os"
"sync"
"time"

guuid "github.com/google/uuid"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 5daf100

Please sign in to comment.