-
Notifications
You must be signed in to change notification settings - Fork 0
/
tubeset.go
53 lines (48 loc) · 1.44 KB
/
tubeset.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
package beanstalk
import (
"time"
)
// TubeSet represents a set of tubes on the server connected to by Conn.
// Name names the tubes represented.
type TubeSet struct {
Conn *Conn
Name map[string]bool
}
// NewTubeSet returns a new TubeSet representing the given names.
func NewTubeSet(c *Conn, name ...string) *TubeSet {
ts := &TubeSet{c, make(map[string]bool)}
for _, s := range name {
ts.Name[s] = true
}
return ts
}
// ReserveWithTimeout reserves and returns a job from one of the tubes in t. If
// no job is available before time timeout has passed, ReserveWithTimeout returns
// a ConnError recording ErrTimeout.
func (t *TubeSet) ReserveWithTimeout(timeout time.Duration) (id uint64, body []byte, err error) {
r, err := t.Conn.cmd(nil, t, nil, "reserve-with-timeout", dur(timeout))
if err != nil {
return 0, nil, err
}
body, err = t.Conn.readResp(r, true, "RESERVED %d", &id)
if err != nil {
return 0, nil, err
}
return id, body, nil
}
// Reserve reserves and returns a job from one of the tubes in t. Reserve
// blocks until a job is available for reservation.
//
// Typically, a client will reserve a job, perform some work, then delete
// the job with Conn.Delete.
func (t *TubeSet) Reserve() (id uint64, body []byte, err error) {
r, err := t.Conn.cmd(nil, t, nil, "reserve")
if err != nil {
return 0, nil, err
}
body, err = t.Conn.readResp(r, true, "RESERVED %d", &id)
if err != nil {
return 0, nil, err
}
return id, body, nil
}