From e770a99f2c45af8d4da4c91ced8d53bbe53b75bc Mon Sep 17 00:00:00 2001 From: Mikalai Konan Date: Fri, 19 Nov 2021 15:21:25 +0300 Subject: [PATCH] fixes for 429 --- README.md | 15 ++++++++++++--- proxy/kucoin/kucoin.go | 43 ++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 291fb8d..0c56538 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ make build "secret": "", "ccxt_config": { "enableRateLimit": false, + "timeout": 60000, "urls": { "api": { "public": "http://127.0.0.1:8080/kucoin", @@ -33,14 +34,17 @@ make build } }, "ccxt_async_config": { - "enableRateLimit": false + "enableRateLimit": false, + "timeout": 60000 } } } ``` ### Docker (suggested way) + ###### Use different tags for different platforms e.g. - main-amd64, main-arm-v6, main-arm-v7, main-arm64 + ``` docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mikekonan/freqtrade-proxy:main-amd64 ``` @@ -55,6 +59,7 @@ docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mik "secret": "", "ccxt_config": { "enableRateLimit": false, + "timeout": 60000, "urls": { "api": { "public": "http://127.0.0.1:8080/kucoin", @@ -63,13 +68,15 @@ docker run --restart=always -p 127.0.0.1:8080:8080 --name freqtrade-proxy -d mik } }, "ccxt_async_config": { - "enableRateLimit": false + "enableRateLimit": false, + "timeout": 60000 } } } ``` ### Docker-compose (best way) + ###### Use different tags for different platforms e.g. - main-amd64, main-arm-v6, main-arm-v7, main-arm64 See example - [docker-compose.yml](docker-compose.yml) @@ -91,6 +98,7 @@ See example - [docker-compose.yml](docker-compose.yml) "secret": "", "ccxt_config": { "enableRateLimit": false, + "timeout": 60000, "urls": { "api": { "public": "http://freqtrade-proxy:8080/kucoin", @@ -99,7 +107,8 @@ See example - [docker-compose.yml](docker-compose.yml) } }, "ccxt_async_config": { - "enableRateLimit": false + "enableRateLimit": false, + "timeout": 60000 } } } diff --git a/proxy/kucoin/kucoin.go b/proxy/kucoin/kucoin.go index 2d6f792..296e5c0 100644 --- a/proxy/kucoin/kucoin.go +++ b/proxy/kucoin/kucoin.go @@ -87,13 +87,13 @@ func New(s *store.Store) *kucoin { instance := &kucoin{ client: fasthttp.Client{}, store: s, - subscriptionManager: &subscriptionManager{clients: nil, rl: ratelimit.New(10), l: new(sync.Mutex)}, + subscriptionManager: &subscriptionManager{clients: nil, rl: ratelimit.New(9), l: new(sync.Mutex)}, } svc := sdk.NewApiService(sdk.ApiKeyVersionOption(sdk.ApiKeyVersionV2)) instance.svc = svc - instance.rl = ratelimit.New(20) + instance.rl = ratelimit.New(15) return instance } @@ -157,6 +157,14 @@ func (ws *ws) serveFor(store *store.Store) { } } +func (kucoin *kucoin) kucoinCodeToHttpCode(str string) int { + if len(str) < 3 { + return 200 + } + + return cast.ToInt(str[:3]) +} + func (kucoin *kucoin) getKlines(pair string, timeframe string, startAt int64, endAt int64, retryCount int) (sdk.KLinesModel, *sdk.ApiResponse, error) { var ( resp *sdk.ApiResponse @@ -170,11 +178,12 @@ func (kucoin *kucoin) getKlines(pair string, timeframe string, startAt int64, en break } - if i == retryCount || resp.Code != "429000" { //This type of Candlestick is currently not provided + logrus.Warn(i) + if i == retryCount { return sdk.KLinesModel{}, resp, err } - time.Sleep(time.Millisecond * 100) + time.Sleep(time.Second) } candlesModel := sdk.KLinesModel{} @@ -251,14 +260,12 @@ func (kucoin *kucoin) Start(port int) { candles := kucoin.store.Get("kucoin", pair, timeframe, startTruncated, endTruncated, kucoin.timeframeToDuration(timeframe)) if len(candles) == 0 { - candlesModel, resp, err := kucoin.getKlines(pair, timeframe, startTruncated.Unix(), endAt, 3) - if err != nil { - logrus.Warn(err) + candlesModel, resp, err := kucoin.getKlines(pair, timeframe, startTruncated.Unix(), endAt, 15) + c.Response.SetStatusCode(kucoin.kucoinCodeToHttpCode(resp.Code)) + c.Response.SetBody((&apiResp{Code: resp.Code, RawData: resp.RawData, Message: resp.Message}).json()) - c.Response.SetStatusCode(200) - c.Response.SetBody((&apiResp{Code: resp.Code, RawData: resp.RawData, Message: resp.Message}).json()) - - return nil + if len(candlesModel) == 0 { + logrus.Warnf("there is no candle data from kucoin for - '%s'", c.Request.RequestURI()) } for _, c := range candlesModel { @@ -267,11 +274,15 @@ func (kucoin *kucoin) Start(port int) { kucoin.store.Store(pc) } - kucoin.subscriptionManager.Subscribe( - kucoin.svc, - sdk.NewSubscribeMessage(fmt.Sprintf("/market/candles:%s_%s", pair, timeframe), false), - kucoin.store, - ) + if err == nil { + go kucoin.subscriptionManager.Subscribe( + kucoin.svc, + sdk.NewSubscribeMessage(fmt.Sprintf("/market/candles:%s_%s", pair, timeframe), false), + kucoin.store, + ) + } + + return nil } _, err := c.Write(candles.KucoinRespJSON())