Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use socket_timeout from parameters as connection timeout #180

Merged
merged 1 commit into from

2 participants

@Pankrat

Setting socket timeouts in Pika is a little bit confusing. Especially overriding the default connection timeout (250ms for blocking connections) involves subclassing the BlockingConnection to set the class attribute.

This patch removes the SOCKET_TIMEOUT class attribute which was only used for setting the initial connection timeout and instead reuses the already available socket_timeout from the connection parameters.

It also allows float values for retry_delay.

@Pankrat Pankrat Use socket_timeout for connection attempts
- Allow float value for retry_delay
- Remove unused SOCKET_TIMEOUT from adapters
cd8e648
@gmr
Owner
gmr commented

Thanks for the change (and the test!)

@gmr gmr merged commit 8e5f870 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 2, 2012
  1. @Pankrat

    Use socket_timeout for connection attempts

    Pankrat authored
    - Allow float value for retry_delay
    - Remove unused SOCKET_TIMEOUT from adapters
This page is out of date. Refresh to see the latest.
View
3  pika/adapters/base_connection.py
@@ -33,7 +33,6 @@ class BaseConnection(connection.Connection):
WRITE = 0x0004
ERROR = 0x0008
- SOCKET_TIMEOUT = 2
ERRORS_TO_IGNORE = [errno.EWOULDBLOCK, errno.EAGAIN, errno.EINTR]
HANDSHAKE = 'do_handshake_on_connect'
def __init__(self, parameters=None,
@@ -360,7 +359,7 @@ def _socket_connect(self):
LOGGER.info("Connecting fd %d to %s:%i%s",
self.socket.fileno(), self.params.host,
self.params.port, ssl_text)
- self.socket.settimeout(self.SOCKET_TIMEOUT)
+ self.socket.settimeout(self.params.socket_timeout)
self.socket.connect((self.params.host, self.params.port))
self.socket.setblocking(0)
View
1  pika/adapters/blocking_connection.py
@@ -25,7 +25,6 @@ class BlockingConnection(base_connection.BaseConnection):
messages from Basic.Deliver, Basic.GetOk, and Basic.Return.
"""
- SOCKET_TIMEOUT = 0.25
SOCKET_TIMEOUT_THRESHOLD = 10
SOCKET_TIMEOUT_MESSAGE = "Timeout exceeded, disconnected"
View
11 pika/connection.py
@@ -45,7 +45,7 @@ def __init__(self,
ssl=False,
ssl_options=None,
connection_attempts=1,
- retry_delay=2,
+ retry_delay=2.0,
socket_timeout=DEFAULT_SOCKET_TIMEOUT,
locale=DEFAULT_LOCALE):
"""Create a new ConnectionParameters instance.
@@ -71,8 +71,8 @@ def __init__(self,
described at http://docs.python.org/dev/library/ssl.html
:param int connection_attempts: Maximum number of retry attempts.
None for infinite. Defaults to 1
- :param int retry_delay: Time to wait in seconds, before the next attempt
- Defaults to 2
+ :param int|float retry_delay: Time to wait in seconds, before the next
+ attempt. Defaults to 2
:param int|float socket_timeout: Use for high latency networks
Defaults to 0.25
:param str locale: Set the locale value
@@ -109,8 +109,9 @@ def __init__(self,
if (connection_attempts is not None and
not isinstance(connection_attempts, int)):
raise TypeError("connection_attempts must be either None or int")
- if not isinstance(retry_delay, int):
- raise TypeError("retry_delay must be an int")
+ if (not isinstance(retry_delay, int) and
+ not isinstance(retry_delay, float)):
+ raise TypeError("retry_delay must be a float or int")
if (not isinstance(socket_timeout, int) and
not isinstance(socket_timeout, float)):
raise TypeError("socket_timeout must be a float or int")
View
38 tests/timeout_tests.py
@@ -0,0 +1,38 @@
+# -*- coding: utf8 -*-
+"""
+Tests for connection parameters.
+"""
+import socket
+from mock import patch
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+from pika import ConnectionParameters, BaseConnection
+from pika.exceptions import AMQPConnectionError
+
+
+def mock_timeout(*args, **kwargs):
+ raise socket.timeout
+
+
+class ConnectionTests(unittest.TestCase):
+
+ def test_parameters(self):
+ params = ConnectionParameters(socket_timeout=0.5,
+ retry_delay=0.1,
+ connection_attempts=3,
+ )
+ self.assertEqual(params.socket_timeout, 0.5)
+ self.assertEqual(params.retry_delay, 0.1)
+ self.assertEqual(params.connection_attempts, 3)
+
+
+ @patch.object(socket.socket, 'settimeout')
+ @patch.object(socket.socket, 'connect')
+ def test_connection_timeout(self, connect, settimeout):
+ connect.side_effect = mock_timeout
+ with self.assertRaises(AMQPConnectionError):
+ BaseConnection(ConnectionParameters(socket_timeout=2.0))
+ settimeout.assert_called_with(2.0)
Something went wrong with that request. Please try again.