googleapis / google-auth-library-python

Google Auth Python Library
https://googleapis.dev/python/google-auth/latest/
Apache License 2.0
752 stars 301 forks source link

Pickleable AuthorizedSession #1509

Closed golddranks closed 2 months ago

golddranks commented 3 months ago

Is your feature request related to a problem? Please describe. In our Django application, to access some Google API's as a part of the processing for some requests, we are currently creating a AuthorizedSession per request where it's needed. However, this tends to take some time, from 0.5 seconds to 1.0 second. In some cases, we are getting bursts of 5-10 subsequent requests, and the time of authorizing sessions might delay the user getting the end result by multiple seconds. We'd like to cache AuthorizedSession, and it would be enough to cache it in Django's LocMemCache, but this requires the cached object to be pickleable, so we are currently unable to do that.

Describe the solution you'd like Make AuthorizedSession pickleable, so the caches would work. Currently many of the _ attributes get lost. Here is a comparision of dir() before and after pickling:

 ['__attrs__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_auth_request', '_auth_request_session', '_default_host', '_is_mtls', '_max_refresh_attempts', '_refresh_status_codes', '_refresh_timeout', 'adapters', 'auth', 'cert', 'close', 'configure_mtls_channel', 'cookies', 'credentials', 'delete', 'get', 'get_adapter', 'get_redirect_target', 'head', 'headers', 'hooks', 'is_mtls', 'max_redirects', 'merge_environment_settings', 'mount', 'options', 'params', 'patch', 'post', 'prepare_request', 'proxies', 'put', 'rebuild_auth', 'rebuild_method', 'rebuild_proxies', 'request', 'resolve_redirects', 'send', 'should_strip_auth', 'stream', 'trust_env', 'verify']

vs.

['__attrs__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'adapters', 'auth', 'cert', 'close', 'configure_mtls_channel', 'cookies', 'delete', 'get', 'get_adapter', 'get_redirect_target', 'head', 'headers', 'hooks', 'is_mtls', 'max_redirects', 'merge_environment_settings', 'mount', 'options', 'params', 'patch', 'post', 'prepare_request', 'proxies', 'put', 'rebuild_auth', 'rebuild_method', 'rebuild_proxies', 'request', 'resolve_redirects', 'send', 'should_strip_auth', 'stream', 'trust_env', 'verify']

The difference seems to be:

'_auth_request', '_auth_request_session', '_default_host', '_is_mtls', '_max_refresh_attempts', '_refresh_status_codes', '_refresh_timeout', 'credentials'

And indeed, the error when trying with cache, is about missing _auth_request.

Describe alternatives you've considered

clundin25 commented 3 months ago

Asking to make AuthorizedSession thread-safe, so we could just stick it into a singleton object. This seems to be hard, because the underlying Requests isn't apparently thread-safe.

Is wrapping the AuthorizedSession with a mutex an option?

I am not keen on making AuthorizedSession pickle-able but I have not done a thorough investigation.

clundin25 commented 2 months ago

We chatted about this and we don't believe we will support pickling AuthorizedSessions. Please re-open this if you have any more questions!