-
Notifications
You must be signed in to change notification settings - Fork 7
/
long_pull.go
83 lines (74 loc) · 1.73 KB
/
long_pull.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
package aliacm
import (
"context"
"errors"
"net/http"
"net/url"
"strings"
"github.com/xiaojiaoyu100/aliyun-acm/v2/info"
)
const (
wordSeparator = string(rune(2))
lineSeparator = string(rune(1))
)
// InfoParam long pull 参数
type InfoParam struct {
info.Info
ContentMD5 string
}
// LongPull 监听配置
func (d *Diamond) LongPull(infoParams ...InfoParam) ([]info.Info, error) {
ip, err := d.QueryIP()
if err != nil {
return nil, err
}
var longPollRequest struct {
ProbeModifyRequest string `url:"Probe-Modify-Request"`
}
for _, infoParam := range infoParams {
if longPollRequest.ProbeModifyRequest != "" {
longPollRequest.ProbeModifyRequest += lineSeparator
}
longPollRequest.ProbeModifyRequest += strings.Join([]string{infoParam.DataID, infoParam.Group, infoParam.ContentMD5, d.option.tenant}, wordSeparator)
}
headerSetters := []headerSetter{
d.withLongPollingTimeout(),
d.withSignature(d.option.tenant, ""),
}
header := make(http.Header)
for _, setter := range headerSetters {
if err := setter(header); err != nil {
return nil, err
}
}
request := d.c.NewRequest().
WithPath(acmLongPull.String(ip)).
WithFormURLEncodedBody(longPollRequest).
WithHeader(header).
Post()
response, err := d.c.Do(context.TODO(), request)
if err != nil {
return nil, err
}
if !response.Success() {
return nil, errors.New(response.String())
}
var ret []info.Info
s, err := url.QueryUnescape(response.String())
if err != nil {
return nil, errors.New("unescape fail")
}
ss := strings.Split(s, lineSeparator)
for _, s := range ss {
tt := strings.Split(s, wordSeparator)
if len(tt) != 3 {
continue
}
i := info.Info{
DataID: tt[0],
Group: tt[1],
}
ret = append(ret, i)
}
return ret, nil
}