Skip to content

Commit

Permalink
Use exponential backoff in HTTP methods. (#1279)
Browse files Browse the repository at this point in the history
* Use exponential backoff in HTTP methods.

* Small change in import order, also increases backoff duration.
  • Loading branch information
gguuss authored and Jon Wayne Parrott committed Dec 15, 2017
1 parent 20b94f1 commit c3f20cd
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion iot/api-client/http_example/cloudiot_http_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
import json
import time

from google.api_core import retry
import jwt
import requests


_BASE_URL = 'https://cloudiot-device.googleapis.com/v1beta1'
_BACKOFF_DURATION = 60


def create_jwt(project_id, private_key_file, algorithm):
Expand All @@ -54,6 +55,9 @@ def create_jwt(project_id, private_key_file, algorithm):
return jwt.encode(token, private_key, algorithm=algorithm).decode('ascii')


@retry.Retry(
predicate=retry.if_exception_type(AssertionError),
deadline=_BACKOFF_DURATION)
def publish_message(
message, message_type, base_url, project_id, cloud_region, registry_id,
device_id, jwt_token):
Expand Down Expand Up @@ -83,9 +87,16 @@ def publish_message(
resp = requests.post(
publish_url, data=json.dumps(body), headers=headers)

if (resp.status_code != 200):
print('Response came back {}, retrying'.format(resp.status_code))
raise AssertionError('Not OK response: {}'.format(resp.status_code))

return resp


@retry.Retry(
predicate=retry.if_exception_type(AssertionError),
deadline=_BACKOFF_DURATION)
def get_config(
version, message_type, base_url, project_id, cloud_region, registry_id,
device_id, jwt_token):
Expand All @@ -102,6 +113,10 @@ def get_config(

resp = requests.get(config_url, headers=headers)

if (resp.status_code != 200):
print('Error getting config: {}, retrying'.format(resp.status_code))
raise AssertionError('Not OK response: {}'.format(resp.status_code))

return resp


Expand Down

0 comments on commit c3f20cd

Please sign in to comment.