forked from vmware/vic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.go
209 lines (181 loc) · 5.32 KB
/
subscriber.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package event
import (
"sync/atomic"
log "github.com/Sirupsen/logrus"
"github.com/vmware/vic/lib/portlayer/event/events"
)
const (
suspendDisabled int32 = iota
suspendDiscard
suspendQueue
)
type Subscriber interface {
// Topic returns the topic this subscriber is subscribed to
Topic() string
// Name returns the name of the subscriber
Name() string
// Suspend suspends processing events by the subscriber. If
// queueEvents is true, the events are queued until Resume()
// is called. If queueEvents is false, events passed into
// onEvent() after this call are discarded.
Suspend(queueEvents bool)
// Resume resumes processing of events by the subscriber.
// If Suspend() was called with queueEvents as true, any events
// that were passed to onEvent() after Suspend() returned are
// processed first.
Resume()
// IsSuspended returns true if the subscriber is suspended.
IsSuspended() bool
// Discarded returns the number of packets that were discarded by
// the subscriber as a result of Pause() being called with
// queueEvents as false.
Discarded() uint64
// Dropped returns the number of packets that were dropped when
// the event queue overflows. This only happens when Pause()
// is called with queueEvents as true.
Dropped() uint64
// onEvent is called by event.Manager to send an event to
// a subscriber
onEvent(events.Event)
}
type subscriber struct {
topic string
name string
callback func(e events.Event)
eventQ chan events.Event
suspendState int32
discarded, dropped uint64
suspend chan suspendCmd
}
type suspendCmd struct {
suspend bool
done chan struct{}
}
const maxEventQueueSize = 1000
// newSubscriber creates a new subscriber to topic
func newSubscriber(topic, name string, callback func(e events.Event)) Subscriber {
s := &subscriber{
topic: topic,
name: name,
callback: callback,
eventQ: make(chan events.Event, maxEventQueueSize),
suspend: make(chan suspendCmd),
}
go func() {
suspended := false
var done chan struct{}
for {
if done != nil {
done <- struct{}{}
done = nil
}
if suspended {
select {
case c := <-s.suspend:
suspended = c.suspend
done = c.done
}
continue
}
// not suspended
select {
case e := <-s.eventQ:
s.callback(e)
case c := <-s.suspend:
suspended = c.suspend
done = c.done
}
}
}()
return s
}
// Topic returns the topic this subscriber is subscribed to
func (s *subscriber) Topic() string {
return s.topic
}
// Name returns the name of the subscriber
func (s *subscriber) Name() string {
return s.name
}
// onEvent is called by event.Manager to send an event to
// a subscriber
func (s *subscriber) onEvent(e events.Event) {
switch atomic.LoadInt32(&s.suspendState) {
case suspendDisabled:
s.eventQ <- e
case suspendDiscard:
log.Warnf("discarding event %q", e)
atomic.AddUint64(&s.discarded, 1)
case suspendQueue:
done := false
for !done {
select {
case s.eventQ <- e:
done = true
default:
// make room; discard oldest
log.Warnf("dropping event %q", <-s.eventQ)
atomic.AddUint64(&s.dropped, 1)
}
}
}
}
// Suspend suspends processing events by the subscriber. If
// queueEvents is true, the events are queued until Resume()
// is called. If queueEvents is false, events passed into
// onEvent() after this call are discarded.
func (s *subscriber) Suspend(queueEvents bool) {
defer func() {
done := make(chan struct{})
s.suspend <- suspendCmd{suspend: true, done: done}
<-done
close(done)
}()
if queueEvents {
atomic.StoreInt32(&s.suspendState, suspendQueue)
return
}
atomic.StoreInt32(&s.suspendState, suspendDiscard)
}
// Resume resumes processing of events by the subscriber.
// If Suspend() was called with queueEvents as true, any events
// that were passed to onEvent() after Suspend() returned are
// processed first.
func (s *subscriber) Resume() {
defer func() {
done := make(chan struct{})
s.suspend <- suspendCmd{suspend: false, done: done}
<-done
close(done)
}()
atomic.StoreInt32(&s.suspendState, suspendDisabled)
}
// IsSuspended returns true if the subscriber is suspended.
func (s *subscriber) IsSuspended() bool {
return atomic.LoadInt32(&s.suspendState) != suspendDisabled
}
// Discarded returns the number of packets that were discarded by
// the subscriber as a result of Pause() being called with
// queueEvents as false.
func (s *subscriber) Discarded() uint64 {
return atomic.LoadUint64(&s.discarded)
}
// Dropped returns the number of packets that were dropped when
// the event queue overflows. This only happens when Pause()
// is called with queueEvents as true.
func (s *subscriber) Dropped() uint64 {
return atomic.LoadUint64(&s.dropped)
}