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

Commit 6bbff05

Browse files
authored
Merge pull request #2746 from nishanttotla/events-restructuring
Adding some clarifying comments for event handling
2 parents c1f9872 + b1772c6 commit 6bbff05

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

api/primary.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,16 @@ func profilerSetup(mainRouter *mux.Router, path string) {
117117
// NewPrimary creates a new API router.
118118
func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, debug, enableCors bool) *mux.Router {
119119
// Register the API events handler in the cluster.
120+
121+
// NewEventsHandler creates a new eventsHandler object.
122+
// The new eventsHandler is initialized with no writers or channels.
123+
// This is in api/events.go
120124
eventsHandler := newEventsHandler()
125+
// This just calls c.eventHandlers.RegisterEventHandler(eventsHandler) internally.
126+
// Eventually, eventsHandler is added to the Cluster struct's EventHandlers map, if it
127+
// doesn't already exist there. The Cluster struct implements the cluster.EventHandler
128+
// interface, as does eventsHandler. So calling the Handle function for a Cluster object
129+
// will eventually call the Handle function for eventsHandler.
121130
cluster.RegisterEventHandler(eventsHandler)
122131

123132
context := &context{

cluster/engine.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ func (e *Engine) StartMonitorEvents() {
228228
close(ec)
229229
}()
230230

231+
// this call starts a goroutine that collects events from the the engine, and returns an error to
232+
// ec (the error channel) in case an error occurs. The corresponding Stop() function tears this
233+
// down. The eventsMonitor itself is initialized using the apiClient and handler (defined below)
234+
// The handler function processes events as received from the engine and decides what to do based
235+
// on each event. Moreover, it also calls the eventHandler's Handle() function.
231236
e.eventsMonitor.Start(ec)
232237
}
233238

cluster/swarm/cluster.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,13 @@ func (c *Cluster) addEngine(addr string) bool {
305305
}
306306

307307
engine := cluster.NewEngine(addr, c.overcommitRatio, c.engineOpts)
308+
// This passes c, which has a Handle(Event) (error) function defined, which acts as the handler
309+
// for events. This is the cluster level handler that is called by individual engines when they
310+
// receive/emit events. This Handler in turn calls the eventHandlers.Handle() function.
311+
// eventHandlers is a map from EventHandler -> struct{}, and eventHandlers.Handle() simply calls
312+
// the Handle function for each of the EventHander objects in the map. Remember that EventHandler
313+
// is an interface, that is implemented by both the Cluster object, as well as the eventsHandler
314+
// object in api/events.go
308315
if err := engine.RegisterEventHandler(c); err != nil {
309316
log.Error(err)
310317
}

0 commit comments

Comments
 (0)