-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwait.go
More file actions
63 lines (61 loc) · 1.38 KB
/
Copy pathwait.go
File metadata and controls
63 lines (61 loc) · 1.38 KB
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
package lasr
import "github.com/boltdb/bolt"
// Wait causes a message to wait for other messages to Ack, before entering the
// Ready state.
//
// When all of the messages Wait is waiting on have been Acked, then the message
// will enter the Ready state.
//
// When there are no messages to wait on, Wait behaves the same as Send.
func (q *Q) Wait(msg []byte, on ...ID) (ID, error) {
if len(on) < 1 {
return q.Send(msg)
}
var id ID
q.mu.RLock()
defer q.mu.RUnlock()
return id, q.db.Update(func(tx *bolt.Tx) error {
var err error
id, err = q.nextSequence(tx)
if err != nil {
return err
}
idb, err := id.MarshalBinary()
if err != nil {
return err
}
blockedOn, err := q.bucket(tx, q.keys.blockedOn)
if err != nil {
return err
}
blockedMsg, err := blockedOn.CreateBucketIfNotExists(idb)
if err != nil {
return err
}
blocking, err := q.bucket(tx, q.keys.blocking)
if err != nil {
return err
}
for _, id := range on {
idc, err := id.MarshalBinary()
if err != nil {
return err
}
if err := blockedMsg.Put(idc, nil); err != nil {
return err
}
blockerMsg, err := blocking.CreateBucketIfNotExists(idc)
if err != nil {
return err
}
if err := blockerMsg.Put(idb, nil); err != nil {
return err
}
}
waiting, err := q.bucket(tx, q.keys.waiting)
if err != nil {
return err
}
return waiting.Put(idb, msg)
})
}