Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unixconn.py 2.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import requests.adapters
  2. import socket
  3. from docker.transport.basehttpadapter import BaseHTTPAdapter
  4. from .. import constants
  5. import urllib3
  6. import urllib3.connection
  7. RecentlyUsedContainer = urllib3._collections.RecentlyUsedContainer
  8. class UnixHTTPConnection(urllib3.connection.HTTPConnection):
  9. def __init__(self, base_url, unix_socket, timeout=60):
  10. super().__init__(
  11. 'localhost', timeout=timeout
  12. )
  13. self.base_url = base_url
  14. self.unix_socket = unix_socket
  15. self.timeout = timeout
  16. def connect(self):
  17. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  18. sock.settimeout(self.timeout)
  19. sock.connect(self.unix_socket)
  20. self.sock = sock
  21. class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
  22. def __init__(self, base_url, socket_path, timeout=60, maxsize=10):
  23. super().__init__(
  24. 'localhost', timeout=timeout, maxsize=maxsize
  25. )
  26. self.base_url = base_url
  27. self.socket_path = socket_path
  28. self.timeout = timeout
  29. def _new_conn(self):
  30. return UnixHTTPConnection(
  31. self.base_url, self.socket_path, self.timeout
  32. )
  33. class UnixHTTPAdapter(BaseHTTPAdapter):
  34. __attrs__ = requests.adapters.HTTPAdapter.__attrs__ + ['pools',
  35. 'socket_path',
  36. 'timeout',
  37. 'max_pool_size']
  38. def __init__(self, socket_url, timeout=60,
  39. pool_connections=constants.DEFAULT_NUM_POOLS,
  40. max_pool_size=constants.DEFAULT_MAX_POOL_SIZE):
  41. socket_path = socket_url.replace('http+unix://', '')
  42. if not socket_path.startswith('/'):
  43. socket_path = '/' + socket_path
  44. self.socket_path = socket_path
  45. self.timeout = timeout
  46. self.max_pool_size = max_pool_size
  47. self.pools = RecentlyUsedContainer(
  48. pool_connections, dispose_func=lambda p: p.close()
  49. )
  50. super().__init__()
  51. def get_connection(self, url, proxies=None):
  52. with self.pools.lock:
  53. pool = self.pools.get(url)
  54. if pool:
  55. return pool
  56. pool = UnixHTTPConnectionPool(
  57. url, self.socket_path, self.timeout,
  58. maxsize=self.max_pool_size
  59. )
  60. self.pools[url] = pool
  61. return pool
  62. def request_url(self, request, proxies):
  63. # The select_proxy utility in requests errors out when the provided URL
  64. # doesn't have a hostname, like is the case when using a UNIX socket.
  65. # Since proxies are an irrelevant notion in the case of UNIX sockets
  66. # anyway, we simply return the path URL directly.
  67. # See also: https://github.com/docker/docker-py/issues/811
  68. return request.path_url