bitfinexcom / bitfinex-api-go

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

nonce too small fix #213

Open JokerCatz opened 4 years ago

JokerCatz commented 4 years ago

https://github.com/bitfinexcom/bitfinex-api-go/blame/4d37e8d4be18f16ab50082b0c2c080542225f4a0/utils/nonce.go#L30

nonce: uint64(time.Now().Unix()) * 1000,
// time.now.second x 1000 != millisecond
// I think it mean
nonce: uint64(time.Now().UnixNano()) / 10e5,

if I new the client obj every time , the nonce will be same of same "second" , the conn obj init nonce will be same ...

so I think just use nano second be nonce will fix this

type CustomNonceGenerator struct {
}
func (u *CustomNonceGenerator) GetNonce() string {
    return strconv.FormatUint(uint64(time.Now().UnixNano()), 10)
   // or same code : millisecond
   // return strconv.FormatUint(uint64(time.Now().UnixNano()) / 10e5, 10)
}
// ...
func{
    conn := bitfinexRest.NewClientWithURLNonce("https://api.bitfinex.com/v2/", &CustomNonceGenerator{}).Credentials(...)
}

or just fix source code , init conn nonce be nano second

nonce: uint64(time.Now().UnixNano()),

but multi conn obj will be nonce collision ...

or use a global var + lock to increase it?

calj commented 3 years ago

We had this problem while doing parallel requests to the rest API. It appears when requests arrive in different orders to the server.

We quick fixed by introducing a lock in our code to prevent parallel rest calls.

Eventually we could introduce this lock in the library. An other approach would be to change the API to async, the lib would push requests in order using a channel, a single go routine would make all rest calls, on response the library would trigger a callback specified by the caller.