-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelay.go
More file actions
57 lines (53 loc) · 1.24 KB
/
delay.go
File metadata and controls
57 lines (53 loc) · 1.24 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
package lasr
import (
"bytes"
"fmt"
"time"
"github.com/boltdb/bolt"
)
var (
// MaxDelayTime is the maximum time that can be passed to Q.Delay().
MaxDelayTime = time.Unix(0, 1<<63-1)
)
// Delay is like Send, but the message will not enter the Ready state until
// after "when" has occurred.
//
// If "when" has already occurred, then it will be set to time.Now().
func (q *Q) Delay(message []byte, when time.Time) (ID, error) {
if when.After(MaxDelayTime) {
return nil, fmt.Errorf("time out of range: %s", when.Format(time.RFC3339))
}
if when.Before(time.Now()) {
when = time.Now()
}
id := Uint64ID(when.UnixNano())
key, err := id.MarshalBinary()
if err != nil {
return nil, err
}
err = q.db.Update(func(tx *bolt.Tx) error {
bucket, err := q.bucket(tx, q.keys.delayed)
if err != nil {
return err
}
// Reserve a spot for the message. If its exact time in unix
// nanoseconds has already been reserved, pick the next spot,
// ad-infinitum.
for {
k, _ := bucket.Cursor().Seek(key)
if !bytes.Equal(k, key) {
break
}
id++
key, err = id.MarshalBinary()
if err != nil {
return err
}
}
return bucket.Put(key, message)
})
if err == nil {
q.waker.WakeAt(time.Unix(0, int64(id)))
}
return id, err
}