Skip to content

Commit

Permalink
feat: Adds new feature for enabling profiler endpoint for the server
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Jun 17, 2024
1 parent 60a8c7c commit 1f2837f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

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

if err := server.Run(); err != nil {
Expand Down
14 changes: 14 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Server struct {
ready chan<- struct{}
addr string
channels []wasabi.Channel
pprofEnabled bool
}

type Option func(*Server)
Expand Down Expand Up @@ -121,6 +122,10 @@ func (s *Server) Run() (err error) {
)
}

if s.pprofEnabled {
mux.Handle("/debug/pprof/", http.DefaultServeMux)
}

s.handler.Handler = mux

s.listenerLock.Lock()
Expand Down Expand Up @@ -229,3 +234,12 @@ func WithTLS(certFile, keyFile string, config ...*tls.Config) Option {
}
}
}

// WithProfilerEndpoint is an option function that enables the profiler endpoint for the server.
// Enabling the profiler endpoint allows profiling and performance monitoring of the server.
// The profiler endpoint is available at /debug/pprof/.
func WithProfilerEndpoint() Option {
return func(s *Server) {
s.pprofEnabled = true
}
}
46 changes: 46 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,49 @@ func TestServer_WithTLS(t *testing.T) {
t.Errorf("Got unexpected error: %v", err)
}
}
func TestServer_WithProfilerEndpoint(t *testing.T) {
ready := make(chan struct{})
// Create a new Server instance
server := NewServer(":0", WithReadinessChan(ready))

// Check if the profiler endpoint is disabled by default
if server.pprofEnabled {
t.Error("Expected profiler endpoint to be disabled")
}

// Apply the WithProfilerEndpoint option
WithProfilerEndpoint()(server)

// Check if the profiler endpoint is enabled
if !server.pprofEnabled {
t.Error("Expected profiler endpoint to be enabled")
}

go func() {
err := server.Run()
if err != nil {
t.Errorf("Got unexpected error: %v", err)
}
}()

defer server.Close()

select {
case <-ready:
case <-time.After(1 * time.Second):
t.Error("Expected server to start")
}

// Check if the profiler endpoint is enabled
res, err := http.Get("http://" + server.Addr().String() + "/debug/pprof/")
if err != nil {
t.Errorf("Got unexpected error: %v", err)
}

defer res.Body.Close()

if res.StatusCode != http.StatusOK {
// TODO: Fix this test, WHY IS IT FAILING?
t.Errorf("Expected status code 200, but got %d", res.StatusCode)
}
}

0 comments on commit 1f2837f

Please sign in to comment.