Skip to content

Commit

Permalink
fix/header-timeout: adding ReadTimeout values
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhuie19 committed Jun 5, 2023
1 parent 29bb12f commit 5685628
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
8 changes: 5 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func (s *Server) Serve(lis net.Listener) {
})

hcsrv := &http.Server{
Handler: hchandler,
Handler: hchandler,
ReadTimeout: s.opts.healthcheckTimeout,
}

//nolint:errcheck
Expand All @@ -101,8 +102,9 @@ func (s *Server) Serve(lis net.Listener) {
wshandler := http.NewServeMux()
wshandler.HandleFunc("/", s.wshandler)
wssrv := &http.Server{
TLSConfig: s.opts.creds.Config,
Handler: wshandler,
TLSConfig: s.opts.creds.Config,
Handler: wshandler,
ReadTimeout: s.opts.wsTimeout,
}

//nolint:errcheck
Expand Down
56 changes: 56 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ed25519"
"net"
"net/http"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -62,3 +63,58 @@ func Test_Healthcheck(t *testing.T) {
}, 1*time.Second, 100*time.Millisecond)

}

func Test_Server_HTTPTimeout_Defaults(t *testing.T) {
// Start the server
privKey := keys.FromHex("c1afd224cec2ff6066746bf9b7cdf7f9f4694ab7ef2ca1692ff923a30df203483b0f149627adb7b6fafe1497a9dfc357f22295a5440786c3bc566dfdb0176808")
pubKeys := []ed25519.PublicKey{}


defaultServer := NewServer(
Creds(privKey, pubKeys),
WithHealthcheck("127.0.0.1:1337"),
)

assert.Equal(t, 5 * time.Second, defaultServer.opts.healthcheckTimeout)
assert.Equal(t, 10 * time.Second, defaultServer.opts.wsTimeout)

expectedTimeout := 1*time.Nanosecond
timeoutServer := NewServer(
Creds(privKey, pubKeys),
WithHealthcheck("127.0.0.1:1337"),
WithHTTPReadTimeout(expectedTimeout, expectedTimeout*2),
)
assert.Equal(t, expectedTimeout, timeoutServer.opts.healthcheckTimeout)
assert.Equal(t, expectedTimeout*2, timeoutServer.opts.wsTimeout)
}

func Test_Server_HTTPTimeout(t *testing.T) {
// Start the server
privKey := keys.FromHex("c1afd224cec2ff6066746bf9b7cdf7f9f4694ab7ef2ca1692ff923a30df203483b0f149627adb7b6fafe1497a9dfc357f22295a5440786c3bc566dfdb0176808")
pubKeys := []ed25519.PublicKey{}

lis, err := net.Listen("tcp", "127.0.0.1:1338")
require.NoError(t, err)

expectedTimeout := 1*time.Nanosecond
s := NewServer(
Creds(privKey, pubKeys),
WithHealthcheck("127.0.0.1:1337"),
WithHTTPReadTimeout(expectedTimeout, expectedTimeout*2),
)

// Start serving
go s.Serve(lis)
defer s.Stop()

// Test until the server boots
assert.Eventually(t, func() bool {
// Run a http call
_, err := http.Get("http://127.0.0.1:1337/healthz")
if err != nil {
return strings.Contains(err.Error(), "EOF") // Check if the error contains "timeout"
}

return false
}, 1*time.Second, 100*time.Millisecond)
}
17 changes: 17 additions & 0 deletions serveroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wsrpc

import (
"crypto/ed25519"
"time"

"github.com/smartcontractkit/wsrpc/credentials"
)
Expand All @@ -21,6 +22,12 @@ type serverOptions struct {

// The address that the healthcheck will run on
healthcheckAddr string

// The HTTP ReadTimeout the healthcheck will use. Set to 0 for no timeout
healthcheckTimeout time.Duration

// The HTTP ReadTimeout the ws server will use. Set to 0 for no timeout
wsTimeout time.Duration
}

// funcServerOption wraps a function that modifies serverOptions into an
Expand All @@ -39,6 +46,14 @@ func (fdo *funcServerOption) apply(do *serverOptions) {
fdo.f(do)
}

// returns a ServerOption that sets the healthcheck HTTP read timeout and the server HTTP read timeout
func WithHTTPReadTimeout(hctime time.Duration, wstime time.Duration) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
o.healthcheckTimeout = hctime
o.wsTimeout = wstime
})
}

// Creds returns a ServerOption that sets credentials for server connections.
func Creds(privKey ed25519.PrivateKey, pubKeys []ed25519.PublicKey) ServerOption {
return newFuncServerOption(func(o *serverOptions) {
Expand Down Expand Up @@ -72,6 +87,8 @@ func ReadBufferSize(s int) ServerOption {
var defaultServerOptions = serverOptions{
writeBufferSize: 4096,
readBufferSize: 4096,
healthcheckTimeout: 5 * time.Second,
wsTimeout: 10 * time.Second,
}

// WithHealthcheck specifies whether to run a healthcheck endpoint. If a url
Expand Down

0 comments on commit 5685628

Please sign in to comment.