Skip to content

Commit

Permalink
merge server stop method to shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
snowAvocado committed Dec 23, 2023
1 parent ee7b871 commit 55e51f1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
45 changes: 19 additions & 26 deletions network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ type IServer interface {
Run() *gerr.GatewayDError
Shutdown()
IsRunning() bool

CountConnections() int
Stop() error
}

type Server struct {
Expand Down Expand Up @@ -581,7 +579,25 @@ func (s *Server) Shutdown() {
s.mu.Unlock()

// Shutdown the server.
if err := s.Stop(); err != nil {
var err error
s.running.Store(false)
if s.listener != nil {
if err = s.listener.Close(); err != nil {
s.logger.Error().Err(err).Msg("Failed to close listener")
}
} else {
s.logger.Error().Msg("Listener is not initialized")
}

select {
case <-s.stopServer:
s.logger.Info().Msg("Server stopped")
default:
s.stopServer <- struct{}{}
close(s.stopServer)
}

if err != nil {
s.logger.Error().Err(err).Msg("Failed to shutdown server")
span.RecordError(err)
}
Expand Down Expand Up @@ -663,26 +679,3 @@ func (s *Server) CountConnections() int {
defer s.mu.RUnlock()
return int(s.connections)
}

// Stop stops the server.
func (s *Server) Stop() error {
var err error
s.running.Store(false)
if s.listener != nil {
if err = s.listener.Close(); err != nil {
s.logger.Error().Err(err).Msg("Failed to close listener")
}
} else {
s.logger.Error().Msg("Listener is not initialized")
}

select {
case <-s.stopServer:
s.logger.Info().Msg("Server stopped")
return err //nolint:wrapcheck
default:
s.stopServer <- struct{}{}
close(s.stopServer)
return err //nolint:wrapcheck
}
}
2 changes: 1 addition & 1 deletion network/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func TestRunServer(t *testing.T) {
// Wait for the server to stop.
<-time.After(100 * time.Millisecond)

//check server status and connections
// check server status and connections
assert.False(t, server.running.Load())
assert.Zero(t, server.connections)

Expand Down

0 comments on commit 55e51f1

Please sign in to comment.