Skip to content
This repository was archived by the owner on Feb 1, 2021. It is now read-only.

Commit 9249e16

Browse files
committed
Moving listener count to APIEventHandler
Signed-off-by: Nishant Totla <nishanttotla@gmail.com>
1 parent f9f7962 commit 9249e16

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

api/handlers.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"sort"
1414
"strconv"
1515
"strings"
16-
"sync/atomic"
1716
"time"
1817

1918
log "github.com/Sirupsen/logrus"
@@ -42,7 +41,7 @@ var (
4241
func getInfo(c *context, w http.ResponseWriter, r *http.Request) {
4342
info := apitypes.Info{
4443
Images: len(c.cluster.Images().Filter(cluster.ImageFilterOptions{})),
45-
NEventsListener: int(atomic.LoadUint64(c.listenerCount)),
44+
NEventsListener: c.eventsHandler.Size(),
4645
Debug: c.debug,
4746
MemoryLimit: true,
4847
SwapLimit: true,
@@ -893,9 +892,6 @@ func getEvents(c *context, w http.ResponseWriter, r *http.Request) {
893892
eventsChan, cancelFunc := c.eventsHandler.Watch()
894893
defer cancelFunc()
895894

896-
atomic.AddUint64(c.listenerCount, 1)
897-
defer atomic.AddUint64(c.listenerCount, ^uint64(0))
898-
899895
// create timer for --until
900896
var (
901897
timer *time.Timer

api/primary.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
type context struct {
1616
cluster cluster.Cluster
1717
eventsHandler *cluster.APIEventHandler
18-
listenerCount *uint64
1918
statusHandler StatusHandler
2019
debug bool
2120
tlsConfig *tls.Config
@@ -121,14 +120,11 @@ func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHan
121120

122121
// eventsHandler is the handler for API events
123122
eventsHandler := cluster.NewAPIEventHandler()
124-
// listenerCount keeps track of the number of API events connections
125-
listenerCount := uint64(0)
126123
cluster.RegisterEventHandler(eventsHandler)
127124

128125
context := &context{
129126
cluster: cluster,
130127
eventsHandler: eventsHandler,
131-
listenerCount: &listenerCount,
132128
statusHandler: status,
133129
tlsConfig: tlsConfig,
134130
}

cluster/api_events.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cluster
22

33
import (
4+
"sync/atomic"
45
"time"
56

67
"github.com/docker/go-events"
@@ -14,20 +15,32 @@ const (
1415

1516
// APIEventsHandler broadcasts events to multiple client listeners.
1617
type APIEventHandler struct {
17-
watchQueue *watch.Queue
18+
listenerCount *uint64
19+
watchQueue *watch.Queue
1820
}
1921

2022
// NewAPIEventHandler creates a new APIEventsHandler for a cluster.
2123
// The new eventsHandler is initialized with no writers or channels.
2224
func NewAPIEventHandler() *APIEventHandler {
25+
count := uint64(0)
2326
return &APIEventHandler{
24-
watchQueue: watch.NewQueue(watch.WithTimeout(defaultEventQueueTimeout), watch.WithLimit(defaultEventQueueLimit), watch.WithCloseOutChan()),
27+
listenerCount: &count,
28+
watchQueue: watch.NewQueue(watch.WithTimeout(defaultEventQueueTimeout), watch.WithLimit(defaultEventQueueLimit), watch.WithCloseOutChan()),
2529
}
2630
}
2731

2832
// Add adds the writer and a new channel for the remote address.
29-
func (eh *APIEventHandler) Watch() (eventq chan events.Event, cancel func()) {
30-
eventq, cancel = eh.watchQueue.Watch()
33+
func (eh *APIEventHandler) Watch() (chan events.Event, func()) {
34+
// create a new queue and subscribe to it
35+
eventq, cancelFunc := eh.watchQueue.Watch()
36+
// increment counter
37+
atomic.AddUint64(eh.listenerCount, 1)
38+
39+
cancel := func() {
40+
// decrement counter
41+
atomic.AddUint64(eh.listenerCount, ^uint64(0))
42+
cancelFunc()
43+
}
3144
return eventq, cancel
3245
}
3346

@@ -41,3 +54,8 @@ func (eh *APIEventHandler) Handle(e *Event) error {
4154
eh.watchQueue.Publish(e)
4255
return nil
4356
}
57+
58+
// Size returns the number of event queues currently listening for events
59+
func (eh *APIEventHandler) Size() int {
60+
return int(atomic.LoadUint64(eh.listenerCount))
61+
}

0 commit comments

Comments
 (0)