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

Implement backoff #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions sumologic/backoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import requests
import logging
import time

logger = logging.getLogger('sumologic.backoff')

MAX_TRIES = 8

def backoff(func):
def limited(*args, **kwargs):
delay = 0.1
tries = 0
lastException = None
while tries < MAX_TRIES:
tries += 1
try:
return func(*args, **kwargs)
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429: # rate limited
logging.debug("Rate limited, sleeping for {0}s".format(delay))
time.sleep(delay)
delay *= 2
lastException = e
continue
else:
raise
logging.debug("Rate limited function still failed after {0} retries.".format(MAX_TRIES))
raise lastException

return limited
11 changes: 8 additions & 3 deletions sumologic/sumologic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import logging
import requests
from .backoff import backoff

try:
import cookielib
Expand All @@ -20,7 +21,7 @@ def __init__(self, accessId, accessKey, endpoint=None, cookieFile='cookies.txt')
self.endpoint = self._get_endpoint()
else:
self.endpoint = endpoint
if endpoint[-1:] == "/":
if self.endpoint[-1:] == "/":
raise Exception("Endpoint should not end with a slash character")

def _get_endpoint(self):
Expand All @@ -41,32 +42,36 @@ def _get_endpoint(self):
endpoint = self.response.url.replace('/collectors', '') # dirty hack to sanitise URI and retain domain
return endpoint

@backoff
def delete(self, method, params=None):
r = self.session.delete(self.endpoint + method, params=params)
if 400 <= r.status_code < 600:
r.reason = r.text
r.raise_for_status()
return r

@backoff
def get(self, method, params=None):
r = self.session.get(self.endpoint + method, params=params)
if 400 <= r.status_code < 600:
r.reason = r.text
r.raise_for_status()
return r

@backoff
def post(self, method, params, headers=None):
r = self.session.post(self.endpoint + method, data=json.dumps(params), headers=headers)
if 400 <= r.status_code < 600:
r.reason = r.text
r.raise_for_status()
return r

@backoff
def put(self, method, params, headers=None):
r = self.session.put(self.endpoint + method, data=json.dumps(params), headers=headers)
r = self.session.put(self.endpoint + method, data=json.dumps(params), headers=headers)
if 400 <= r.status_code < 600:
r.reason = r.text
r.raise_for_status()
r.raise_for_status()
return r

def search(self, query, fromTime=None, toTime=None, timeZone='UTC'):
Expand Down