2018-02-05 16:05:59 -05:00
|
|
|
package events // import "github.com/docker/docker/daemon/events"
|
2015-04-03 16:58:56 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
eventtypes "github.com/docker/docker/api/types/events"
|
2015-04-03 16:58:56 -04:00
|
|
|
"github.com/docker/docker/pkg/pubsub"
|
|
|
|
)
|
|
|
|
|
2015-11-25 21:03:10 -05:00
|
|
|
const (
|
2017-04-02 18:21:56 -04:00
|
|
|
eventsLimit = 256
|
2015-11-25 21:03:10 -05:00
|
|
|
bufferSize = 1024
|
|
|
|
)
|
2015-04-03 16:58:56 -04:00
|
|
|
|
2015-12-21 17:55:23 -05:00
|
|
|
// Events is pubsub channel for events generated by the engine.
|
2015-04-03 16:58:56 -04:00
|
|
|
type Events struct {
|
|
|
|
mu sync.Mutex
|
2015-12-21 17:55:23 -05:00
|
|
|
events []eventtypes.Message
|
2015-04-03 16:58:56 -04:00
|
|
|
pub *pubsub.Publisher
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns new *Events instance
|
|
|
|
func New() *Events {
|
|
|
|
return &Events{
|
2015-12-21 17:55:23 -05:00
|
|
|
events: make([]eventtypes.Message, 0, eventsLimit),
|
2015-11-25 21:03:10 -05:00
|
|
|
pub: pubsub.NewPublisher(100*time.Millisecond, bufferSize),
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-20 06:41:51 -05:00
|
|
|
// Subscribe adds new listener to events, returns slice of 256 stored
|
2015-10-12 14:54:46 -04:00
|
|
|
// last events, a channel in which you can expect new events (in form
|
|
|
|
// of interface{}, so you need type assertion), and a function to call
|
|
|
|
// to stop the stream of events.
|
2015-12-21 17:55:23 -05:00
|
|
|
func (e *Events) Subscribe() ([]eventtypes.Message, chan interface{}, func()) {
|
2016-07-20 19:11:28 -04:00
|
|
|
eventSubscribers.Inc()
|
2015-04-03 16:58:56 -04:00
|
|
|
e.mu.Lock()
|
2015-12-21 17:55:23 -05:00
|
|
|
current := make([]eventtypes.Message, len(e.events))
|
2015-04-03 16:58:56 -04:00
|
|
|
copy(current, e.events)
|
|
|
|
l := e.pub.Subscribe()
|
|
|
|
e.mu.Unlock()
|
2015-10-12 14:54:46 -04:00
|
|
|
|
|
|
|
cancel := func() {
|
|
|
|
e.Evict(l)
|
|
|
|
}
|
|
|
|
return current, l, cancel
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
|
|
|
|
2017-12-20 06:41:51 -05:00
|
|
|
// SubscribeTopic adds new listener to events, returns slice of 256 stored
|
2015-11-25 21:03:10 -05:00
|
|
|
// last events, a channel in which you can expect new events (in form
|
|
|
|
// of interface{}, so you need type assertion).
|
2016-04-11 14:52:34 -04:00
|
|
|
func (e *Events) SubscribeTopic(since, until time.Time, ef *Filter) ([]eventtypes.Message, chan interface{}) {
|
2016-07-20 19:11:28 -04:00
|
|
|
eventSubscribers.Inc()
|
2015-11-25 21:03:10 -05:00
|
|
|
e.mu.Lock()
|
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
var topic func(m interface{}) bool
|
|
|
|
if ef != nil && ef.filter.Len() > 0 {
|
|
|
|
topic = func(m interface{}) bool { return ef.Include(m.(eventtypes.Message)) }
|
2015-11-25 21:03:10 -05:00
|
|
|
}
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
buffered := e.loadBufferedEvents(since, until, topic)
|
2015-11-25 21:03:10 -05:00
|
|
|
|
|
|
|
var ch chan interface{}
|
2016-03-07 19:02:35 -05:00
|
|
|
if topic != nil {
|
2015-11-25 21:03:10 -05:00
|
|
|
ch = e.pub.SubscribeTopic(topic)
|
|
|
|
} else {
|
|
|
|
// Subscribe to all events if there are no filters
|
|
|
|
ch = e.pub.Subscribe()
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
e.mu.Unlock()
|
2015-11-25 21:03:10 -05:00
|
|
|
return buffered, ch
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:58:56 -04:00
|
|
|
// Evict evicts listener from pubsub
|
|
|
|
func (e *Events) Evict(l chan interface{}) {
|
2016-07-20 19:11:28 -04:00
|
|
|
eventSubscribers.Dec()
|
2015-04-03 16:58:56 -04:00
|
|
|
e.pub.Evict(l)
|
|
|
|
}
|
|
|
|
|
2017-04-02 18:21:56 -04:00
|
|
|
// Log creates a local scope message and publishes it
|
2015-12-21 17:55:23 -05:00
|
|
|
func (e *Events) Log(action, eventType string, actor eventtypes.Actor) {
|
2015-09-15 15:33:11 -04:00
|
|
|
now := time.Now().UTC()
|
2015-12-21 17:55:23 -05:00
|
|
|
jm := eventtypes.Message{
|
|
|
|
Action: action,
|
|
|
|
Type: eventType,
|
|
|
|
Actor: actor,
|
2017-04-02 18:21:56 -04:00
|
|
|
Scope: "local",
|
2015-12-21 17:55:23 -05:00
|
|
|
Time: now.Unix(),
|
|
|
|
TimeNano: now.UnixNano(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill deprecated fields for container and images
|
|
|
|
switch eventType {
|
|
|
|
case eventtypes.ContainerEventType:
|
|
|
|
jm.ID = actor.ID
|
|
|
|
jm.Status = action
|
|
|
|
jm.From = actor.Attributes["image"]
|
|
|
|
case eventtypes.ImageEventType:
|
|
|
|
jm.ID = actor.ID
|
|
|
|
jm.Status = action
|
|
|
|
}
|
|
|
|
|
2017-04-02 18:21:56 -04:00
|
|
|
e.PublishMessage(jm)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishMessage broadcasts event to listeners. Each listener has 100 milliseconds to
|
|
|
|
// receive the event or it will be skipped.
|
|
|
|
func (e *Events) PublishMessage(jm eventtypes.Message) {
|
|
|
|
eventsCounter.Inc()
|
|
|
|
|
2015-09-15 14:28:22 -04:00
|
|
|
e.mu.Lock()
|
|
|
|
if len(e.events) == cap(e.events) {
|
|
|
|
// discard oldest event
|
|
|
|
copy(e.events, e.events[1:])
|
|
|
|
e.events[len(e.events)-1] = jm
|
|
|
|
} else {
|
|
|
|
e.events = append(e.events, jm)
|
|
|
|
}
|
|
|
|
e.mu.Unlock()
|
|
|
|
e.pub.Publish(jm)
|
2015-04-03 16:58:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribersCount returns number of event listeners
|
|
|
|
func (e *Events) SubscribersCount() int {
|
|
|
|
return e.pub.Len()
|
|
|
|
}
|
2016-03-07 19:02:35 -05:00
|
|
|
|
|
|
|
// loadBufferedEvents iterates over the cached events in the buffer
|
2016-04-11 14:52:34 -04:00
|
|
|
// and returns those that were emitted between two specific dates.
|
|
|
|
// It uses `time.Unix(seconds, nanoseconds)` to generate valid dates with those arguments.
|
2016-03-07 19:02:35 -05:00
|
|
|
// It filters those buffered messages with a topic function if it's not nil, otherwise it adds all messages.
|
2016-04-11 14:52:34 -04:00
|
|
|
func (e *Events) loadBufferedEvents(since, until time.Time, topic func(interface{}) bool) []eventtypes.Message {
|
2016-03-07 19:02:35 -05:00
|
|
|
var buffered []eventtypes.Message
|
2016-04-11 14:52:34 -04:00
|
|
|
if since.IsZero() && until.IsZero() {
|
2016-03-07 19:02:35 -05:00
|
|
|
return buffered
|
|
|
|
}
|
|
|
|
|
2016-04-11 14:52:34 -04:00
|
|
|
var sinceNanoUnix int64
|
|
|
|
if !since.IsZero() {
|
|
|
|
sinceNanoUnix = since.UnixNano()
|
|
|
|
}
|
|
|
|
|
|
|
|
var untilNanoUnix int64
|
|
|
|
if !until.IsZero() {
|
|
|
|
untilNanoUnix = until.UnixNano()
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
for i := len(e.events) - 1; i >= 0; i-- {
|
|
|
|
ev := e.events[i]
|
2016-04-11 14:52:34 -04:00
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
if ev.TimeNano < sinceNanoUnix {
|
|
|
|
break
|
|
|
|
}
|
2016-04-11 14:52:34 -04:00
|
|
|
|
|
|
|
if untilNanoUnix > 0 && ev.TimeNano > untilNanoUnix {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:02:35 -05:00
|
|
|
if topic == nil || topic(ev) {
|
|
|
|
buffered = append([]eventtypes.Message{ev}, buffered...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffered
|
|
|
|
}
|