generated from andskur/go-microservice-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwriter.go
59 lines (50 loc) · 1.68 KB
/
writer.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
package service
import (
"fmt"
"math/big"
"time"
"quasar-evm/pkg/wsClient"
)
// runWriter is used to run writer
func (s *coinAVGPriceManager) runWriter() {
logInfo("start", "Writer")
go s.listenAndSendARGPrice(s.tokenNames(), s.id, s.stream, s.stopWriter)
if err := s.subscribeCoinAveragePrice(s.tokenNames(), s.id, s.stream); err != nil {
s.close()
}
}
// subscribeCoinAveragePrice is used to send subscribe message to the ws server
func (s *coinAVGPriceManager) subscribeCoinAveragePrice(tokens []string, id int, stream chan *wsClient.CoinAveragePriceStream) error {
if err := s.wsClient.SubscribeCoinAveragePrice(tokens, id, stream); err != nil {
time.Sleep(time.Second * 5)
return s.subscribeCoinAveragePrice(tokens, id, stream)
}
return nil
}
// listenAndSendARGPrice is used to listen to the CoinAveragePriceStream chanel and write prices to the
// coinAVGPriceManager. Will resubscribe automatically if needed.
func (s *coinAVGPriceManager) listenAndSendARGPrice(tokens []string, id int, stream chan *wsClient.CoinAveragePriceStream, stopWriter chan struct{}) {
for {
select {
case <-stopWriter:
logInfo("stop...", "Writer")
return
case <-s.resubscribe:
if err := s.subscribeCoinAveragePrice(tokens, id, stream); err != nil {
logErr(fmt.Sprintln("err resubscribe:", err.Error()), "Writer")
return
}
case data := <-stream:
logInfo(fmt.Sprintf("received data on the %s coin", data.Coin), "Writer")
for _, t := range s.tokens {
if data.Coin == t.Symbol {
price := big.NewFloat(data.Value)
price = price.Mul(price, big.NewFloat(100000))
integer, _ := price.Int64()
s.prices.Store(t, big.NewInt(integer))
break
}
}
}
}
}