Skip to content

Commit

Permalink
when docker connect failed should exit event
Browse files Browse the repository at this point in the history
  • Loading branch information
slene committed Jul 16, 2015
1 parent 725c89a commit 0dc8031
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ func (c *Client) RemoveEventListener(listener chan *APIEvents) error {
return err
}
if len(c.eventMonitor.listeners) == 0 {
err = c.eventMonitor.disableEventMonitoring()
if err != nil {
return err
}
c.eventMonitor.disableEventMonitoring()
}
return nil
}
Expand Down Expand Up @@ -118,8 +115,6 @@ func (eventState *eventMonitoringState) removeListener(listener chan<- *APIEvent
}

func (eventState *eventMonitoringState) closeListeners() {
eventState.Lock()
defer eventState.Unlock()
for _, l := range eventState.listeners {
close(l)
eventState.Add(-1)
Expand Down Expand Up @@ -151,9 +146,13 @@ func (eventState *eventMonitoringState) enableEventMonitoring(c *Client) error {
}

func (eventState *eventMonitoringState) disableEventMonitoring() error {
eventState.Wait()
eventState.Lock()
defer eventState.Unlock()

eventState.closeListeners()

eventState.Wait()

if eventState.enabled {
eventState.enabled = false
close(eventState.C)
Expand All @@ -168,8 +167,9 @@ func (eventState *eventMonitoringState) monitorEvents(c *Client) {
time.Sleep(10 * time.Millisecond)
}
if err = eventState.connectWithRetry(c); err != nil {
eventState.closeListeners()
eventState.terminate()
// terminate if connect failed
eventState.disableEventMonitoring()
return
}
for eventState.isEnabled() {
timeout := time.After(100 * time.Millisecond)
Expand All @@ -179,15 +179,14 @@ func (eventState *eventMonitoringState) monitorEvents(c *Client) {
return
}
if ev == EOFEvent {
eventState.closeListeners()
eventState.terminate()
eventState.disableEventMonitoring()
return
}
eventState.updateLastSeen(ev)
go eventState.sendEvent(ev)
case err = <-eventState.errC:
if err == ErrNoListeners {
eventState.terminate()
eventState.disableEventMonitoring()
return
} else if err != nil {
defer func() { go eventState.monitorEvents(c) }()
Expand Down Expand Up @@ -247,10 +246,6 @@ func (eventState *eventMonitoringState) updateLastSeen(e *APIEvents) {
}
}

func (eventState *eventMonitoringState) terminate() {
eventState.disableEventMonitoring()
}

func (c *Client) eventHijack(startTime int64, eventChan chan *APIEvents, errChan chan error) error {
uri := "/events"
if startTime != 0 {
Expand Down

0 comments on commit 0dc8031

Please sign in to comment.