-
Notifications
You must be signed in to change notification settings - Fork 21
/
sub.go
183 lines (160 loc) · 3.99 KB
/
sub.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
172
173
174
175
176
177
178
179
180
181
182
183
package api
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/funkygao/gafka/cmd/kateway/gateway"
"github.com/funkygao/gafka/sla"
"github.com/funkygao/gorequest"
)
type SubOption struct {
AppId string
Topic, Ver string
Group string
Batch int
Reset string // newest | oldest
Shadow string
Wait string
Tag string // tag filter
AutoClose bool
Mux bool
}
type SubHandler func(statusCode int, msg []byte) error
func (this *Client) Sub(opt SubOption, h SubHandler) error {
var u url.URL
u.Scheme = this.cf.Sub.Scheme
u.Host = this.cf.Sub.Endpoint
u.Path = fmt.Sprintf("/v1/msgs/%s/%s/%s", opt.AppId, opt.Topic, opt.Ver)
q := u.Query()
q.Set("group", opt.Group)
if opt.Shadow != "" && sla.ValidateShadowName(opt.Shadow) {
q.Set("use", opt.Shadow)
}
if opt.Reset != "" {
q.Set("reset", opt.Reset)
}
if opt.Batch > 1 {
q.Set("batch", strconv.Itoa(opt.Batch))
}
if opt.Wait != "" {
q.Set("wait", opt.Wait)
}
if opt.Mux {
q.Set("mux", "1")
}
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
if opt.AutoClose {
req.Close = true
}
req.Header.Set(gateway.HttpHeaderAppid, this.cf.AppId)
req.Header.Set(gateway.HttpHeaderSubkey, this.cf.Secret)
if opt.Tag != "" {
req.Header.Set(gateway.HttpHeaderMsgTag, opt.Tag)
}
for {
response, err := this.subConn.Do(req)
if err != nil {
return err
}
b, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if this.cf.Debug {
log.Printf("--> [%s]", response.Status)
log.Printf("Partition:%s Offset:%s",
response.Header.Get("X-Partition"),
response.Header.Get("X-Offset"))
}
// reuse the connection
response.Body.Close()
if err = h(response.StatusCode, b); err != nil {
if err == ErrSubStop {
return nil
}
return err
}
}
}
type SubXHandler func(statusCode int, msg []byte, r *SubXResult) error
type SubXResult struct {
Bury string
Offset string
Partition string
Tag string
}
func (this *SubXResult) Reset() {
this.Bury = ""
}
// SubX is advanced Sub with features of delayed ack and shadow bury.
func (this *Client) SubX(opt SubOption, h SubXHandler) error {
var u url.URL
u.Scheme = this.cf.Sub.Scheme
u.Host = this.cf.Sub.Endpoint
u.Path = fmt.Sprintf("/v1/msgs/%s/%s/%s", opt.AppId, opt.Topic, opt.Ver)
q := u.Query()
q.Set("group", opt.Group)
q.Set("ack", "1")
if opt.Shadow != "" && sla.ValidateShadowName(opt.Shadow) {
q.Set("q", opt.Shadow)
}
if opt.Reset != "" {
q.Set("reset", opt.Reset)
}
if opt.Batch > 1 {
q.Set("batch", strconv.Itoa(opt.Batch))
}
if opt.Wait != "" {
q.Set("wait", opt.Wait)
}
u.RawQuery = q.Encode()
req := gorequest.New()
req.Get(u.String()).
Set(gateway.HttpHeaderAppid, this.cf.AppId).
Set(gateway.HttpHeaderSubkey, this.cf.Secret).
Set("User-Agent", UserAgent).
Set(gateway.HttpHeaderPartition, "-1").
Set(gateway.HttpHeaderOffset, "-1")
if opt.Tag != "" {
req.Set(gateway.HttpHeaderMsgTag, opt.Tag)
}
r := &SubXResult{}
for {
response, b, errs := req.EndBytes()
if len(errs) > 0 {
return errs[0]
}
// reset the request header
req.Set(gateway.HttpHeaderPartition, "")
req.Set(gateway.HttpHeaderOffset, "")
req.Set(gateway.HttpHeaderMsgBury, "")
if this.cf.Debug {
log.Printf("--> [%s]", response.Status)
log.Printf("Partition:%s Offset:%s",
response.Header.Get(gateway.HttpHeaderPartition),
response.Header.Get(gateway.HttpHeaderOffset))
}
r.Reset()
r.Partition = response.Header.Get(gateway.HttpHeaderPartition)
r.Offset = response.Header.Get(gateway.HttpHeaderOffset)
r.Tag = response.Header.Get(gateway.HttpHeaderMsgTag)
if err := h(response.StatusCode, b, r); err != nil {
return err
}
req.Set(gateway.HttpHeaderPartition, r.Partition)
req.Set(gateway.HttpHeaderOffset, r.Offset)
if r.Bury != "" {
if r.Bury != ShadowRetry && r.Bury != ShadowDead {
return ErrInvalidBury
}
req.Set("X-Bury", r.Bury)
}
}
}