Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Handle urllib3 connection pool max size for GCS storage backend. #203

Open
jackton1 opened this issue Aug 31, 2020 · 2 comments
Open

Handle urllib3 connection pool max size for GCS storage backend. #203

jackton1 opened this issue Aug 31, 2020 · 2 comments

Comments

@jackton1
Copy link

2020-08-31 12:39:21,019 urllib3.connectionpool "WARNING" - Connection pool is full, discarding connection: storage.googleapis.com

Steps to replicate

COLLECTFAST_THREADS = 20
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'

Run

$ python manage.py collecstatic
@lev112
Copy link

lev112 commented Jun 19, 2021

based on this: googleapis/python-storage#253 (comment)
I created the following workaround:

from storages.backends.gcloud import GoogleCloudStorage
from storages.utils import setting
from google.cloud.storage import Client
import requests


class GoogleCloudStorageCustom(GoogleCloudStorage):
    def __init__(self, **settings):
        super().__init__(**settings)

        threads = setting("COLLECTFAST_THREADS")
        adapter = requests.adapters.HTTPAdapter(
            pool_connections=threads, pool_maxsize=threads, max_retries=3, pool_block=True
        )
        self.client._http.mount("https://", adapter)
        self.client._http._auth_request.session.mount("https://", adapter)

and than use

STATICFILES_STORAGE = "path.to.GoogleCloudStorageCustom"

@jackton1
Copy link
Author

An alternative would be

    @property
    def client(self):
        if self._client is None:
            scope = (
                "https://www.googleapis.com/auth/devstorage.full_control",
                "https://www.googleapis.com/auth/devstorage.read_only",
                "https://www.googleapis.com/auth/devstorage.read_write",
            )
            credentials, _ = google.auth.default(scopes=scope)

            threads = settings.COLLECTFAST_THREADS
            adapter = HTTPAdapter(
                pool_connections=threads,
                pool_maxsize=threads,
                max_retries=3,
                pool_block=True
            )

            auth_request_session = requests.Session()
            auth_request_session.mount("https://", adapter)
            auth_request = Request(auth_request_session)

            _http = AuthorizedSession(
                credentials,
                refresh_timeout=300,
                auth_request=auth_request,
            )
            _http.mount('https://', adapter)

            self._client = Client(
                project=self.project_id,
                credentials=credentials,
                _http=_http,
            )

        return self._client

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants