Skip to content

Commit

Permalink
Merge pull request #9 from diggyk/master
Browse files Browse the repository at this point in the history
Added retry logic to the client for 500 errors
  • Loading branch information
gmjosack committed Jun 16, 2015
2 parents 2bf2f8c + 027713f commit ac81fa2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
27 changes: 25 additions & 2 deletions bin/hermes
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import logging
import requests
from requests.packages import urllib3
import sys
from time import sleep
import traceback


Expand All @@ -24,6 +25,22 @@ class HermesException(Exception):
"""Generic exception used to indicate a problem with a Hermes operation"""
pass

def retry(num_attempts, min_sleep=.1, max_sleep=5):
for num in range(1, num_attempts+1):
yield num

# Don't sleep on final attempt. You've failed :(
if num == num_attempts:
break

# Sleep longer for each attempt
sleep_time = (min_sleep * (num * num))

# Clamp the sleep time to max_sleep
sleep_time = max(0, min(max_sleep, sleep_time))

sleep(sleep_time)

def request_get(path):
"""Make an HTTP GET request for the given path
Expand All @@ -32,7 +49,10 @@ def request_get(path):
Returns:
the http response
"""
response = requests.get(settings.hermes_server + path)
for attempt in retry(settings.api_retries):
response = requests.get(settings.hermes_server + path)
if response.status_code < 500:
break

if response.status_code != requests.codes.ok or not response.content:
try:
Expand All @@ -58,7 +78,10 @@ def request_post(path, json):
Returns:
the http response
"""
response = requests.post(settings.hermes_server + path, json=json)
for attempt in retry(settings.api_retries):
response = requests.post(settings.hermes_server + path, json=json)
if response.status_code < 500:
break

if (
response.status_code != requests.codes.created
Expand Down
3 changes: 2 additions & 1 deletion hermes/settings_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ def __getattr__(self, name):
"log_format": "%(asctime)-15s\t%(levelname)s\t%(message)s",
"debug": False,
"user_auth_header": "X-Hermes-Email",
"hermes_server": "http://localhost:10901"
"hermes_server": "http://localhost:10901",
"api_retries": 5
})
2 changes: 1 addition & 1 deletion hermes/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.18"
__version__ = "0.1.19"

0 comments on commit ac81fa2

Please sign in to comment.