forked from segmentio/nsq-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
118 lines (100 loc) · 2.07 KB
/
error.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package nsqlookup
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"time"
)
type Error struct {
Code string
Reason string
}
const (
ErrInvalid = "E_INVALID"
ErrBadTopic = "E_BAD_TOPIC"
ErrBadChannel = "E_BAD_CHANNEL"
ErrBadBody = "E_BAD_BODY"
ErrBadProtocol = "E_BAD_PROTOCOL"
)
func (e Error) Error() string {
return e.Code + " " + e.Reason
}
func (e Error) Status() string {
return e.Code
}
func (e Error) Write(w *bufio.Writer) (err error) {
return writeResponse(w, []byte(e.Error()))
}
func makeErrInvalid(s string, a ...interface{}) Error {
return makeError(ErrInvalid, s, a...)
}
func makeErrBadTopic(s string, a ...interface{}) Error {
return makeError(ErrBadTopic, s, a...)
}
func makeErrBadChannel(s string, a ...interface{}) Error {
return makeError(ErrBadChannel, s, a...)
}
func makeErrBadBody(s string, a ...interface{}) Error {
return makeError(ErrBadBody, s, a...)
}
func makeErrBadProtocol(s string, a ...interface{}) Error {
return makeError(ErrBadTopic, s, a...)
}
func makeError(c string, s string, a ...interface{}) Error {
return Error{
Code: c,
Reason: fmt.Sprintf(s, a...),
}
}
func readError(data []byte) Error {
off := bytes.IndexByte(data, ' ')
if off < 0 {
off = len(data)
}
code, reason := data[:off], data[off+1:]
return Error{
Code: string(code),
Reason: string(reason),
}
}
func isTimeout(err error) bool {
if err == nil {
return false
}
e, ok := err.(interface {
Timeout() bool
})
return ok && e.Timeout()
}
func isTemporary(err error) bool {
if err == nil {
return false
}
e, ok := err.(interface {
Temporary() bool
})
return ok && e.Temporary()
}
func appendError(err error, e error) error {
if err == nil {
return e
}
return errors.New(err.Error() + "; " + e.Error())
}
func backoff(attempt int, max time.Duration) time.Duration {
d := time.Duration(attempt*attempt) * 10 * time.Millisecond
if d > max {
d = max
}
return d
}
func sleep(ctx context.Context, d time.Duration) {
timer := time.NewTimer(d)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
}
}