Skip to content

Commit 57c3ff9

Browse files
committed
RFC: Only init channels that have listeners
1 parent 7e0c57d commit 57c3ff9

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

service/swarmlistener.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ func newSwarmListener(
8484
ssListener SwarmServiceListening,
8585
ssClient SwarmServiceInspector,
8686
ssCache SwarmServiceCacher,
87+
ssEventChan chan Event,
88+
ssNotificationChan chan Notification,
8789

8890
nodeListener NodeListening,
8991
nodeClient NodeInspector,
9092
nodeCache NodeCacher,
93+
nodeEventChan chan Event,
94+
nodeNotificationChan chan Notification,
9195

9296
notifyDistributor NotifyDistributing,
9397

@@ -105,13 +109,13 @@ func newSwarmListener(
105109
SSListener: ssListener,
106110
SSClient: ssClient,
107111
SSCache: ssCache,
108-
SSEventChan: make(chan Event),
109-
SSNotificationChan: make(chan Notification),
112+
SSEventChan: ssEventChan,
113+
SSNotificationChan: ssNotificationChan,
110114
NodeListener: nodeListener,
111115
NodeClient: nodeClient,
112116
NodeCache: nodeCache,
113-
NodeEventChan: make(chan Event),
114-
NodeNotificationChan: make(chan Notification),
117+
NodeEventChan: nodeEventChan,
118+
NodeNotificationChan: nodeNotificationChan,
115119
NotifyDistributor: notifyDistributor,
116120
ServiceCreateRemoveCancelManager: &CreateRemoveCancelManager{
117121
createCancelManager: serviceCreateCancelManager,
@@ -140,29 +144,42 @@ func NewSwarmListenerFromEnv(retries, interval int, logger *log.Logger) (*SwarmL
140144
var ssListener *SwarmServiceListener
141145
var ssClient *SwarmServiceClient
142146
var ssCache *SwarmServiceCache
147+
var ssEventChan chan Event
148+
var ssNotificationChan chan Notification
149+
143150
var nodeListener *NodeListener
144151
var nodeClient *NodeClient
145152
var nodeCache *NodeCache
153+
var nodeEventChan chan Event
154+
var nodeNotificationChan chan Notification
146155

147156
if notifyDistributor.HasServiceListeners() {
148157
ssListener = NewSwarmServiceListener(dockerClient, logger)
149158
ssClient = NewSwarmServiceClient(dockerClient, ignoreKey, "com.df.scrapeNetwork", logger)
150159
ssCache = NewSwarmServiceCache()
160+
ssEventChan = make(chan Event)
161+
ssNotificationChan = make(chan Notification)
151162
}
152163

153164
if notifyDistributor.HasNodeListeners() {
154165
nodeListener = NewNodeListener(dockerClient, logger)
155166
nodeClient = NewNodeClient(dockerClient)
156167
nodeCache = NewNodeCache()
168+
nodeEventChan = make(chan Event)
169+
nodeNotificationChan = make(chan Notification)
157170
}
158171

159172
return newSwarmListener(
160173
ssListener,
161174
ssClient,
162175
ssCache,
176+
ssEventChan,
177+
ssNotificationChan,
163178
nodeListener,
164179
nodeClient,
165180
nodeCache,
181+
nodeEventChan,
182+
nodeNotificationChan,
166183
notifyDistributor,
167184
NewCancelManager(false),
168185
NewCancelManager(false),

service/swarmlistener_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ func (s *SwarmListenerTestSuite) SetupTest() {
4949
s.SSListenerMock,
5050
s.SSClientMock,
5151
s.SSCacheMock,
52+
make(chan Event),
53+
make(chan Notification),
5254
s.NodeListeningMock,
5355
s.NodeClientMock,
5456
s.NodeCacheMock,
57+
make(chan Event),
58+
make(chan Notification),
5559
s.NotifyDistributorMock,
5660
NewCancelManager(true),
5761
NewCancelManager(true),

0 commit comments

Comments
 (0)