-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand_put.go
63 lines (49 loc) · 1.21 KB
/
command_put.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
package beanstalk
import (
"fmt"
"strconv"
"strings"
"time"
)
type PutCommand struct {
Priority uint32
Delay time.Duration
TTR time.Duration
Data []byte
}
type PutCommandResponse struct {
ID int
}
func (c PutCommand) CommandLine() string {
return fmt.Sprintf("put %d %0.f %0.f %d", c.Priority, c.Delay.Seconds(), c.TTR.Seconds(), len(c.Data))
}
func (c PutCommand) Body() []byte {
return c.Data
}
func (c PutCommand) HasResponseBody() bool {
return false
}
func (c PutCommand) BuildResponse(responseLine string, _ []byte) (CommandResponse, error) {
switch {
case strings.HasPrefix(responseLine, "INSERTED"):
i := strings.LastIndex(responseLine, " ")
if i == -1 {
return nil, ErrUnexpectedResponse
}
id, err := strconv.Atoi(responseLine[i+1:])
if err != nil {
return nil, err
}
return PutCommandResponse{id}, nil
case strings.HasPrefix(responseLine, "BURIED"):
return nil, ErrBuried
case strings.EqualFold(responseLine, "EXPECTED_CRLF"):
return nil, ErrExpectedCRLF
case strings.EqualFold(responseLine, "JOB_TOO_BIG"):
return nil, ErrJobTooBig
case strings.EqualFold(responseLine, "DRAINING"):
return nil, ErrDraining
default:
return nil, ErrUnexpectedResponse
}
}