aboutsummaryrefslogtreecommitdiff
path: root/libpod/events/memory.go
blob: b3e03d86bca4b3e0aac856f8437305629ccc6b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package events

import (
	"context"
)

// EventMemory is the structure for event writing to a channel. It contains the eventer
// options and the event itself.  Methods for reading and writing are also defined from it.
type EventMemory struct {
	options  EventerOptions
	elements chan *Event
}

// Write event to memory queue
func (e EventMemory) Write(event Event) (err error) {
	e.elements <- &event
	return
}

// Read event(s) from memory queue
func (e EventMemory) Read(ctx context.Context, options ReadOptions) (err error) {
	select {
	case <-ctx.Done():
		return
	default:
	}

	select {
	case event := <-e.elements:
		options.EventChannel <- event
	default:
	}
	return nil
}

// String returns eventer type
func (e EventMemory) String() string {
	return e.options.EventerType
}

// NewMemoryEventer returns configured MemoryEventer
func NewMemoryEventer() Eventer {
	return EventMemory{
		options: EventerOptions{
			EventerType: Memory.String(),
		},
		elements: make(chan *Event, 100),
	}
}