@@ -26,16 +26,20 @@ import (
2626 "github.com/sirupsen/logrus"
2727)
2828
29- // DiscoveryManager is required to handle multiple pluggable-discovery that
30- // may be shared across platforms
29+ // DiscoveryManager manages the many-to-many communication between all pluggable
30+ // discoveries and all watchers. Each PluggableDiscovery, once started, will
31+ // produce a sequence of "events". These events will be broadcasted to all
32+ // listening Watcher.
33+ // The DiscoveryManager will not start the discoveries until the Start method
34+ // is called.
3135type DiscoveryManager struct {
3236 discoveriesMutex sync.Mutex
33- discoveries map [string ]* discovery.PluggableDiscovery
34- discoveriesRunning bool
35- feed chan * discovery.Event
37+ discoveries map [string ]* discovery.PluggableDiscovery // all registered PluggableDiscovery
38+ discoveriesRunning bool // set to true once discoveries are started
39+ feed chan * discovery.Event // all events will pass through this channel
3640 watchersMutex sync.Mutex
37- watchers map [* PortWatcher ]bool
38- watchersCache map [string ]map [string ]* discovery.Event
41+ watchers map [* PortWatcher ]bool // all registered Watcher
42+ watchersCache map [string ]map [string ]* discovery.Event // this is a cache of all active ports
3943}
4044
4145var tr = i18n .Tr
@@ -85,7 +89,7 @@ func (dm *DiscoveryManager) Start() {
8589 }
8690
8791 go func () {
88- // Feed all watchers with data coming from the discoveries
92+ // Send all events coming from the feed channel to all active watchers
8993 for ev := range dm .feed {
9094 dm .feedEvent (ev )
9195 }
@@ -152,11 +156,13 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
152156 }
153157 go func () {
154158 dm .watchersMutex .Lock ()
159+ // When a watcher is started, send all the current active ports first...
155160 for _ , cache := range dm .watchersCache {
156161 for _ , ev := range cache {
157162 watcher .feed <- ev
158163 }
159164 }
165+ // ...and after that add the watcher to the list of watchers receiving events
160166 dm .watchers [watcher ] = true
161167 dm .watchersMutex .Unlock ()
162168 }()
@@ -165,6 +171,7 @@ func (dm *DiscoveryManager) Watch() (*PortWatcher, error) {
165171
166172func (dm * DiscoveryManager ) startDiscovery (d * discovery.PluggableDiscovery ) (discErr error ) {
167173 defer func () {
174+ // If this function returns an error log it
168175 if discErr != nil {
169176 logrus .Errorf ("Discovery %s failed to run: %s" , d .GetID (), discErr )
170177 }
@@ -181,6 +188,7 @@ func (dm *DiscoveryManager) startDiscovery(d *discovery.PluggableDiscovery) (dis
181188 // XXX do better cleanup if the discovery fails to start
182189
183190 go func () {
191+ // Transfer all incoming events from this discovery to the feed channel
184192 for ev := range eventCh {
185193 dm .feed <- ev
186194 }
0 commit comments