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

Introduce server config method(s) #76

Merged
merged 29 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 25 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
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
linters-settings:
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#exported
- name: exported
severity: warning
disabled: false
exclude: [""]
arguments:
- "disableStutteringCheck"
shan-96 marked this conversation as resolved.
Show resolved Hide resolved
exhaustive:
default-signifies-exhaustive: true
errcheck:
Expand Down
3 changes: 2 additions & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func main() {
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Addr, server.WithBaseContext(context.Background()), server.WithProfilerEndpoint())
server := server.NewServer(Addr, server.DefaultServerConfig, server.WithBaseContext(context.Background()), server.WithProfilerEndpoint())

server.AddChannel(channel)

if err := server.Run(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {

channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Addr, server.WithBaseContext(context.Background()))
server := server.NewServer(Addr, server.DefaultServerConfig, server.WithBaseContext(context.Background()))
server.AddChannel(channel)

if err := server.Run(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Addr, server.WithBaseContext(context.Background()))
server := server.NewServer(Addr, server.DefaultServerConfig, server.WithBaseContext(context.Background()))
server.AddChannel(channel)

if err := server.Run(); err != nil {
Expand Down
20 changes: 11 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ import (
"fmt"
"net"
"net/http"
"reflect"
"sync"
"time"

"github.com/ksysoev/wasabi"
"golang.org/x/exp/slog"
)

const (
ReadHeaderTimeout = 3 * time.Second
ReadTimeout = 30 * time.Second
)

var ErrServerAlreadyRunning = fmt.Errorf("server is already running")

type Server struct {
Expand Down Expand Up @@ -69,7 +64,7 @@ func WithReadinessChan(ch chan<- struct{}) Option {
// NewServer creates new instance of Wasabi server
// port - port to listen on
// returns new instance of Server
func NewServer(addr string, opts ...Option) *Server {
func NewServer(addr string, serverConfig ServerConfig, opts ...Option) *Server {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not introduce new mandatory arguments, I think in majority of cases these timeouts don't need to be redefined and reasonable default can be use without bothering the user. I'd suggest to pass it as optional parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added an optional method

if addr == "" {
addr = ":http"
}
Expand All @@ -88,16 +83,23 @@ func NewServer(addr string, opts ...Option) *Server {

server.handler = &http.Server{
Addr: addr,
ReadHeaderTimeout: ReadHeaderTimeout,
ReadTimeout: ReadTimeout,
ReadHeaderTimeout: serverConfig.ReadHeaderTimeout,
ReadTimeout: serverConfig.ReadTimeout,
shan-96 marked this conversation as resolved.
Show resolved Hide resolved
BaseContext: func(_ net.Listener) context.Context {
return server.baseCtx
},
}

server.ApplyServerConfig(serverConfig)

return server
}

// Applies the ServerConfig as a value in the baseCtx of the server
func (s *Server) ApplyServerConfig(serverConfig ServerConfig) {
s.baseCtx = context.WithValue(s.baseCtx, reflect.TypeOf(serverConfig), serverConfig)
shan-96 marked this conversation as resolved.
Show resolved Hide resolved
}

// AddChannel adds new channel to server
func (s *Server) AddChannel(channel wasabi.Channel) {
s.channels = append(s.channels, channel)
Expand Down
19 changes: 19 additions & 0 deletions server/server_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package server

import (
"time"
)

type ServerConfig struct {
ReadHeaderTimeout time.Duration
ReadTimeout time.Duration
}
shan-96 marked this conversation as resolved.
Show resolved Hide resolved

var DefaultServerConfig = ServerConfig{
ReadHeaderTimeout: ReadHeaderTimeoutSeconds * time.Second,
ReadTimeout: ReadTimeoutSeconds * time.Second,
}

const ReadHeaderTimeoutSeconds = 3

const ReadTimeoutSeconds = 30
28 changes: 17 additions & 11 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"net/http"
"os"
"reflect"
"testing"
"time"

Expand All @@ -19,7 +20,7 @@ type testCtxKey string

func TestNewServer(t *testing.T) {
addr := ":8080"
server := NewServer(addr)
server := NewServer(addr, DefaultServerConfig)

if server.addr != addr {
t.Errorf("Expected port %s, but got %s", addr, server.addr)
Expand All @@ -33,15 +34,15 @@ func TestNewServer(t *testing.T) {
t.Error("Expected non-nil mutex")
}

server = NewServer("")
server = NewServer("", DefaultServerConfig)

if server.addr != ":http" {
t.Errorf("Expected default port :http, but got %s", server.addr)
}
}
func TestServer_AddChannel(t *testing.T) {
// Create a new Server instance
server := NewServer(":0")
server := NewServer(":0", DefaultServerConfig)

// Create a new channel
channel := mocks.NewMockChannel(t)
Expand All @@ -64,7 +65,7 @@ func TestServer_WithBaseContext(t *testing.T) {
// Create a new Server instance with a base context
ctx := context.WithValue(context.Background(), testCtxKey("test"), "test")

server := NewServer(":0", WithBaseContext(ctx))
server := NewServer(":0", DefaultServerConfig, WithBaseContext(ctx))

// Check if the base context was set correctly
if server.baseCtx == nil {
Expand All @@ -74,12 +75,17 @@ func TestServer_WithBaseContext(t *testing.T) {
if server.baseCtx.Value(testCtxKey("test")) != "test" {
t.Errorf("Expected context value 'test', but got '%s'", server.baseCtx.Value("test"))
}

// Check that server config is part of context
if server.baseCtx.Value(reflect.TypeFor[ServerConfig]()) != DefaultServerConfig {
t.Errorf("Expected context to key value ServerConfig with value '%s' but got '%s'", DefaultServerConfig, server.baseCtx.Value(reflect.TypeFor[ServerConfig]()))
}
}

func TestServer_WithReadinessChan(t *testing.T) {
// Create a new Server instance with a base context
ready := make(chan struct{})
server := NewServer(":0", WithReadinessChan(ready))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(ready))

if server.ready == nil {
t.Error("Expected non-nil channel")
Expand All @@ -99,7 +105,7 @@ func TestServer_Run(t *testing.T) {
t.Run(fmt.Sprintf("%d times of calling Run", run), func(t *testing.T) {
// Create a new Server instance
ready := make(chan struct{})
server := NewServer(":0", WithReadinessChan(ready))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(ready))

channel := mocks.NewMockChannel(t)
channel.EXPECT().Path().Return("/test")
Expand Down Expand Up @@ -152,7 +158,7 @@ func TestServer_Run(t *testing.T) {
func TestServer_Close(t *testing.T) {
// Create a new Server instance
ready := make(chan struct{})
server := NewServer(":0", WithReadinessChan(ready))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(ready))

// Create a context with a timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
Expand Down Expand Up @@ -202,7 +208,7 @@ func TestServer_Close(t *testing.T) {
func TestServer_Close_NoContext(t *testing.T) {
// Create a new Server instance
ready := make(chan struct{})
server := NewServer(":0", WithReadinessChan(ready))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(ready))

// Create a mock channel
channel := mocks.NewMockChannel(t)
Expand Down Expand Up @@ -249,7 +255,7 @@ func TestServer_Addr(t *testing.T) {
// Create a new Server instance
done := make(chan struct{})

server := NewServer(":0", WithReadinessChan(done))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(done))

defer server.Close()

Expand Down Expand Up @@ -284,7 +290,7 @@ func TestServer_Addr(t *testing.T) {
}
func TestServer_WithTLS(t *testing.T) {
// Create a new Server instance
server := NewServer(":0")
server := NewServer(":0", DefaultServerConfig)
// Set TLS configuration using WithTLS
certPath := "/path/to/cert.pem"
keyPath := "/path/to/key.pem"
Expand Down Expand Up @@ -323,7 +329,7 @@ func TestServer_WithTLS(t *testing.T) {
func TestServer_WithProfilerEndpoint(t *testing.T) {
ready := make(chan struct{})
// Create a new Server instance
server := NewServer(":0", WithReadinessChan(ready))
server := NewServer(":0", DefaultServerConfig, WithReadinessChan(ready))

// Check if the profiler endpoint is disabled by default
if server.pprofEnabled {
Expand Down
2 changes: 1 addition & 1 deletion tests/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestEcho(t *testing.T) {
ch := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

ready := make(chan struct{})
s := server.NewServer(":0", server.WithBaseContext(context.Background()), server.WithReadinessChan(ready))
s := server.NewServer(":0", server.DefaultServerConfig, server.WithBaseContext(context.Background()), server.WithReadinessChan(ready))
s.AddChannel(ch)

go func() {
Expand Down
Loading