forked from beanstalkd/go-beanstalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tubeset.go
39 lines (35 loc) · 1.02 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
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
}
// Reserve reserves and returns a job from one of the tubes in t. If no
// job is available before time timeout has passed, Reserve returns a
// ConnError recording ErrTimeout.
//
// Typically, a client will reserve a job, perform some work, then delete
// the job with Conn.Delete.
func (t *TubeSet) Reserve(timeout time.Duration) (id string, body []byte, err error) {
r, err := t.Conn.cmd(nil, t, nil, "reserve-with-timeout", dur(timeout))
if err != nil {
return "", nil, err
}
body, err = t.Conn.readResp(r, true, "RESERVED %s", &id)
if err != nil {
return "", nil, err
}
return id, body, nil
}