Giter Site home page Giter Site logo

Comments (12)

RunOrVeith avatar RunOrVeith commented on September 22, 2024 7

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!

from python-storage.

dylancaponi avatar dylancaponi commented on September 22, 2024 3

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().

from python-storage.

andrewsg avatar andrewsg commented on September 22, 2024 3

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

from python-storage.

micimize avatar micimize commented on September 22, 2024 1

@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

from python-storage.

micimize avatar micimize commented on September 22, 2024 1

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,
            )

from python-storage.

dylancaponi avatar dylancaponi commented on September 22, 2024 1

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.

from python-storage.

micimize avatar micimize commented on September 22, 2024

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

from python-storage.

dylancaponi avatar dylancaponi commented on September 22, 2024

@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>)

from python-storage.

dylancaponi avatar dylancaponi commented on September 22, 2024

@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?

from python-storage.

micimize avatar micimize commented on September 22, 2024

@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

from python-storage.

dylancaponi avatar dylancaponi commented on September 22, 2024

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

from python-storage.

jackkwok avatar jackkwok commented on September 22, 2024

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

from python-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.