Skip to content

Commit b94e31b

Browse files
committed
Add connect_timeout to cluster and Connection.factory
PYTHON-206
1 parent 35c6ce5 commit b94e31b

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

cassandra/cluster.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,14 @@ def auth_provider(self, value):
427427
See :attr:`.schema_event_refresh_window` for discussion of rationale
428428
"""
429429

430+
connect_timeout = 5
431+
"""
432+
Timeout, in seconds, for creating new connections.
433+
434+
This timeout covers the entire connection negotiation, including TCP
435+
establishment, options passing, and authentication.
436+
"""
437+
430438
sessions = None
431439
control_connection = None
432440
scheduler = None
@@ -465,7 +473,8 @@ def __init__(self,
465473
control_connection_timeout=2.0,
466474
idle_heartbeat_interval=30,
467475
schema_event_refresh_window=2,
468-
topology_event_refresh_window=10):
476+
topology_event_refresh_window=10,
477+
connect_timeout=5):
469478
"""
470479
Any of the mutable Cluster attributes may be set as keyword arguments
471480
to the constructor.
@@ -518,6 +527,7 @@ def __init__(self,
518527
self.idle_heartbeat_interval = idle_heartbeat_interval
519528
self.schema_event_refresh_window = schema_event_refresh_window
520529
self.topology_event_refresh_window = topology_event_refresh_window
530+
self.connect_timeout = connect_timeout
521531

522532
self._listeners = set()
523533
self._listener_lock = Lock()
@@ -707,11 +717,11 @@ def connection_factory(self, address, *args, **kwargs):
707717
Intended for internal use only.
708718
"""
709719
kwargs = self._make_connection_kwargs(address, kwargs)
710-
return self.connection_class.factory(address, *args, **kwargs)
720+
return self.connection_class.factory(address, self.connect_timeout, *args, **kwargs)
711721

712722
def _make_connection_factory(self, host, *args, **kwargs):
713723
kwargs = self._make_connection_kwargs(host.address, kwargs)
714-
return partial(self.connection_class.factory, host.address, *args, **kwargs)
724+
return partial(self.connection_class.factory, host.address, self.connect_timeout, *args, **kwargs)
715725

716726
def _make_connection_kwargs(self, address, kwargs_dict):
717727
if self._auth_provider_callable:

cassandra/connection.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,19 @@ def handle_fork(self):
240240
pass
241241

242242
@classmethod
243-
def factory(cls, *args, **kwargs):
243+
def factory(cls, host, timeout, *args, **kwargs):
244244
"""
245245
A factory function which returns connections which have
246246
succeeded in connecting and are ready for service (or
247247
raises an exception otherwise).
248248
"""
249-
timeout = kwargs.pop('timeout', 5.0)
250-
conn = cls(*args, **kwargs)
249+
conn = cls(host, *args, **kwargs)
251250
conn.connected_event.wait(timeout)
252251
if conn.last_error:
253252
raise conn.last_error
254253
elif not conn.connected_event.is_set():
255254
conn.close()
256-
raise OperationTimedOut("Timed out creating connection")
255+
raise OperationTimedOut("Timed out creating connection (%s seconds)" % timeout)
257256
else:
258257
return conn
259258

tests/integration/standard/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def get_connection(self):
5858
e = None
5959
for i in range(5):
6060
try:
61-
conn = self.klass.factory(protocol_version=PROTOCOL_VERSION)
61+
conn = self.klass.factory(host='127.0.0.1', timeout=5, protocol_version=PROTOCOL_VERSION)
6262
break
6363
except (OperationTimedOut, NoHostAvailable) as e:
6464
continue

0 commit comments

Comments
 (0)