From 97a5cd8207a5471e1cf02cb7d3bd7597f237f375 Mon Sep 17 00:00:00 2001 From: Aldash Biibosunov Date: Mon, 14 Jun 2021 18:06:22 +0800 Subject: [PATCH 1/4] Throttle when hitting API rate limits --- binance/client.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/binance/client.py b/binance/client.py index e9f5a542a..fefa0e7fa 100755 --- a/binance/client.py +++ b/binance/client.py @@ -1,3 +1,4 @@ +import sys from typing import Dict, Optional, List, Tuple import aiohttp @@ -312,6 +313,22 @@ def _request(self, method, uri: str, signed: bool, force_params: bool = False, * kwargs = self._get_request_kwargs(method, signed, force_params, **kwargs) self.response = getattr(self.session, method)(uri, **kwargs) + + status_code = self.response.status_code + used_weight = int(self.response.headers.get('x-mbx-used-weight', 0)) + + if used_weight > 1150: + sys.stdout.write( + f'Request weight might exceed limit: {used_weight} out of 1200. Waiting for 1 min...\n' + ) + time.sleep(61) # blocking + + if status_code in (418, 429): + sys.stdout.write( + f'[{status_code}] Binance is overloaded or IP is banned. Waiting for 30 min...\n' + ) + time.sleep(60 * 30 + 1) # blocking + return self._handle_response(self.response) @staticmethod @@ -6507,6 +6524,21 @@ async def _handle_response(self, response: aiohttp.ClientResponse): Raises the appropriate exceptions when necessary; otherwise, returns the response. """ + status_code = response.status_code + used_weight = int(response.headers.get('x-mbx-used-weight', 0)) + + if used_weight > 1150: + sys.stdout.write( + f'Request weight might exceed limit: {used_weight} out of 1200. Waiting for 1 min...\n' + ) + time.sleep(61) # blocking + + if status_code in (418, 429): + sys.stdout.write( + f'[{status_code}] Binance is overloaded or IP is banned. Waiting for 30 min...\n' + ) + time.sleep(60 * 30 + 1) # blocking + if not str(response.status).startswith('2'): raise BinanceAPIException(response, response.status, await response.text()) try: From d788ab745d5b58c3ad3da00b1a0809b88cad39bc Mon Sep 17 00:00:00 2001 From: Aldash Biibosunov Date: Mon, 14 Jun 2021 18:14:59 +0800 Subject: [PATCH 2/4] Update client.py --- binance/client.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/binance/client.py b/binance/client.py index fefa0e7fa..f8fff0cba 100755 --- a/binance/client.py +++ b/binance/client.py @@ -6524,7 +6524,6 @@ async def _handle_response(self, response: aiohttp.ClientResponse): Raises the appropriate exceptions when necessary; otherwise, returns the response. """ - status_code = response.status_code used_weight = int(response.headers.get('x-mbx-used-weight', 0)) if used_weight > 1150: @@ -6533,9 +6532,9 @@ async def _handle_response(self, response: aiohttp.ClientResponse): ) time.sleep(61) # blocking - if status_code in (418, 429): + if response.status in (418, 429): sys.stdout.write( - f'[{status_code}] Binance is overloaded or IP is banned. Waiting for 30 min...\n' + f'[{response.status}] Binance is overloaded or IP is banned. Waiting for 30 min...\n' ) time.sleep(60 * 30 + 1) # blocking From 261b873a59a8ea6d4b548727b8aaf8f81ec72219 Mon Sep 17 00:00:00 2001 From: Aldash Biibosunov Date: Thu, 22 Jul 2021 20:01:57 +0800 Subject: [PATCH 3/4] Use ujson instead of native json lib --- binance/client.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/binance/client.py b/binance/client.py index f8fff0cba..190213d7a 100755 --- a/binance/client.py +++ b/binance/client.py @@ -10,6 +10,7 @@ from operator import itemgetter from urllib.parse import urlencode +import ujson from .helpers import interval_to_milliseconds, convert_ts_str from .exceptions import BinanceAPIException, BinanceRequestException, NotImplementedException @@ -6502,7 +6503,8 @@ def _init_session(self) -> aiohttp.ClientSession: session = aiohttp.ClientSession( loop=self.loop, - headers=self._get_headers() + headers=self._get_headers(), + json_serialize=ujson.dumps ) return session From 84a21eb98e7c24bce834d4b81065e78b45c351a5 Mon Sep 17 00:00:00 2001 From: Aldash Biibosunov Date: Thu, 22 Jul 2021 20:56:59 +0800 Subject: [PATCH 4/4] Remove ujson --- binance/client.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/binance/client.py b/binance/client.py index 190213d7a..bf5e25877 100755 --- a/binance/client.py +++ b/binance/client.py @@ -10,8 +10,6 @@ from operator import itemgetter from urllib.parse import urlencode -import ujson - from .helpers import interval_to_milliseconds, convert_ts_str from .exceptions import BinanceAPIException, BinanceRequestException, NotImplementedException from .enums import HistoricalKlinesType @@ -6503,8 +6501,7 @@ def _init_session(self) -> aiohttp.ClientSession: session = aiohttp.ClientSession( loop=self.loop, - headers=self._get_headers(), - json_serialize=ujson.dumps + headers=self._get_headers() ) return session