Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(api)!: remove timeout code #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions requests_unixsocket/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@
# https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py
class UnixHTTPConnection(httplib.HTTPConnection, object):

def __init__(self, unix_socket_url, timeout=60):
def __init__(self, unix_socket_url):
"""Create an HTTP connection to a unix domain socket

:param unix_socket_url: A URL with a scheme of 'http+unix' and the
netloc is a percent-encoded path to a unix domain socket. E.g.:
'http+unix://%2Ftmp%2Fprofilesvc.sock/status/pid'
"""
super(UnixHTTPConnection, self).__init__('localhost', timeout=timeout)
super(UnixHTTPConnection, self).__init__('localhost')
self.unix_socket_url = unix_socket_url
self.timeout = timeout
self.sock = None

def __del__(self): # base class does not have d'tor
Expand All @@ -44,21 +43,18 @@ def connect(self):

class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):

def __init__(self, socket_path, timeout=60):
super(UnixHTTPConnectionPool, self).__init__(
'localhost', timeout=timeout)
def __init__(self, socket_path):
super(UnixHTTPConnectionPool, self).__init__('localhost')
self.socket_path = socket_path
self.timeout = timeout

def _new_conn(self):
return UnixHTTPConnection(self.socket_path, self.timeout)
return UnixHTTPConnection(self.socket_path)


class UnixAdapter(HTTPAdapter):

def __init__(self, timeout=60, pool_connections=25, *args, **kwargs):
def __init__(self, pool_connections=25, *args, **kwargs):
super(UnixAdapter, self).__init__(*args, **kwargs)
self.timeout = timeout
self.pools = urllib3._collections.RecentlyUsedContainer(
pool_connections, dispose_func=lambda p: p.close()
)
Expand All @@ -76,7 +72,7 @@ def get_connection(self, url, proxies=None):
if pool:
return pool

pool = UnixHTTPConnectionPool(url, self.timeout)
pool = UnixHTTPConnectionPool(url)
self.pools[url] = pool

return pool
Expand Down
40 changes: 40 additions & 0 deletions timeout_socket_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import socket
import time
import os

server_address = './uds_socket'

# Make sure the socket does not already exist
try:
os.unlink(server_address)
except OSError:
if os.path.exists(server_address):
raise

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
print('starting up on {}'.format(server_address))
sock.bind(server_address)
sock.listen(1)

while True:
print('\nwaiting for a connection')

connection, client_address = sock.accept()
connection.settimeout(300)

try:
print('connected')
connect_time = time.time()
while True:
try:
data = connection.recv(640)
if not data:
elapsed_time = int(time.time() - connect_time)
print(f'no data after {elapsed_time}s')
break
except socket.timeout as exc:
elapsed_time = int(time.time() - connect_time)
print(f'timeout after {elapsed_time}s on recv')
break
finally:
connection.close()