forked from fwtpe/owl-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender.go
178 lines (153 loc) · 3.97 KB
/
sender.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
package sender
import (
"bytes"
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"net/http"
"time"
cmodel "github.com/fwtpe/owl-backend/common/model"
tsema "github.com/toolkits/concurrent/semaphore"
"github.com/toolkits/container/nmap"
thttpclient "github.com/toolkits/http/httpclient"
ttime "github.com/toolkits/time"
"github.com/fwtpe/owl-backend/modules/nodata/config"
"github.com/fwtpe/owl-backend/modules/nodata/g"
)
var (
MockMap = nmap.NewSafeMap()
sema = tsema.NewSemaphore(1)
)
func Start() {
if !g.Config().Sender.Enabled {
log.Println("sender.Start warning, not enabled")
return
}
startGaussCron()
log.Println("sender.Start ok")
}
func AddMock(key string, endpoint string, metric string, tags string, ts int64, dstype string, step int64, value interface{}) {
item := &cmodel.JsonMetaData{metric, endpoint, ts, step, value, dstype, tags}
MockMap.Put(key, item)
}
func SendMockOnceAsync() {
go SendMockOnce()
}
func SendMockOnce() int {
if !sema.TryAcquire() {
return -1
}
defer sema.Release()
// not enabled
if !g.Config().Sender.Enabled {
return 0
}
start := time.Now().Unix()
cnt, _ := sendMock()
end := time.Now().Unix()
if g.Config().Debug {
log.Printf("sender cron, cnt %d, time %ds, start %s", cnt, end-start, ttime.FormatTs(start))
}
// statistics
g.SenderCronCnt.Incr()
g.SenderLastTs.SetCnt(end - start)
g.SenderCnt.IncrBy(int64(cnt))
return cnt
}
func sendMock() (cnt int, errt error) {
// check nodata flood
cfgsize := config.Size()
ndsize := MockMap.Size()
if cfgsize < 1 {
return
}
rate := int32(100 * ndsize / cfgsize)
threshold := getThreshold()
if g.Config().Debug {
log.Printf("nodata threshold: %d", threshold)
}
// statistics
g.FloodRate.SetCnt(int64(rate))
g.Threshold.SetCnt(int64(threshold))
if g.Config().Sender.Block.Enabled {
if rate > threshold || g.Config().Sender.Block.SetBlock { // nodata flooding, blocking
log.Printf("nodata blocking: flood rate %d, threshold %d", int(rate), int(threshold))
// statistics
g.Blocking.SetCnt(1)
// clear send buffer
MockMap.Clear()
return 0, nil
}
}
// statistics
g.Blocking.SetCnt(0)
cfg := g.Config().Sender
batch := int(cfg.Batch)
connTimeout := cfg.ConnectTimeout
requTimeout := cfg.RequestTimeout
// send mock to transfer
mocks := MockMap.Slice()
MockMap.Clear()
mockSize := len(mocks)
i := 0
for i < mockSize {
leftLen := mockSize - i
sendSize := batch
if leftLen < sendSize {
sendSize = leftLen
}
fetchMocks := mocks[i : i+sendSize]
i += sendSize
items := make([]*cmodel.JsonMetaData, 0)
for _, val := range fetchMocks {
if val == nil {
continue
}
items = append(items, val.(*cmodel.JsonMetaData))
}
cntonce, err := sendItemsToTransfer(items, len(items), "nodata.mock",
time.Millisecond*time.Duration(connTimeout),
time.Millisecond*time.Duration(requTimeout))
if err == nil {
if g.Config().Debug {
log.Println("send items:", items)
}
cnt += cntonce
}
}
return cnt, nil
}
//
func sendItemsToTransfer(items []*cmodel.JsonMetaData, size int, httpcliname string,
connT, reqT time.Duration) (cnt int, errt error) {
if size < 1 {
return
}
cfg := g.Config()
transUlr := fmt.Sprintf("http://%s/api/push", cfg.Sender.TransferAddr)
hcli := thttpclient.GetHttpClient(httpcliname, connT, reqT)
// form request args
itemsBody, err := json.Marshal(items)
if err != nil {
log.Println(transUlr+", format body error,", err)
errt = err
return
}
// fetch items
req, err := http.NewRequest("POST", transUlr, bytes.NewBuffer(itemsBody))
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
req.Header.Set("Connection", "close")
postResp, err := hcli.Do(req)
if err != nil {
log.Println(transUlr+", post to dest error,", err)
errt = err
return
}
defer postResp.Body.Close()
if postResp.StatusCode/100 != 2 {
log.Println(transUlr+", post to dest, bad response,", postResp.Body)
errt = fmt.Errorf("request failed, %s", postResp.Body)
return
}
return size, nil
}