Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jhulten/go-dockerclient
base: 20aa6f0bd4
...
head fork: jhulten/go-dockerclient
compare: 79ebc4f80f
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
View
1  AUTHORS
@@ -10,3 +10,4 @@ Philippe Lafoucrière <philippe.lafoucriere@tech-angels.com>
Sridhar Ratnakumar <sridharr@activestate.com>
Tim Schindler <tim@catalyst-zero.com>
Wiliam Souza <wiliamsouza83@gmail.com>
+Jeffrey Hulten <jhulten@gmail.com>
View
30 event.go
@@ -1,3 +1,7 @@
+// Copyright 2013 go-dockerclient authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
package docker
import (
@@ -16,15 +20,15 @@ import (
type APIEvents struct {
Status string
- Id string
+ ID string
From string
- Time uint32
+ Time int64
}
type EventMonitoringState struct {
sync.RWMutex
enabled bool
- lastSeen uint32
+ lastSeen int64
C chan *APIEvents
errC chan error
listeners []chan *APIEvents
@@ -33,7 +37,7 @@ type EventMonitoringState struct {
var eventMonitor EventMonitoringState
var ErrNoListeners = errors.New("No listeners to send event to...")
-func (c *Client) AddEventListener(listener *chan *APIEvents) error {
+func (c *Client) AddEventListener(listener chan *APIEvents) error {
err := eventMonitor.enableEventMonitoring(c)
if err != nil {
return err
@@ -45,7 +49,7 @@ func (c *Client) AddEventListener(listener *chan *APIEvents) error {
return nil
}
-func (c *Client) RemoveEventListener(listener *chan *APIEvents) error {
+func (c *Client) RemoveEventListener(listener chan *APIEvents) error {
err := eventMonitor.removeListener(listener)
if err != nil {
return err
@@ -61,23 +65,23 @@ func (c *Client) RemoveEventListener(listener *chan *APIEvents) error {
return nil
}
-func (eventState *EventMonitoringState) addListener(listener *chan *APIEvents) error {
+func (eventState *EventMonitoringState) addListener(listener chan *APIEvents) error {
eventState.Lock()
defer eventState.Unlock()
if listenerExists(listener, &eventState.listeners) {
return fmt.Errorf("Listener already exists")
}
- eventState.listeners = append(eventState.listeners, *listener)
+ eventState.listeners = append(eventState.listeners, listener)
return nil
}
-func (eventState *EventMonitoringState) removeListener(listener *chan *APIEvents) error {
+func (eventState *EventMonitoringState) removeListener(listener chan *APIEvents) error {
eventState.Lock()
defer eventState.Unlock()
var newListeners []chan *APIEvents
if listenerExists(listener, &eventState.listeners) {
for _, l := range eventState.listeners {
- if l != *listener {
+ if l != listener {
newListeners = append(newListeners, l)
}
}
@@ -86,9 +90,9 @@ func (eventState *EventMonitoringState) removeListener(listener *chan *APIEvents
return nil
}
-func listenerExists(a *chan *APIEvents, list *[]chan *APIEvents) bool {
+func listenerExists(a chan *APIEvents, list *[]chan *APIEvents) bool {
for _, b := range *list {
- if b == *a {
+ if b == a {
return true
}
}
@@ -127,11 +131,11 @@ func (eventState *EventMonitoringState) monitorEvents(c *Client) {
time.Sleep(10 * time.Millisecond)
}
- for err = c.eventHijack(eventState.lastSeen, eventState.C, eventState.errC); err != nil && retries < 5; retries++ {
+ for err = c.eventHijack(uint32(eventState.lastSeen), eventState.C, eventState.errC); err != nil && retries < 5; retries++ {
waitTime := float64(time.Duration(100*time.Millisecond)) * math.Pow(2, float64(retries))
eventState.errC <- fmt.Errorf("connection to event stream failed, retrying in %n: %s", waitTime, err)
time.Sleep(time.Duration(int64(waitTime)))
- err = c.eventHijack(eventState.lastSeen, eventState.C, eventState.errC)
+ err = c.eventHijack(uint32(eventState.lastSeen), eventState.C, eventState.errC)
}
if err != nil {
View
4 event_test.go
@@ -1,3 +1,7 @@
+// Copyright 2013 go-dockerclient authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
package docker
// import (
View
38 example_test.go
@@ -7,9 +7,11 @@ package docker_test
import (
"archive/tar"
"bytes"
- "github.com/fsouza/go-dockerclient"
"io"
"log"
+ "time"
+
+ "github.com/fsouza/go-dockerclient"
)
func ExampleClient_AttachToContainer() {
@@ -73,3 +75,37 @@ func ExampleClient_CopyFromContainer() {
}
log.Println(buf.String())
}
+
+func ExampleClient_ListenEvents() {
+ client, err := docker.NewClient("http://localhost:4243")
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ listener := make(chan *docker.APIEvents)
+ err = client.AddEventListener(listener)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ defer func() {
+
+ err = client.RemoveEventListener(listener)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ }()
+
+ timeout := time.After(1 * time.Second)
+
+ for {
+ select {
+ case msg := <-listener:
+ log.Println(msg)
+ case <-timeout:
+ break
+ }
+ }
+
+}
View
51 testing/server.go
@@ -11,9 +11,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "github.com/fsouza/go-dockerclient"
- "github.com/fsouza/go-dockerclient/utils"
- "github.com/gorilla/mux"
mathrand "math/rand"
"net"
"net/http"
@@ -21,6 +18,10 @@ import (
"strings"
"sync"
"time"
+
+ "github.com/fsouza/go-dockerclient"
+ "github.com/fsouza/go-dockerclient/utils"
+ "github.com/gorilla/mux"
)
// DockerServer represents a programmable, concurrent (not much), HTTP server
@@ -71,6 +72,7 @@ func (s *DockerServer) buildMuxer() {
s.mux.Path("/images/json").Methods("GET").HandlerFunc(s.listImages)
s.mux.Path("/images/{id:.*}").Methods("DELETE").HandlerFunc(s.removeImage)
s.mux.Path("/images/{name:.*}/push").Methods("POST").HandlerFunc(s.pushImage)
+ s.mux.Path("/events").Methods("GET").HandlerFunc(s.listEvents)
}
// Stop stops the server.
@@ -401,3 +403,46 @@ func (s *DockerServer) removeImage(w http.ResponseWriter, r *http.Request) {
s.images[index] = s.images[len(s.images)-1]
s.images = s.images[:len(s.images)-1]
}
+
+func (s *DockerServer) listEvents(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/json")
+
+ var events [][]byte
+ count := mathrand.Intn(20)
+ for i := 0; i < count; i++ {
+ data, err := json.Marshal(s.generateEvent())
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ events = append(events, data)
+ }
+
+ w.WriteHeader(http.StatusOK)
+
+ for _, d := range events {
+ fmt.Fprintln(w, d)
+ time.Sleep(time.Duration(mathrand.Intn(200)) * time.Millisecond)
+ }
+}
+
+func (s *DockerServer) generateEvent() *docker.APIEvents {
+ var eventType string
+
+ switch mathrand.Intn(4) {
+ case 0:
+ eventType = "create"
+ case 1:
+ eventType = "start"
+ case 2:
+ eventType = "stop"
+ case 3:
+ eventType = "destroy"
+ }
+
+ return &docker.APIEvents{
+ ID: s.generateID(),
+ Status: eventType,
+ From: "mybase:latest",
+ Time: time.Now().Unix()}
+}

No commit comments for this range

Something went wrong with that request. Please try again.