Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttling api requests. Reverting log change #1562

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions pokemongo_bot/api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from pgoapi import PGoApi
from pgoapi.exceptions import NotLoggedInException
from human_behaviour import sleep
import time
import logger

class ApiWrapper(object):
def __init__(self, api):
self._api = api
self.request_callers = []
self.reset_auth()
self.last_api_request_time = None
self.requests_per_seconds = 2

def reset_auth(self):
self._api._auth_token = None
Expand Down Expand Up @@ -54,22 +57,26 @@ def call(self, max_retry=5):
if not self._can_call():
return False # currently this is never ran, exceptions are raised before

request_timestamp = None

api_req_method_list = self._api._req_method_list
result = None
try_cnt = 0
while True:
request_timestamp = self.throttle_sleep()
self._api._req_method_list = [req_method for req_method in api_req_method_list] # api internally clear this field after a call
result = self._api.call()
if not self._is_response_valid(result, request_callers):
try_cnt += 1
if try_cnt > 1:
logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')

logger.log('Server seems to be busy or offline - try again - {}/{}'.format(try_cnt, max_retry), 'red')

if try_cnt >= max_retry:
raise ServerBusyOrOfflineException()
sleep(1)
else:
break

self.last_api_request_time = request_timestamp
return result

def login(self, provider, username, password):
Expand All @@ -82,5 +89,14 @@ def __getattr__(self, func):
self.request_callers.append(func)
return getattr(self._api, func)

def throttle_sleep(self):
now_milliseconds = time.time() * 1000
required_delay_between_requests = 1000 / self.requests_per_seconds

difference = now_milliseconds - (self.last_api_request_time if self.last_api_request_time else 0)

if (self.last_api_request_time != None and difference < required_delay_between_requests):
sleep_time = required_delay_between_requests - difference
time.sleep(sleep_time / 1000)

return now_milliseconds