| 43 | |
| 44 | |
| 45 | class UnixHTTPAdapter(BaseHTTPAdapter): |
| 46 | |
| 47 | __attrs__ = requests.adapters.HTTPAdapter.__attrs__ + ['pools', |
| 48 | 'socket_path', |
| 49 | 'timeout', |
| 50 | 'max_pool_size'] |
| 51 | |
| 52 | def __init__(self, socket_url, timeout=60, |
| 53 | pool_connections=constants.DEFAULT_NUM_POOLS, |
| 54 | max_pool_size=constants.DEFAULT_MAX_POOL_SIZE): |
| 55 | socket_path = socket_url.replace('http+unix://', '') |
| 56 | if not socket_path.startswith('/'): |
| 57 | socket_path = f"/{socket_path}" |
| 58 | self.socket_path = socket_path |
| 59 | self.timeout = timeout |
| 60 | self.max_pool_size = max_pool_size |
| 61 | self.pools = RecentlyUsedContainer( |
| 62 | pool_connections, dispose_func=lambda p: p.close() |
| 63 | ) |
| 64 | super().__init__() |
| 65 | |
| 66 | def get_connection(self, url, proxies=None): |
| 67 | with self.pools.lock: |
| 68 | pool = self.pools.get(url) |
| 69 | if pool: |
| 70 | return pool |
| 71 | |
| 72 | pool = UnixHTTPConnectionPool( |
| 73 | url, self.socket_path, self.timeout, |
| 74 | maxsize=self.max_pool_size |
| 75 | ) |
| 76 | self.pools[url] = pool |
| 77 | |
| 78 | return pool |
| 79 | |
| 80 | def request_url(self, request, proxies): |
| 81 | # The select_proxy utility in requests errors out when the provided URL |
| 82 | # doesn't have a hostname, like is the case when using a UNIX socket. |
| 83 | # Since proxies are an irrelevant notion in the case of UNIX sockets |
| 84 | # anyway, we simply return the path URL directly. |
| 85 | # See also: https://github.com/docker/docker-py/issues/811 |
| 86 | return request.path_url |