Skip to content

Commit

Permalink
Retry mechanism (#35)
Browse files Browse the repository at this point in the history
* Beta version of retry mechanism

* added 502 response code and exponential backoff

* bugfix

* Update CHANGELOG.rst

* fixed tests

* flake8

* Update CHANGELOG.rst
  • Loading branch information
birnbaum authored Jun 12, 2020
1 parent 3a45a22 commit 0cd5fd9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ Possible types of changes are:
Unreleased
----------

1.4.0 - 12.06.2020
------------------

Changed
'''''''
- The underlying HTTP client is now configured to automatically retry requests that return a status code of "429 Too Many Requests", "502 Bad Gateway", "503 Service Unavailable" and "504 Gateway Timeout".

Fixed
'''''
- Some tests were still calling ``get_bucket()`` from the constructor of ``GCSFS``.


1.3.0 - 20.05.2020
------------------

Expand Down
11 changes: 11 additions & 0 deletions fs_gcsfs/_gcsfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from fs.time import datetime_to_epoch
from google.cloud.storage import Client
from google.cloud.storage.blob import Blob
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry


__all__ = ["GCSFS"]

Expand Down Expand Up @@ -57,6 +60,7 @@ def __init__(self,
root_path: str = None,
create: bool = False,
client: Client = None,
retry: int = 5,
strict: bool = True):
super().__init__()
self._bucket_name = bucket_name
Expand All @@ -71,6 +75,13 @@ def __init__(self,
if self.client is None:
self.client = Client()

if retry:
adapter = HTTPAdapter(max_retries=Retry(total=retry,
status_forcelist=[429, 502, 503, 504],
method_whitelist=False, # retry on any HTTP method
backoff_factor=0.5))
self.client._http.mount("https://", adapter)

self.bucket = self.client.bucket(self._bucket_name)

if self._prefix != "":
Expand Down
2 changes: 1 addition & 1 deletion fs_gcsfs/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def client():

@pytest.fixture(scope="module")
def bucket(client):
return client.get_bucket(os.environ['TEST_BUCKET'])
return client.bucket(os.environ['TEST_BUCKET'])


@pytest.fixture(scope="function")
Expand Down
7 changes: 6 additions & 1 deletion fs_gcsfs/tests/test_gcsfs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import unittest
import uuid
from unittest import mock

import pytest
from fs import open_fs
Expand All @@ -18,7 +19,7 @@ class TestGCSFS(FSTestCases, unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.client = Client()
cls.bucket = cls.client.get_bucket(TEST_BUCKET)
cls.bucket = cls.client.bucket(TEST_BUCKET)
super().setUpClass()

def setUp(self):
Expand All @@ -42,6 +43,10 @@ class ClientMock:
def bucket(self, _):
pass

@property
def _http(self):
return mock.MagicMock()

return ClientMock()


Expand Down

0 comments on commit 0cd5fd9

Please sign in to comment.