bitfinexcom / bitfinex-api-go

BITFINEX Go trading API - Bitcoin, Litecoin, and Ether exchange
https://www.bitfinex.com/
MIT License
306 stars 221 forks source link

Duplicate write onchannel from websocket #133

Open saniales opened 5 years ago

saniales commented 5 years ago

I signal here that using websocket to retrieve data (V1) something strange happens

// api is a bitfinex V1 client
api.WebSocket.AddSubscribe(bitfinex.ChanTicker, tickerKey, tickers)
handleTicker := func(results <-chan []float64, tickerKey string) {
        for {
            values, stillOpen := <-results
            if !stillOpen {
                return
            }
                        fmt.Println(values, tickerKey)
                        // ... other code
        }
    }
        go handleTicker(tickers, tickerKey)

        go api.Subscribe()

Here the output

[0.0085788 693.42706957 0.0085809 807.79752121 -0.0001808 -0.0207 0.0085692 24460.81097189 0.008895 0.0085195] LTCBTC
[0.0085809 807.79752121 -0.0001808 -0.0207 0.0085692 24460.81097189 0.008895 0.0085195] LTCBTC

[0.032302 351.59487017 0.032315 242.43108078 -0.003122 -0.0881 0.032315 35404.67950253 0.035663 0.031924] ETHBTC
[0.032315 242.43108078 -0.003122 -0.0881 0.032315 35404.67950253 0.035663 0.031924] ETHBTC

[0.020202 825.36196188 0.020237 1068.59876956 0.001134 0.0595 0.0202 22745.26364041 0.022298 0.019021] ZECBTC
[0.020237 1068.59876956 0.001134 0.0595 0.0202 22745.26364041 0.022298 0.019021] ZECBTC

[0.0016445 4739.97658367 0.0016459 3647.27738417 -3.23e-05 -0.0192 0.0016467 20592.09547885 0.0016937 0.0016419] ETCBTC
[0.0016459 3647.27738417 -3.23e-05 -0.0192 0.0016467 20592.09547885 0.0016937 0.0016419] ETCBTC

[3.636e-05 604689.79599884 3.658e-05 214247.56797288 -2.7e-06 -0.027 3.654e-05 576044.754326 4.159e-05 3.641e-05] XLMBTC
[3.658e-05 214247.56797288 -2.7e-06 -0.027 3.654e-05 576044.754326 4.159e-05 3.641e-05] XLMBTC

Let's take first couple of lines

[0.0085788 693.42706957 0.0085809 807.79752121 -0.0001808 -0.0207 0.0085692 24460.81097189 0.008895 0.0085195] LTCBTC
[0.0085809 807.79752121 -0.0001808 -0.0207 0.0085692 24460.81097189 0.008895 0.0085195] LTCBTC

The first two elements of the array (got from the channel) are missing the call of handleDataMessage in the library fills the channel twice, and removes the first two elements the second time it pushes into the chan.

Please solve this or explain if this is a normal behaviour, thanks

saniales commented 5 years ago

UPDATE: the same problem seem to be encountered also by subscribing to orderbook

[0.001642 2 54.01921392]
[0.0016403 1 101.73380781]
[0.0016402 1 70]
[0.0016401 1 1]
[0.00164 3 5.425]
[0.001639 1 10]
[0.0016387 1 235]
[0.0016386 1 5.66773013]
[0.0016384 1 2539.999]
[0.001638 1 20]
[0.0016376 1 60.99420555]
[0.0016372 1 30]
[0.0016371 2 69.10058896]
[0.0016369 1 24.750402]
[0.0016366 2 182.96402085]
[0.0016358 1 273.4215416]
[0.0016356 4 769.83048073]
[0.0016355 1 304.83379453]
[0.0016354 1 49.48639335]
[0.0016353 1 100]
[0.0016351 3 427.08876288]
[0.001635 2 184.29517357]
[0.0016347 2 527.3700244]
[0.0016346 1 487.9536444]
[0.0016341 1 487.99233852]
[0.0016421 5 -142.29863851]
[0.0016423 1 -60.9570253]
[0.0016426 1 -304.83513425]
[0.0016428 1 -1.1]
[0.0016429 1 -61.00220523]
[0.001643 1 -240.34083171]
[0.0016431 1 -121.99808464]
[0.0016432 1 -30]
[0.0016433 2 -487.8986083]
[0.0016438 1 -32]
[0.0016442 1 -160]
[0.0016444 1 -243.74449125]
[0.0016446 1 -488.06676754]
[0.0016456 1 -107.6299756]
[0.0016457 1 -18.2940773]
[0.0016463 1 -243.71745655]
[0.0016465 1 -243.99616927]
[0.0016467 2 -35.49551]
[0.0016468 1 -18.16454]
[0.0016469 1 -95]
[0.0016477 2 -35.80481024]
[0.0016481 1 -2539.999]
[0.0016486 1 -100]
[0.0016489 1 -350]
[0.0016497 1 -12]
[0.0016423 2 -123.1270253]
[-123.1270253]

so i suggest to revise this piece of code

// from 
func (w *WebSocketService) handleDataMessage(msg []byte) {
// ...
else {
            itemsSlice := fullPayload[1]
            i, _ := json.Marshal(itemsSlice)
            var items [][]float64
            err = json.Unmarshal(i, &items)
            if err == nil {
                chanID := fullPayload[0].(float64)
                for _, v := range items {
                    w.chanMap[chanID] <- v
                }
            }
//...
}