-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttp_saver.go
More file actions
75 lines (69 loc) · 1.99 KB
/
Copy pathhttp_saver.go
File metadata and controls
75 lines (69 loc) · 1.99 KB
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
package main
import (
"encoding/json"
log "github.com/cihub/seelog"
"github.com/golang/snappy"
"github.com/parnurzeal/gorequest"
"time"
)
type HttpSaver struct {
appConfig *AppConfig
compress bool
}
func (s *HttpSaver) SaveTransactionList(transactionList []Transaction) (int64, error) {
var batchTransactionList []Transaction
var resize uint64 = s.appConfig.BatchSize
for _, transaction := range transactionList {
if resize == 0 {
for _, endpoint := range s.appConfig.SubscribeEndpointList {
s.PostTransactionList(endpoint, batchTransactionList)
}
//reset
batchTransactionList = nil
resize = s.appConfig.BatchSize
}
batchTransactionList = append(batchTransactionList, transaction)
resize--
}
//process transaction list left
if len(batchTransactionList) > 0 {
for _, endpoint := range s.appConfig.SubscribeEndpointList {
s.PostTransactionList(endpoint, batchTransactionList)
}
}
return int64(len(transactionList)), nil
}
func (s *HttpSaver) PostTransactionList(endpoint string, transactionList []Transaction) (int64, error) {
marshal, _ := json.Marshal(transactionList)
if s.compress {
marshal = snappy.Encode(nil, marshal)
}
resp, body, errs := gorequest.New().
Timeout(time.Second * 10).
Post(endpoint).
Type("text").
SendString(string(marshal)).
End()
if errs != nil && len(errs) > 0 && errs[0] != nil {
var err error
for _, err = range errs {
log.Errorf("request %v error, url %v, error %v", transactionList, endpoint, err)
}
return -1, err
} else if resp.StatusCode != 200 {
log.Errorf("request %v invalid, url %v, body %v", transactionList, endpoint, body)
return -1, nil
}
log.Debugf("request %v response body %v", endpoint, body)
type Result struct {
Result int64 `json:"result"`
Message string `json:"message"`
Data int64 `json:"data"`
}
result := Result{}
if err := json.Unmarshal([]byte(body), &result); err != nil {
log.Errorf("unmarshal eth body %v error, %v", body, err)
return -1, err
}
return result.Data, nil
}