GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add Retry Strategy to manual libraries. · Issue #338 · googleapis/python-storage

This repository was archived by the owner on Mar 31, 2026. It is now read-only.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Add Retry Strategy to manual libraries. #338

Copy link
Copy link
Closed
Closed
Add Retry Strategy to manual libraries.#338
Copy link
Assignees
andrewsg
Labels
api: storageIssues related to the googleapis/python-storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Description

@frankyn
frankyn
opened on Sep 25, 2019
Issue body actions

Feature request to track work done to fix retry strategy issues in Python libraries.

Issues that have a workaround will be deduped against this bug as @crwilcox, @tritone and I organize the effort.

Reacted by Michael Engelhard, RyanQuey, Philipp Wiesner, Veith Röthlingshöfer, Mathias, g-abello, Thomas Riccardi and Pietro De Nicolao

Activity

  1. frankyn
    assigned
    crwilcox
    ,
    frankyn
    and
    tritone
    on Sep 25, 2019
  2. tseaver
    changed the title [-]Correct Retry Strategy in Python libraries[/-] [+]Add Retry Strategy to manual libraries.[/+] on Sep 25, 2019
  3. micimize commented on Jan 21, 2020

    @micimize
    micimize
    on Jan 21, 2020
    More actions

    This might be possible with the gcs client currently via

    session = requests.Session()
    session.mount("https://storage.googleapis.com", requests.adapters.HTTPAdapter(max_retries=5))
    client = Client(project=env.GOOGLE_PROJECT, _http=session)

    Haven't tested though

  4. dylancaponi commented on Mar 3, 2020

    @dylancaponi
    dylancaponi
    on Mar 3, 2020
    More actions

    @micimize This is giving me errors (below) on deployment. Any idea why?

    I'm assuming Client here is really:
    from google.cloud import storage
    storage.Client(...) ?

    Code snippet:

    from google.cloud import storage
    import requests
    session = requests.Session()
    session.mount("https://storage.googleapis.com", requests.adapters.HTTPAdapter(max_retries=5))
    client = storage.Client(project=PROJECT_ID, _http=session)
    

    Error when attempting blob.download_as_string():

    ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: 401 GET https://storage.googleapis.com/download/storage/v1/b/<redacted file name>/o/secrets?alt=media: ('Request failed with status code', 401, 'Expected one of', <HTTPStatus.OK: 200>, <HTTPStatus.PARTIAL_CONTENT: 206>)
    
  5. micimize commented on Mar 3, 2020

    @micimize
    micimize
    on Mar 3, 2020
    More actions

    @dylancaponi does it work in the same environment without the _http argument? 401 means unauthorized, so you probably need to "bind the credentials" like the docs say the default does:

    If not passed, an _http object is created that is bound to the credentials for the current object

    Reacted by Dylan Caponi
  6. micimize commented on Mar 3, 2020

    @micimize
    micimize
    on Mar 3, 2020
    More actions

    They broke the monorepo up, but I found where they set credentials for reference:

            if self._http_internal is None:
                self._http_internal = google.auth.transport.requests.AuthorizedSession(
                    self._credentials,
                    refresh_timeout=_CREDENTIALS_REFRESH_TIMEOUT,
                )
    Reacted by Dylan Caponi
  7. RunOrVeith commented on May 13, 2020

    @RunOrVeith
    RunOrVeith
    on May 13, 2020
    More actions

    Since these retry-able errors happen quite frequently, I'd really like for this to be a feature sooner than later.
    Thanks for your efforts!

    Reacted by Phillip Thomas, Thomas Griseau, Billie Thompson, Philipp Wiesner, Mariana Mărășoiu, Fred Turkington and Jack Kwok
  8. dylancaponi commented on Jul 9, 2020

    @dylancaponi
    dylancaponi
    on Jul 9, 2020
    More actions

    @micimize Yes, the code runs when we don't pass _http. That is how our normal (non-retrying) connection is established.

    builtins.storage_client = storage.Client(project='<redacted>')

    However, that means it's not passing the session that defines 5 retries which I really want to do. Do you know what the proper approach to that is?

  9. micimize commented on Jul 9, 2020

    @micimize
    micimize
    on Jul 9, 2020
    More actions

    @dylancaponi I don't, but what I would do is keep digging through the source until you find google.auth.transport.requests.AuthorizedSession. You basically want to extract the auth logic they're using and figure out how to combine it with the requests.adapters.HTTPAdapter(max_retries=5) solution, and then pass the resulting session in as _http

  10. dylancaponi commented on Jul 9, 2020

    @dylancaponi
    dylancaponi
    on Jul 9, 2020
    More actions

    Thanks! I think I want to do something like:

    storage_client = storage.Client()
    adapter = requests.adapters.HTTPAdapter(max_retries=5)
    storage_client._http.mount("https://", adapter)
    storage_client._http._auth_request.session.mount("https://", adapter)
    

    as mentioned here:
    googleapis/python-bigquery#59 (comment)

    After 8 days of testing, this function did not terminate once due to connectionResetError!

    Adding to production code and will report back.

    Reacted by Michael Joseph Rosenthal
  11. dylancaponi commented on Jul 21, 2020

    @dylancaponi
    dylancaponi
    on Jul 21, 2020
    More actions

    We implemented this in production and have seen zero errors after 5 days.

    Previously we were seeing ~10 errors a day.

    This solved our issues. Thanks @micimize & @pietrodn

    def add_client_retries(client):
        """ Retry connection to prevent connectionResetError
        """
        adapter = requests.adapters.HTTPAdapter(max_retries=5)
        client._http.mount("https://", adapter)
        client._http._auth_request.session.mount("https://", adapter)
        return client
    

    in main.py:

    from google.cloud import storage
    from google.cloud import bigquery
    storage_client = add_client_retries(storage.Client())
    bq_client = add_client_retries(bigquery.Client())
    

    Also, remember to only initialize these once outside of your main() function and reuse them in main().

    Reacted by Michael Joseph Rosenthal and liyicloud
    Reacted by Pietro De Nicolao
  12. dylancaponi commented on Dec 4, 2020

    @dylancaponi
    dylancaponi
    on Dec 4, 2020
    More actions

    The solution above worked for a month or two but we are now experiencing this issue again ...
    How is everyone else faring?

  13. crwilcox
    transferred this issue fromgoogleapis/google-cloud-pythonon Dec 4, 2020
  14. product-auto-label
    added
    api: storageIssues related to the googleapis/python-storage API.
    on Dec 4, 2020
  15. yoshi-automation
    added
    🚨This issue needs some love.
    triage meI really want to be triaged.
    on Dec 4, 2020
  16. frankyn
    assigned
    andrewsg
    and unassigned
    crwilcox
    ,
    frankyn
    and
    tritone
    on Dec 4, 2020
  17. frankyn
    added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    🚨This issue needs some love.
    triage meI really want to be triaged.
    on Dec 4, 2020
  18. andrewsg commented on Dec 11, 2020

    @andrewsg
    andrewsg
    on Dec 11, 2020
    Contributor
    More actions

    This is now resolved for storage. Retries are supported by default, documentation work for configuring them is pending (for now, see docstrings).

    Reacted by Dylan Caponi
    Reacted by Dylan Caponi
    Reacted by Dylan Caponi
  19. andrewsg
    closed this as completedon Dec 11, 2020
  20. jackkwok commented on May 17, 2021

    @jackkwok
    jackkwok
    on May 17, 2021
    More actions

    @andrewsg : What release version of google-cloud-storage is retries supported by default?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @andrewsg
    andrewsg

Labels

api: storageIssues related to the googleapis/python-storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.