-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcell.go
267 lines (241 loc) · 6.64 KB
/
cell.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Tideland Go Cells - Cell
//
// Copyright (C) 2010-2017 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package cells
//--------------------
// IMPORTS
//--------------------
import (
"sync"
"time"
"github.com/tideland/golib/errors"
"github.com/tideland/golib/identifier"
"github.com/tideland/golib/logger"
"github.com/tideland/golib/loop"
"github.com/tideland/golib/monitoring"
)
//--------------------
// CONNECTIONS
//--------------------
// connections manages the connections to connected
// cells.
type connections struct {
mutex sync.RWMutex
cells []*cell
}
// newConnections creates an instance of the
// connection manager.
func newConnections() *connections {
return &connections{
cells: []*cell{},
}
}
// add adds a new cell with a given identifier to the
// connections.
func (cs *connections) add(c *cell) {
cs.mutex.Lock()
defer cs.mutex.Unlock()
for _, csc := range cs.cells {
if csc == c {
return
}
}
cs.cells = append(cs.cells, c)
}
// remove deletes the identified cell.
func (cs *connections) remove(id string) {
cs.mutex.Lock()
defer cs.mutex.Unlock()
remaining := []*cell{}
for _, csc := range cs.cells {
if csc.id != id {
remaining = append(remaining, csc)
}
}
cs.cells = remaining
}
// ids returns the identifiers of the connected cells.
func (cs *connections) ids() []string {
var ids []string
for _, csc := range cs.cells {
ids = append(ids, csc.id)
}
return ids
}
// do executes the passed function for all connected cells
// and collects potential errors.
func (cs *connections) do(f func(c *cell) error) error {
cs.mutex.RLock()
defer cs.mutex.RUnlock()
var errs []error
for _, csc := range cs.cells {
if err := f(csc); err != nil {
errs = append(errs, err)
}
}
switch len(errs) {
case 0:
return nil
case 1:
return errs[0]
default:
return errors.Collect(errs...)
}
}
//--------------------
// CELL
//--------------------
// cell for event processing.
type cell struct {
env *environment
id string
measuringID string
queue Queue
behavior Behavior
emitters *connections
subscribers *connections
recoveringNumber int
recoveringDuration time.Duration
loop loop.Loop
}
// newCell create a new cell around a behavior.
func newCell(env *environment, id string, behavior Behavior) (*cell, error) {
logger.Infof("cell '%s' starts", id)
// Init cell runtime.
c := &cell{
env: env,
id: id,
measuringID: identifier.Identifier("cells", env.id, "cell", id),
queue: env.createQueue(),
behavior: behavior,
emitters: newConnections(),
subscribers: newConnections(),
}
// Set configuration.
if brf, ok := behavior.(BehaviorRecoveringFrequency); ok {
number, duration := brf.RecoveringFrequency()
if duration.Seconds()/float64(number) < 0.1 {
number = minRecoveringNumber
duration = minRecoveringDuration
}
c.recoveringNumber = number
c.recoveringDuration = duration
} else {
c.recoveringNumber = minRecoveringNumber
c.recoveringDuration = minRecoveringDuration
}
// Init behavior.
if err := behavior.Init(c); err != nil {
return nil, errors.Annotate(err, ErrCellInit, errorMessages, id)
}
// Start backend.
c.loop = loop.GoRecoverable(c.backendLoop, c.checkRecovering, id)
return c, nil
}
// Environment implements the Cell interface.
func (c *cell) Environment() Environment {
return c.env
}
// ID implements the Cell interface.
func (c *cell) ID() string {
return c.id
}
// Emit implements the Cell interface.
func (c *cell) Emit(event Event) error {
return c.SubscribersDo(func(cs Subscriber) error {
return cs.ProcessEvent(event)
})
}
// EmitNew implements the Cell interface.
func (c *cell) EmitNew(topic string, payload interface{}) error {
event, err := NewEvent(topic, payload)
if err != nil {
return err
}
return c.Emit(event)
}
// ProcessEvent implements the Subscriber interface.
func (c *cell) ProcessEvent(event Event) error {
return c.queue.Emit(event)
}
// ProcessNewEvent implements the Subscriber interface.
func (c *cell) ProcessNewEvent(topic string, payload Payload) error {
event, err := NewEvent(topic, payload)
if err != nil {
return err
}
return c.ProcessEvent(event)
}
// SubscribersDo implements the Subscriber interface.
func (c *cell) SubscribersDo(f func(s Subscriber) error) error {
return c.subscribers.do(func(sc *cell) error { return f(Subscriber(sc)) })
}
// stop terminates the cell.
func (c *cell) stop() error {
// Terminate connactions to emitters and subscribers.
c.emitters.do(func(ec *cell) error {
ec.subscribers.remove(c.id)
return nil
})
c.subscribers.do(func(sc *cell) error {
sc.emitters.remove(c.id)
return nil
})
// Stop own backend.
c.queue.Close()
err := c.loop.Stop()
if err != nil {
logger.Errorf("cell '%s' stopped with error: %v", c.id, err)
} else {
logger.Infof("cell '%s' stopped", c.id)
}
return err
}
// backendLoop is the backend for the processing of messages.
func (c *cell) backendLoop(l loop.Loop) error {
totalCellsID := identifier.Identifier("cells", c.env.ID(), "total-cells")
monitoring.IncrVariable(totalCellsID)
defer monitoring.DecrVariable(totalCellsID)
for {
select {
case <-l.ShallStop():
return c.behavior.Terminate()
case event := <-c.queue.Events():
if event == nil {
panic("received illegal nil event!")
}
measuring := monitoring.BeginMeasuring(c.measuringID)
err := c.behavior.ProcessEvent(event)
measuring.EndMeasuring()
if err != nil {
logger.Errorf("cell %q processed event %q with error: %v", c.id, event.Topic(), err)
return err
}
}
}
}
// checkRecovering checks if the cell may recover after a panic. It will
// signal an error and let the cell stop working if there have been 12 recoverings
// during the last minute or the behaviors Recover() signals, that it cannot
// handle the error.
func (c *cell) checkRecovering(rs loop.Recoverings) (loop.Recoverings, error) {
logger.Warningf("recovering cell %q after error: %v", c.id, rs.Last().Reason)
// Check frequency.
if rs.Frequency(c.recoveringNumber, c.recoveringDuration) {
err := errors.New(ErrRecoveredTooOften, errorMessages, rs.Last().Reason)
logger.Errorf("recovering frequency of cell %q too high", c.id)
return nil, err
}
// Try to recover.
if err := c.behavior.Recover(rs.Last().Reason); err != nil {
err := errors.Annotate(err, ErrEventRecovering, errorMessages, rs.Last().Reason)
logger.Errorf("recovering of cell %q failed: %v", c.id, err)
return nil, err
}
logger.Infof("successfully recovered cell %q", c.id)
return rs.Trim(c.recoveringNumber), nil
}
// EOF