Skip to content

Commit

Permalink
Added method (*Server).GetOpenConnectionsCount() and option DisableSl…
Browse files Browse the repository at this point in the history
…eepWhenConcurrencyLimitsExceeded (#485)

* Change sleep when concurrency limits exceeded.
* Added method (*Server).GetOpenConnectionsCount()
  • Loading branch information
xaionaro authored and erikdubbelboer committed Dec 13, 2018
1 parent 1d2d99c commit caea867
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ type Server struct {
// * cONTENT-lenGTH -> Content-Length
DisableHeaderNamesNormalizing bool

// SleepWhenConcurrencyLimitsExceeded is a duration to be slept of if
// the concurrency limit in exceeded (default [when is 0]: don't sleep
// and accept new connections immidiatelly).
SleepWhenConcurrencyLimitsExceeded time.Duration

// NoDefaultServerHeader, when set to true, causes the default Server header
// to be excluded from the Response.
//
Expand Down Expand Up @@ -1566,7 +1571,11 @@ func (s *Server) Serve(ln net.Listener) error {
//
// There is a hope other servers didn't reach their
// concurrency limits yet :)
time.Sleep(100 * time.Millisecond)
//
// See also: https://github.com/valyala/fasthttp/pull/485#discussion_r239994990
if s.SleepWhenConcurrencyLimitsExceeded > 0 {
time.Sleep(s.SleepWhenConcurrencyLimitsExceeded)
}
}
c = nil
}
Expand Down Expand Up @@ -1741,6 +1750,13 @@ func (s *Server) GetCurrentConcurrency() uint32 {
return atomic.LoadUint32(&s.concurrency)
}

// GetOpenConnectionsCount returns a number of opened connections.
//
// This function is intended be used by monitoring systems
func (s *Server) GetOpenConnectionsCount() int32 {
return atomic.LoadInt32(&s.open) - 1
}

func (s *Server) getConcurrency() int {
n := s.Concurrency
if n <= 0 {
Expand Down

0 comments on commit caea867

Please sign in to comment.