diff --git a/api/api.go b/api/api.go index d20f31ad73..748c603134 100644 --- a/api/api.go +++ b/api/api.go @@ -28,7 +28,7 @@ const APIVERSION = "1.16" type context struct { cluster cluster.Cluster - eventsHandler *eventsHandler + eventsHandler *EventsHandler debug bool tlsConfig *tls.Config } diff --git a/api/events.go b/api/events.go index e67a2fbba9..8b3558788e 100644 --- a/api/events.go +++ b/api/events.go @@ -9,23 +9,24 @@ import ( "github.com/docker/swarm/cluster" ) -type eventsHandler struct { +// EventsHandler broadcasts events to multiple client listeners. +type EventsHandler struct { sync.RWMutex ws map[string]io.Writer cs map[string]chan struct{} } -// NewEventsHandler creates a new eventsHandler for a cluster. +// NewEventsHandler creates a new EventsHandler for a cluster. // The new eventsHandler is initialized with no writers or channels. -func NewEventsHandler() *eventsHandler { - return &eventsHandler{ +func NewEventsHandler() *EventsHandler { + return &EventsHandler{ ws: make(map[string]io.Writer), cs: make(map[string]chan struct{}), } } // Add adds the writer and a new channel for the remote address. -func (eh *eventsHandler) Add(remoteAddr string, w io.Writer) { +func (eh *EventsHandler) Add(remoteAddr string, w io.Writer) { eh.Lock() eh.ws[remoteAddr] = w eh.cs[remoteAddr] = make(chan struct{}) @@ -33,13 +34,13 @@ func (eh *eventsHandler) Add(remoteAddr string, w io.Writer) { } // Wait waits on a signal from the remote address. -func (eh *eventsHandler) Wait(remoteAddr string) { +func (eh *EventsHandler) Wait(remoteAddr string) { <-eh.cs[remoteAddr] } // Handle writes information about a cluster event to each remote address in the cluster that has been added to the events handler. // After a successful write to a remote address, the associated channel is closed and the address is removed from the events handler. -func (eh *eventsHandler) Handle(e *cluster.Event) error { +func (eh *EventsHandler) Handle(e *cluster.Event) error { eh.RLock() str := fmt.Sprintf("{%q:%q,%q:%q,%q:%q,%q:%d,%q:{%q:%q,%q:%q,%q:%q,%q:%q}}", @@ -71,7 +72,7 @@ func (eh *eventsHandler) Handle(e *cluster.Event) error { } // Size returns the number of remote addresses that the events handler currently contains. -func (eh *eventsHandler) Size() int { +func (eh *EventsHandler) Size() int { eh.RLock() defer eh.RUnlock() return len(eh.ws) diff --git a/api/server.go b/api/server.go index d850fd202d..8400500373 100644 --- a/api/server.go +++ b/api/server.go @@ -35,7 +35,7 @@ func newListener(proto, addr string, tlsConfig *tls.Config) (net.Listener, error // // The expected format for a host string is [protocol://]address. The protocol // must be either "tcp" or "unix", with "tcp" used by default if not specified. -func ListenAndServe(c cluster.Cluster, hosts []string, enableCors bool, tlsConfig *tls.Config, eventsHandler *eventsHandler) error { +func ListenAndServe(c cluster.Cluster, hosts []string, enableCors bool, tlsConfig *tls.Config, eventsHandler *EventsHandler) error { context := &context{ cluster: c, eventsHandler: eventsHandler,