-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpq.go
171 lines (155 loc) · 3.36 KB
/
httpq.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Package httpq is a specialization of queue.AsyncQueue for HTTP requests.
package httpq
import (
"bytes"
"encoding/gob"
"errors"
"io"
"net/http"
"net/url"
"time"
"github.com/esote/enc"
"github.com/esote/queue"
)
// Request represents a future HTTP request sent by the queue.
type Request struct {
Body []byte
Method string
URL *url.URL
}
// HTTPQueue sends HTTP requests.
type HTTPQueue interface {
// Add request to the queue. Safe for concurrent use.
Enqueue(req *Request) error
// Close the queue.
io.Closer
}
// Config is used to configure the behaviour of the HTTP queue.
type Config struct {
Workers int
DefaultMaxRetries int
Client *http.Client
Errors chan<- error
}
type request struct {
*Request
Retries int
}
type httpQueue struct {
pass []byte
q queue.AsyncQueue
client *http.Client
maxRetries int
errors chan<- error
}
// New constructs an async HTTP queue. When a nil config is given, reasonable
// defaults will be used. If pass is non-nil, requests stored in the queue are
// encrypted with the password (see github.com/esote/enc for details).
func New(q queue.Queue, pass []byte, cfg *Config) (HTTPQueue, error) {
if q == nil {
return nil, errors.New("httpq: q is nil")
}
if cfg == nil {
cfg = &Config{
Workers: 5,
DefaultMaxRetries: 3,
Client: &http.Client{
Timeout: time.Second,
},
}
} else {
if cfg.Client == nil {
return nil, errors.New("httpq: config has nil Client")
}
if cfg.DefaultMaxRetries < 0 {
cfg.DefaultMaxRetries = 0
}
}
httpq := &httpQueue{
pass: pass,
client: cfg.Client,
maxRetries: cfg.DefaultMaxRetries,
errors: cfg.Errors,
}
async, err := queue.NewAsyncQueue(q, httpq.handler, cfg.Workers)
if err != nil {
return nil, err
}
httpq.q = async
return httpq, nil
}
func (q *httpQueue) Enqueue(req *Request) error {
return q.enqueue(&request{
Request: req,
Retries: q.maxRetries,
})
}
func (q *httpQueue) enqueue(req *request) error {
data, err := q.encode(req)
if err != nil {
return err
}
return q.q.Enqueue(data)
}
func (q *httpQueue) Close() error {
return q.q.Close()
}
// TODO: benchmark encoder/decoder pools vs. only buffer pools vs no pools.
func (q *httpQueue) encode(v interface{}) ([]byte, error) {
if q.pass == nil {
var b bytes.Buffer
if err := gob.NewEncoder(&b).Encode(v); err != nil {
return nil, err
}
return b.Bytes(), nil
}
data, _, err := enc.Encrypt(q.pass, v)
if err != nil {
return nil, err
}
return data, nil
}
func (q *httpQueue) decode(data []byte, v interface{}) error {
if q.pass == nil {
return gob.NewDecoder(bytes.NewReader(data)).Decode(v)
}
return enc.Decrypt(data, q.pass, v)
}
func (q *httpQueue) handler(data []byte, err error) {
if err != nil {
q.log(err)
return
}
var req request
if err = q.decode(data, &req); err != nil {
q.log(err)
return
}
httpReq, err := http.NewRequest(req.Method, req.URL.String(),
bytes.NewReader(req.Body))
if err != nil {
q.log(err)
return
}
resp, err := q.client.Do(httpReq)
if err != nil {
q.log(err)
return
}
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK && req.Retries > 0 {
req.Retries--
if err = q.enqueue(&req); err != nil {
q.log(err)
}
}
return
}
func (q *httpQueue) log(err error) {
if q.errors != nil {
select {
case q.errors <- err:
default:
}
}
}