Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reopen Docker event stream when getting EOF error #2240

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions agent/dockerclient/dockerapi/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,22 +863,26 @@ func (dg *dockerGoClient) ContainerEvents(ctx context.Context) (<-chan DockerCon
for {
select {
case err := <-eventErr:
if err == io.EOF || err == io.ErrUnexpectedEOF {
seelog.Info("DockerGoClient: All events have been received")
cancel()
// If parent ctx has been canceled, stop listening and return. Otherwise reopen the stream.
if ctx.Err() != nil {
return
}

if err == io.EOF {
seelog.Infof("DockerGoClient: Docker events stream closed with: %v", err)
} else {
// If an error is returned, we need to reopen channel to continue listening
seelog.Errorf("DockerGoClient: error while listening to Docker Events : %v", err)
nextCtx, nextCancel := context.WithCancel(ctx)
dockerEvents, eventErr = client.Events(nextCtx, types.EventsOptions{})
// Cache the event from docker client.
go buffer.StartListening(nextCtx, dockerEvents)
// Close previous stream after starting to listen on new one
cancel()
// Reassign cancel variable next Cancel function to setup next iteration of loop.
cancel = nextCancel
seelog.Errorf("DockerGoClient: Docker events stream closed with error: %v", err)
}

// Reopen a new event stream to continue listening.
nextCtx, nextCancel := context.WithCancel(ctx)
dockerEvents, eventErr = client.Events(nextCtx, types.EventsOptions{})
// Cache the event from docker client.
go buffer.StartListening(nextCtx, dockerEvents)
// Close previous stream after starting to listen on new one
cancel()
// Reassign cancel variable next Cancel function to setup next iteration of loop.
cancel = nextCancel
case <-ctx.Done():
return
}
Expand Down
7 changes: 5 additions & 2 deletions agent/dockerclient/dockerapi/docker_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,18 @@ func TestContainerEventsEOFError(t *testing.T) {

eventsChan := make(chan events.Message, dockerEventBufferSize)
errChan := make(chan error)
mockDockerSDK.EXPECT().Events(gomock.Any(), gomock.Any()).Return(eventsChan, errChan)
mockDockerSDK.EXPECT().Events(gomock.Any(), gomock.Any()).Return(eventsChan, errChan).MinTimes(1)

dockerEvents, err := client.ContainerEvents(context.TODO())
require.NoError(t, err, "Could not get container events")
go func() {
errChan <- io.EOF
eventsChan <- events.Message{Type: "container", ID: "containerId", Status: "create"}
}()

assert.Equal(t, len(dockerEvents), 0, "Wrong number of docker events")
event := <-dockerEvents
assert.Equal(t, event.DockerID, "containerId", "Wrong docker id")
assert.Equal(t, event.Status, apicontainerstatus.ContainerCreated, "Wrong status")
}

func TestContainerEventsStreamError(t *testing.T) {
Expand Down