@@ -135,15 +135,26 @@ func NewSwarmListenerFromEnv(retries, interval int, logger *log.Logger) (*SwarmL
135135 if err != nil {
136136 return nil , err
137137 }
138- ssListener := NewSwarmServiceListener (dockerClient , logger )
139- ssClient := NewSwarmServiceClient (dockerClient , ignoreKey , "com.df.scrapeNetwork" , logger )
140- ssCache := NewSwarmServiceCache ()
138+ notifyDistributor := NewNotifyDistributorFromEnv (retries , interval , logger )
141139
142- nodeListener := NewNodeListener (dockerClient , logger )
143- nodeClient := NewNodeClient (dockerClient )
144- nodeCache := NewNodeCache ()
140+ var ssListener * SwarmServiceListener
141+ var ssClient * SwarmServiceClient
142+ var ssCache * SwarmServiceCache
143+ var nodeListener * NodeListener
144+ var nodeClient * NodeClient
145+ var nodeCache * NodeCache
146+
147+ if notifyDistributor .HasServiceListeners () {
148+ ssListener = NewSwarmServiceListener (dockerClient , logger )
149+ ssClient = NewSwarmServiceClient (dockerClient , ignoreKey , "com.df.scrapeNetwork" , logger )
150+ ssCache = NewSwarmServiceCache ()
151+ }
145152
146- notifyDistributor := NewNotifyDistributorFromEnv (retries , interval , logger )
153+ if notifyDistributor .HasNodeListeners () {
154+ nodeListener = NewNodeListener (dockerClient , logger )
155+ nodeClient = NewNodeClient (dockerClient )
156+ nodeCache = NewNodeCache ()
157+ }
147158
148159 return newSwarmListener (
149160 ssListener ,
@@ -167,13 +178,13 @@ func NewSwarmListenerFromEnv(retries, interval int, logger *log.Logger) (*SwarmL
167178
168179// Run starts swarm listener
169180func (l * SwarmListener ) Run () {
170- l .connectServiceChannels ()
171- l .connectNodeChannels ()
172181
173- if l .SSEventChan != nil {
182+ if l .NotifyDistributor .HasServiceListeners () {
183+ l .connectServiceChannels ()
174184 l .SSListener .ListenForServiceEvents (l .SSEventChan )
175185 }
176- if l .NodeEventChan != nil {
186+ if l .NotifyDistributor .HasNodeListeners () {
187+ l .connectNodeChannels ()
177188 l .NodeListener .ListenForNodeEvents (l .NodeEventChan )
178189 }
179190
@@ -182,13 +193,6 @@ func (l *SwarmListener) Run() {
182193
183194func (l * SwarmListener ) connectServiceChannels () {
184195
185- // Remove service channels if there are no service listeners
186- if ! l .NotifyDistributor .HasServiceListeners () {
187- l .SSEventChan = nil
188- l .SSNotificationChan = nil
189- return
190- }
191-
192196 go func () {
193197 for event := range l .SSEventChan {
194198 if event .Type == EventTypeCreate {
@@ -289,13 +293,6 @@ func (l *SwarmListener) processServiceEventRemove(event Event) {
289293
290294func (l * SwarmListener ) connectNodeChannels () {
291295
292- // Remove node channels if there are no service listeners
293- if ! l .NotifyDistributor .HasNodeListeners () {
294- l .NodeEventChan = nil
295- l .NodeNotificationChan = nil
296- return
297- }
298-
299296 go func () {
300297 for event := range l .NodeEventChan {
301298 if event .Type == EventTypeCreate {
@@ -389,6 +386,11 @@ func (l *SwarmListener) processNodeEventRemove(event Event) {
389386
390387// NotifyServices places all services on queue to notify services on service events
391388func (l SwarmListener ) NotifyServices (useCache bool ) {
389+
390+ if ! l .NotifyDistributor .HasServiceListeners () {
391+ return
392+ }
393+
392394 services , err := l .SSClient .SwarmServiceList (context .Background ())
393395 if err != nil {
394396 l .Log .Printf ("ERROR: NotifyService, %v" , err )
@@ -405,6 +407,11 @@ func (l SwarmListener) NotifyServices(useCache bool) {
405407
406408// NotifyNodes places all services on queue to notify serivces on node events
407409func (l SwarmListener ) NotifyNodes (useCache bool ) {
410+
411+ if ! l .NotifyDistributor .HasNodeListeners () {
412+ return
413+ }
414+
408415 nodes , err := l .NodeClient .NodeList (context .Background ())
409416 if err != nil {
410417 l .Log .Printf ("ERROR: NotifyNodes, %v" , err )
0 commit comments