forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.go
38 lines (31 loc) · 1.13 KB
/
activity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package server
import "sync/atomic"
// ActivityMonitor can be used to count and share the number of active requests.
type ActivityMonitor struct {
// counter for # of active requests
reqCount uint32
}
// NewActivityMonitor will return a new ActivityMonitor instance.
func NewActivityMonitor() *ActivityMonitor {
return &ActivityMonitor{}
}
// Active returns true if there are requests currently in flight.
func (a *ActivityMonitor) Active() bool {
return a.NumActiveRequests() > 0
}
// CountRequest will increment the request count and signal
// the activity monitor to stay active. Call this in your server
// when you receive a request.
func (a *ActivityMonitor) CountRequest() {
atomic.AddUint32(&a.reqCount, 1)
}
// UncountRequest will decrement the active request count. Best practice is to `defer`
// this function call directly after calling CountRequest().
func (a *ActivityMonitor) UncountRequest() {
atomic.AddUint32(&a.reqCount, ^uint32(0))
}
// NumActiveRequests returns the number of in-flight requests currently
// running on this server.
func (a *ActivityMonitor) NumActiveRequests() uint32 {
return atomic.LoadUint32(&a.reqCount)
}