/
tuplespace.go
456 lines (401 loc) · 9.28 KB
/
tuplespace.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package tuplespace
import (
"errors"
"sync"
"time"
"github.com/alecthomas/expr"
"gopkg.in/tomb.v2"
)
const (
// MaxTupleLifetime is the maximum time a tuple can be alive in the TupleSpace.
MaxTupleLifetime = time.Second * 60
MaxConsumeTimeout = time.Hour * 24
MaxReservationTimeout = time.Second * 60
)
var (
ErrTimeout = errors.New("timeout")
ErrReservationTimeout = errors.New("reservation timeout")
ErrTupleSpaceDeleted = errors.New("tuplespace deleted")
ErrCancelled = errors.New("cancelled")
)
type Tuple expr.V
type ConsumeOptions struct {
Match string
Timeout time.Duration
All bool
Take bool
Cancel <-chan bool // Cancel can be used to cancel a waiting consume.
}
type tupleEntry struct {
Tuple Tuple
Expires time.Time
lock sync.Mutex
ack chan error
}
func (t *tupleEntry) Processed(err error) {
t.lock.Lock()
defer t.lock.Unlock()
if t.ack == nil {
return
}
t.ack <- err
close(t.ack)
t.ack = nil
}
type tupleEntries struct {
lock sync.Mutex
entries map[*tupleEntry]struct{}
}
func (t *tupleEntries) Close() error {
t.lock.Lock()
defer t.lock.Unlock()
t.entries = nil
return nil
}
func (t *tupleEntries) Size() int {
t.lock.Lock()
defer t.lock.Unlock()
return len(t.entries)
}
func (t *tupleEntries) Add(tuple *tupleEntry) {
t.lock.Lock()
t.entries[tuple] = struct{}{}
t.lock.Unlock()
}
func (t *tupleEntries) Copy() []*tupleEntry {
t.lock.Lock()
defer t.lock.Unlock()
entries := []*tupleEntry{}
for entry := range t.entries {
entries = append(entries, entry)
}
return entries
}
func (t *tupleEntries) ConsumeMatch(take, all bool, m *expr.Expression) (entries []*tupleEntry) {
t.lock.Lock()
defer t.lock.Unlock()
now := time.Now()
for entry := range t.entries {
if entry.Expires.Before(now) {
entry.Processed(ErrTimeout)
delete(t.entries, entry)
} else if m == nil || m.Bool(expr.V(entry.Tuple)) {
entry.Processed(nil)
if take {
delete(t.entries, entry)
}
entries = append(entries, entry)
if !all {
return
}
}
}
return
}
type waiter struct {
match *expr.Expression
expires <-chan time.Time
tuple chan *tupleEntry
take bool
}
func (w *waiter) Close() error {
close(w.tuple)
return nil
}
type waiters struct {
lock sync.Mutex
waiters map[*waiter]struct{}
}
func (w *waiters) add(a *waiter) int {
w.lock.Lock()
defer w.lock.Unlock()
w.waiters[a] = struct{}{}
return len(w.waiters) - 1
}
func (w *waiters) remove(waiter *waiter) {
w.lock.Lock()
defer w.lock.Unlock()
delete(w.waiters, waiter)
}
func (w *waiters) Try(entry *tupleEntry) (taken bool, ok bool) {
w.lock.Lock()
defer w.lock.Unlock()
for waiter := range w.waiters {
if waiter.match.Bool(expr.V(entry.Tuple)) {
waiter.tuple <- entry
delete(w.waiters, waiter)
return waiter.take, true
}
}
return false, false
}
func (w *waiters) Size() int {
w.lock.Lock()
defer w.lock.Unlock()
return len(w.waiters)
}
func (w *waiters) Close() error {
w.lock.Lock()
defer w.lock.Unlock()
for waiter := range w.waiters {
waiter.Close()
}
w.waiters = nil
return nil
}
type TupleSpace struct {
lock sync.Mutex
entries tupleEntries
waiters waiters
status Status
}
func New() *TupleSpace {
return &TupleSpace{
entries: tupleEntries{
entries: map[*tupleEntry]struct{}{},
},
waiters: waiters{
waiters: map[*waiter]struct{}{},
},
}
}
func (t *TupleSpace) Close() error {
t.lock.Lock()
defer t.lock.Unlock()
t.entries.Close()
return t.waiters.Close()
}
type TuplesStatus struct {
Count int `json:"count"`
Seen int `json:"seen"`
}
type WaitersStatus struct {
Count int `json:"count"`
Seen int `json:"seen"`
}
type Status struct {
Tuples TuplesStatus `json:"tuples"`
Waiters WaitersStatus `json:"waiters"`
}
func (t *TupleSpace) Status() *Status {
t.lock.Lock()
defer t.lock.Unlock()
t.status.Tuples.Count = t.entries.Size()
t.status.Waiters.Count = t.waiters.Size()
return &t.status
}
func (t *TupleSpace) Send(tuple Tuple, expires time.Duration) error {
t.sendTuple(tuple, expires, false)
return nil
}
func (t *TupleSpace) SendMany(tuples []Tuple, expires time.Duration) error {
for _, tuple := range tuples {
t.sendTuple(tuple, expires, false)
}
return nil
}
// SendWithAcknowledgement sends a tuple and waits for an acknowledgement that
// the tuple has been processed.
func (t *TupleSpace) SendWithAcknowledgement(tuple Tuple, expires time.Duration) error {
if expires == 0 {
expires = MaxTupleLifetime
}
timeout := time.After(expires)
entry := t.sendTuple(tuple, expires, true)
select {
case <-timeout:
// FIXME: There's a race here. The tuple may still be consumed in the
// window after this timeout.
return ErrTimeout
case err := <-entry.ack:
return err
}
}
func (t *TupleSpace) sendTuple(tuple Tuple, expires time.Duration, ack bool) *tupleEntry {
if expires == 0 || expires > MaxTupleLifetime {
expires = MaxTupleLifetime
}
var ackch chan error
if ack {
ackch = make(chan error, 1)
}
entry := &tupleEntry{
Tuple: tuple,
Expires: time.Now().Add(expires),
ack: ackch,
}
t.lock.Lock()
t.status.Tuples.Seen++
taken, ok := t.waiters.Try(entry)
if !taken || !ok {
t.addEntry(entry)
}
t.lock.Unlock()
return entry
}
// Add an entry to the tuplespace.
func (t *TupleSpace) addEntry(entry *tupleEntry) {
t.entries.Add(entry)
}
// Read an entry from the TupleSpace. Consecutive reads can return the same tuple.
func (t *TupleSpace) Read(match string, timeout time.Duration) (Tuple, error) {
entries, err := t.consume(&ConsumeOptions{
Match: match,
Timeout: timeout,
})
if err != nil {
return nil, err
}
entries[0].Processed(nil)
return entries[0].Tuple, nil
}
func (t *TupleSpace) ReadAll(match string, timeout time.Duration) ([]Tuple, error) {
entries, err := t.consume(&ConsumeOptions{
Match: match,
Timeout: timeout,
All: true,
})
if err != nil {
return nil, err
}
tuples := make([]Tuple, len(entries))
for i, entry := range entries {
tuples[i] = entry.Tuple
entry.Processed(nil)
}
return tuples, nil
}
func (t *TupleSpace) TakeWithCancel(match string, timeout time.Duration, reservationTimeout time.Duration, cancel <-chan bool) (*Reservation, error) {
if reservationTimeout == 0 {
reservationTimeout = MaxReservationTimeout
}
entries, err := t.consume(&ConsumeOptions{
Match: match,
Timeout: timeout,
Take: true,
Cancel: cancel,
})
if err != nil {
return nil, err
}
r := &Reservation{
entry: entries[0],
space: t,
timeout: time.After(reservationTimeout),
}
r.tomb.Go(r.run)
return r, nil
}
// Take a tuple within a reservation. Takes must always be performed inside a
// reservation due so that in a multi-server setup, redundant taken tuples can
// be returned.
func (t *TupleSpace) Take(match string, timeout time.Duration, reservationTimeout time.Duration) (*Reservation, error) {
return t.TakeWithCancel(match, timeout, reservationTimeout, nil)
}
func (t *TupleSpace) consume(req *ConsumeOptions) ([]*tupleEntry, error) {
m, err := expr.Compile(req.Match)
if err != nil {
return nil, err
}
// Lock around searching and configuring waiters
t.lock.Lock()
entries := t.entries.ConsumeMatch(req.Take, req.All, m)
if len(entries) > 0 {
t.lock.Unlock()
return entries, nil
}
var expires <-chan time.Time
if req.Timeout != 0 {
expires = time.After(req.Timeout)
}
waiter := &waiter{
match: m,
expires: expires,
tuple: make(chan *tupleEntry),
take: req.Take,
}
t.status.Waiters.Seen++
t.waiters.add(waiter)
t.lock.Unlock()
// NOTE: We don't remove() the waiter if the tuple space has been deleted,
// thus no "defer t.waiters.remove(id)"" here.
select {
case <-req.Cancel:
t.waiters.remove(waiter)
return nil, ErrCancelled
case <-waiter.expires:
t.waiters.remove(waiter)
return nil, ErrTimeout
case entry := <-waiter.tuple:
if entry == nil {
return nil, ErrTupleSpaceDeleted
}
t.waiters.remove(waiter)
return []*tupleEntry{entry}, nil
}
}
// Consume provides low-level control over consume operations Read, ReadAll and Take.
func (t *TupleSpace) Consume(req *ConsumeOptions) ([]Tuple, error) {
entries, err := t.consume(req)
if err != nil {
return nil, err
}
tuples := make([]Tuple, len(entries))
for i, entry := range entries {
tuples[i] = entry.Tuple
entry.Processed(nil)
}
return tuples, nil
}
type Reservation struct {
tomb tomb.Tomb
lock sync.Mutex
entry *tupleEntry
space *TupleSpace
timeout <-chan time.Time
}
func (r *Reservation) run() error {
select {
case <-r.timeout:
r.lock.Lock()
defer r.lock.Unlock()
r.cancel()
return ErrReservationTimeout
case <-r.tomb.Dying():
return nil
}
}
// Expired returns true if the reservation is no longer valid, for any reason.
func (r *Reservation) Expired() bool {
return r.tomb.Err() != tomb.ErrStillAlive
}
func (r *Reservation) Wait() error {
return r.tomb.Wait()
}
func (r *Reservation) Tuple() Tuple {
return r.entry.Tuple
}
func (r *Reservation) Complete() error {
r.lock.Lock()
if !r.tomb.Alive() {
r.lock.Unlock()
return r.tomb.Err()
}
r.entry.Processed(nil)
r.tomb.Kill(nil)
r.lock.Unlock()
return r.tomb.Wait()
}
func (r *Reservation) Cancel() error {
r.lock.Lock()
defer r.lock.Unlock()
if !r.tomb.Alive() {
return r.tomb.Err()
}
r.tomb.Kill(nil)
r.cancel()
return r.tomb.Wait()
}
func (r *Reservation) cancel() {
r.space.addEntry(r.entry)
}