Skip to content

Commit

Permalink
internal/api: do not drain requests channel when stopping Server
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed Mar 7, 2024
1 parent 04fc51b commit 1dba9d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 13 additions & 4 deletions internal/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type Server struct {
config *Config
listen net.Listener
requests chan *Request
done chan struct{}
closed chan struct{}

mutex sync.Mutex
stop bool
Expand Down Expand Up @@ -72,9 +74,12 @@ func (s *Server) handleRequest(conn net.Conn) {
}

// forward client's request to daemon
s.requests <- &Request{
select {
case s.requests <- &Request{
msg: msg,
conn: conn,
}:
case <-s.done:
}
}

Expand All @@ -83,6 +88,7 @@ func (s *Server) handleClients() {
defer func() {
_ = s.listen.Close()
close(s.requests)
close(s.closed)
}()
for {
// wait for new client connection
Expand Down Expand Up @@ -199,15 +205,16 @@ func (s *Server) Start() error {

// Stop stops the API server
func (s *Server) Stop() {
close(s.done)

// stop listener
s.setStopping()
err := s.listen.Close()
if err != nil {
log.WithError(err).Error("Daemon could not close unix listener")
}
for range s.requests {
// wait for clients channel close
}

<-s.closed
}

// Requests returns the clients channel
Expand All @@ -220,5 +227,7 @@ func NewServer(config *Config) *Server {
return &Server{
config: config,
requests: make(chan *Request),
done: make(chan struct{}),
closed: make(chan struct{}),
}
}
10 changes: 7 additions & 3 deletions internal/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@ func TestServerRequests(t *testing.T) {
func TestNewServer(t *testing.T) {
config := NewConfig()
server := NewServer(config)

if server == nil ||
server.requests == nil ||
server.done == nil ||
server.closed == nil {
t.Errorf("got nil, want != nil")
}
if server.config != config {
t.Errorf("got %p, want %p", server.config, config)
}
if server.requests == nil {
t.Errorf("got nil, want != nil")
}
}

0 comments on commit 1dba9d5

Please sign in to comment.