Skip to content

Commit

Permalink
increase correctness of open conn count (#738)
Browse files Browse the repository at this point in the history
Currently, it appears that GetOpenConnectionsCount underreports the
count by 1 after Shutdown has been called. This leads to a confusing
value when servers are gracefully terminating. For instance, if a server
is stuck in graceful termination, and there is one open connection
keeping it alive, GetOpenConnectionsCount would report zero.

This fix removes the decrement while the server is shutting down. It is
not perfect due to the use of two sequential atomic loads, but in the
common case it should return a more correct value overall.
  • Loading branch information
forestgagnon committed Feb 7, 2020
1 parent 69d5c37 commit b8803fe
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,15 @@ func (s *Server) GetCurrentConcurrency() uint32 {
//
// This function is intended be used by monitoring systems
func (s *Server) GetOpenConnectionsCount() int32 {
return atomic.LoadInt32(&s.open) - 1
if atomic.LoadInt32(&s.stop) == 0 {
// Decrement by one to avoid reporting the extra open value that gets
// counted while the server is listening.
return atomic.LoadInt32(&s.open) - 1
}
// This is not perfect, because s.stop could have changed to zero
// before we load the value of s.open. However, in the common case
// this avoids underreporting open connections by 1 during server shutdown.
return atomic.LoadInt32(&s.open)
}

func (s *Server) getConcurrency() int {
Expand Down

0 comments on commit b8803fe

Please sign in to comment.