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

Fix panic on sse-server shutdown #36

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 15 additions & 6 deletions sse.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Server struct {
channels map[string]*Channel
addClient chan *Client
removeClient chan *Client
shutdown chan bool
shutdown chan struct{}
closeChannel chan string
}

Expand All @@ -37,7 +37,7 @@ func NewServer(options *Options) *Server {
make(map[string]*Channel),
make(chan *Client),
make(chan *Client),
make(chan bool),
make(chan struct{}),
make(chan string),
}

Expand Down Expand Up @@ -76,14 +76,24 @@ func (s *Server) ServeHTTP(response http.ResponseWriter, request *http.Request)
channelName = s.options.ChannelNameFunc(request)
}

select {
case <-s.shutdown:
http.Error(response, "SSE server is in termination state.", http.StatusServiceUnavailable)
return
default:
}

lastEventID := request.Header.Get("Last-Event-ID")
c := newClient(lastEventID, channelName)
s.addClient <- c
closeNotify := request.Context().Done()

go func() {
<-closeNotify
s.removeClient <- c
select {
case <-s.shutdown:
case <-closeNotify:
s.removeClient <- c
}
}()

response.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -128,7 +138,7 @@ func (s *Server) Restart() {

// Shutdown performs a graceful server shutdown.
func (s *Server) Shutdown() {
s.shutdown <- true
close(s.shutdown)
}

// ClientCount returns the number of clients connected to this server.
Expand Down Expand Up @@ -255,7 +265,6 @@ func (s *Server) dispatch() {
close(s.addClient)
close(s.removeClient)
close(s.closeChannel)
close(s.shutdown)

s.options.Logger.Print("server stopped.")
return
Expand Down
50 changes: 50 additions & 0 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tests

import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/alexandrevicenzi/go-sse"
)

func TestServerShutdown(t *testing.T) {
sseServer := sse.NewServer(nil)
s := httptest.NewServer(sseServer)

ch := make(chan struct{})
ch2 := make(chan struct{})
defer close(ch)

go func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, s.URL, nil)
if err != nil {
t.Fail()
}
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")

_, err = http.DefaultClient.Do(req)
if err != nil {
t.Fail()
}

close(ch2)
// Wait until the main test routine is done
<-ch
}()
// Wait until the HTTP request is sent
<-ch2

sseServer.Shutdown()
s.Close()

// Wait a bit after closing HTTP server
time.Sleep(250 * time.Millisecond)
}