2017-02-16 22:45:13 +00:00
|
|
|
package execution
|
2017-02-13 18:23:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2017-04-03 20:14:15 +00:00
|
|
|
"github.com/containerd/containerd"
|
2017-02-16 22:45:13 +00:00
|
|
|
|
2017-02-13 18:23:28 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2017-02-16 22:45:13 +00:00
|
|
|
func newCollector(ctx context.Context, runtimes map[string]containerd.Runtime) (*collector, error) {
|
2017-02-13 18:23:28 +00:00
|
|
|
c := &collector{
|
|
|
|
context: ctx,
|
2017-02-16 22:45:13 +00:00
|
|
|
ch: make(chan *containerd.Event, 2048),
|
2017-02-13 18:23:28 +00:00
|
|
|
eventClients: make(map[*eventClient]struct{}),
|
|
|
|
}
|
|
|
|
for _, r := range runtimes {
|
|
|
|
if err := c.collect(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// run the publisher
|
|
|
|
go c.publisher()
|
|
|
|
// run a goroutine that waits for the context to be done
|
|
|
|
// and closes the channel after all writes have finished
|
|
|
|
go c.waitDone()
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventClient struct {
|
|
|
|
eCh chan error
|
2017-02-16 22:45:13 +00:00
|
|
|
w *grpcEventWriter
|
2017-02-13 18:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type collector struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
context context.Context
|
2017-02-16 22:45:13 +00:00
|
|
|
ch chan *containerd.Event
|
2017-02-13 18:23:28 +00:00
|
|
|
eventClients map[*eventClient]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect collects events from the provided runtime
|
2017-02-16 22:45:13 +00:00
|
|
|
func (c *collector) collect(r containerd.Runtime) error {
|
2017-02-13 18:23:28 +00:00
|
|
|
c.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
for e := range r.Events(c.context) {
|
|
|
|
c.ch <- e
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 22:45:13 +00:00
|
|
|
func (c *collector) forward(w *grpcEventWriter) error {
|
2017-02-13 18:23:28 +00:00
|
|
|
client := &eventClient{
|
|
|
|
w: w,
|
|
|
|
eCh: make(chan error, 1),
|
|
|
|
}
|
|
|
|
c.mu.Lock()
|
|
|
|
c.eventClients[client] = struct{}{}
|
|
|
|
c.mu.Unlock()
|
2017-02-16 00:59:58 +00:00
|
|
|
err := <-client.eCh
|
2017-02-13 18:23:28 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
delete(c.eventClients, client)
|
|
|
|
c.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *collector) publisher() {
|
|
|
|
for e := range c.ch {
|
|
|
|
c.mu.Lock()
|
|
|
|
for client := range c.eventClients {
|
|
|
|
if err := client.w.Write(e); err != nil {
|
|
|
|
client.eCh <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.mu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitDone waits for the context to finish, waits for all the goroutines to finish
|
|
|
|
// collecting grpc events from the shim, and closes the output channel
|
|
|
|
func (c *collector) waitDone() {
|
|
|
|
<-c.context.Done()
|
|
|
|
c.wg.Wait()
|
|
|
|
close(c.ch)
|
|
|
|
}
|