-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (153 loc) · 3.64 KB
/
main.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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/valyala/fasthttp"
"github.com/k0l1br1/loader/candles"
)
const (
exitOk = 0
exitError = 1
exitInterrupt = 130
)
// max limit load candles 1000
type Candles [1000]candles.Candle
func datePrint(t int64) {
dt := time.UnixMilli(t)
if t == 0 {
os.Stdout.WriteString("no date\n")
return
}
os.Stdout.WriteString(dt.UTC().Format("2006-01-02 15:04:05") + "\n")
}
func errorPrint(err error) {
os.Stderr.WriteString(err.Error() + "\n")
}
func errorWrap(msg string, err error) error {
return fmt.Errorf("%s: %w", msg, err)
}
func hostClient(host string) *fasthttp.HostClient {
isTLS := true
// single HostClient will be enough, so no need to use fasthttp.Client
return &fasthttp.HostClient{
Addr: fasthttp.AddMissingPort(host, isTLS),
// increase DNS cache time to an hour instead of default minute
Dial: (&fasthttp.TCPDialer{
DNSCacheDuration: time.Hour,
}).Dial,
DisableHeaderNamesNormalizing: true,
DisablePathNormalizing: true,
IsTLS: isTLS,
MaxIdleConnDuration: time.Second * 10,
NoDefaultUserAgentHeader: true,
}
}
func run() int {
opts, err := parseOptions(os.Args)
if err != nil {
errorPrint(err)
switch err {
case errReqSymbol, errReqStartTime, errTimeWithoutNew:
return exitOk
default:
return exitError
}
}
var stg *candles.Storage
if opts.IsNew {
stg, err = candles.NewDefaultStorage(opts.Symbol)
if err != nil {
errorPrint(errorWrap("init new storage", err))
return exitError
}
} else {
stg, err = candles.DefaultStorage(opts.Symbol)
if err != nil {
errorPrint(errorWrap("open storage", err))
return exitError
}
}
defer stg.Close()
if opts.ShowStart {
t, err := stg.FirstCandleCloseTime()
if err != nil {
errorPrint(errorWrap("read storage first close time", err))
return exitError
}
datePrint(t)
return exitOk
}
if opts.ShowEnd {
t, err := stg.LastCandleCloseTime()
if err != nil {
errorPrint(errorWrap("read storage last close time", err))
return exitError
}
datePrint(t)
return exitOk
}
t := opts.StartTimestamp
if !opts.IsNew {
t, err = stg.LastCandleCloseTime()
if err != nil {
errorPrint(errorWrap("load last close time from storage", err))
return exitError
}
}
totalCandles, err := stg.SizeCandles()
if err != nil {
errorPrint(errorWrap("get total candles", err))
return exitError
}
q := query{}
q.Init(opts.Symbol)
uri := &fasthttp.URI{}
uri.Parse(nil, []byte(apiUriBase))
req := &fasthttp.Request{}
resp := &fasthttp.Response{}
hc := hostClient(string(uri.Host()))
defer hc.CloseIdleConnections()
intChan := make(chan os.Signal, 1)
signal.Notify(intChan, os.Interrupt, syscall.SIGTERM)
var cs Candles
for {
uri.SetQueryStringBytes(q.QueryStringBytes(t))
// make an inner copy of parsed uri
req.SetURI(uri)
if err = hc.Do(req, resp); err != nil {
errorPrint(errorWrap("api do request", err))
return exitError
}
n, err := parseCandles(resp.Body(), &cs)
if err != nil {
errorPrint(errorWrap("parse candles", err))
return exitError
}
if err = stg.Save(cs[:n]); err != nil {
errorPrint(errorWrap("save candles", err))
return exitError
}
// conver the time of the last candle seconds to milli
t = candles.SecToMilli(cs[n-1].CTime)
totalCandles += int64(n)
fmt.Printf("\b\rloaded %d\n", totalCandles)
if len(cs) > n {
// all done
fmt.Println("All done!")
return exitOk
}
select {
case <-intChan:
fmt.Println("Interrupted!")
return exitInterrupt
default:
// meaning that the selects never block
}
}
}
func main() {
os.Exit(run())
}