/
main.go
141 lines (134 loc) · 3.31 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
package main
import (
"context"
"encoding/json"
"github.com/bitfinexcom/bitfinex-api-go/v2"
"log"
"sync"
"time"
)
// Bfx is bitfinex wrapper client
type Bfx struct {
// Symbols are the ticker pairs you want to subscribe
Symbols []string
data Data
orderbooks map[string]*Orderbook
tickerDone chan struct{}
orderbookDone chan struct{}
}
// Data is tickers data for redis
type Data struct {
mu *sync.Mutex
// OK is if the data is available
Ok bool
// Tickers is the ticker data
Tickers map[string][][]float64
// Last is the last price map
Last map[string]float64
}
// NewBfx create a Bfx instance
func NewBfx(Symbols []string, tickerDone, orderbookDone chan struct{}) *Bfx {
data := Data{
mu: new(sync.Mutex),
Ok: false,
Tickers: map[string][][]float64{},
Last: map[string]float64{},
}
orderbooks := make(map[string]*Orderbook)
for _, symbol := range Symbols {
orderbook := &Orderbook{
mu: new(sync.Mutex),
Symbol: symbol,
Bid: SortableBook{},
Ask: SortableBook{},
Ok: false,
bidMap: make(map[float64][]float64),
askMap: make(map[float64][]float64),
}
orderbooks[symbol] = orderbook
}
b := &Bfx{Symbols: Symbols, data: data, orderbooks: orderbooks, tickerDone: tickerDone, orderbookDone: orderbookDone}
return b
}
// RunTicker can start the ticker handler
func (bfx *Bfx) RunTicker() {
c := bitfinex.NewClient()
err := c.Websocket.Connect()
if err != nil {
bfx.data.Ok = false
bfx.tickerDone <- struct{}{}
return
}
c.Websocket.SetReadTimeout(time.Second * 10)
c.Websocket.AttachEventHandler(func(ev interface{}) {
log.Printf("EVENT: %#v", ev)
})
c.Websocket.AttachEventHandler(func(ev interface{}) {
log.Printf("EVENT: %#v", ev)
})
for _, symbol := range bfx.Symbols {
go func(symbol string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
msg := &bitfinex.PublicSubscriptionRequest{
Event: "subscribe",
Channel: bitfinex.ChanTicker,
Symbol: bitfinex.TradingPrefix + symbol,
}
h := bfx.createTickerHandler(symbol)
err = c.Websocket.Subscribe(ctx, msg, h)
if err != nil {
bfx.data.Ok = false
bfx.tickerDone <- struct{}{}
return
}
}(symbol)
}
for {
select {
case <-c.Websocket.Done():
log.Printf("channel closed: %s", c.Websocket.Err())
bfx.data.Ok = false
bfx.tickerDone <- struct{}{}
return
}
}
}
func (bfx *Bfx) createTickerHandler(symbol string) func(ev interface{}) {
return func(ev interface{}) {
t, ok := ev.([][]float64)
if ok {
last := t[0][6]
bfx.data.mu.Lock()
bfx.data.Tickers[symbol] = t
bfx.data.Last[symbol] = last
bfx.data.Ok = true
d, _ := json.Marshal(bfx.data)
redis.HSet(KEY, TICKER, string(d))
bfx.data.mu.Unlock()
log.Printf("PUBLIC MSG %s: %#v", symbol, last)
} else {
//log.Printf("PUBLIC MSG HEARTBEAT %s: %#v", symbol, ev)
}
}
}
func main() {
//b := NewBfx(Symbols)
//go b.run()
reconnectInterval := time.Second * 2
tickerDone := make(chan struct{})
orderbookDone := make(chan struct{})
b := NewBfx([]string{"BTCUSD", "LTCUSD"}, tickerDone, orderbookDone)
go b.RunTicker()
go b.RunOrderbook()
for {
select {
case <-tickerDone:
time.Sleep(reconnectInterval)
go b.RunTicker()
case <-orderbookDone:
time.Sleep(reconnectInterval)
go b.RunOrderbook()
}
}
}