Skip to content

Commit

Permalink
Merge pull request #180 from Pankrat/master
Browse files Browse the repository at this point in the history
Use socket_timeout from parameters as connection timeout
  • Loading branch information
Gavin M. Roy committed Oct 2, 2012
2 parents f0b9081 + cd8e648 commit 8e5f870
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
3 changes: 1 addition & 2 deletions pika/adapters/base_connection.py
Expand Up @@ -33,7 +33,6 @@ class BaseConnection(connection.Connection):
WRITE = 0x0004 WRITE = 0x0004
ERROR = 0x0008 ERROR = 0x0008


SOCKET_TIMEOUT = 2
ERRORS_TO_IGNORE = [errno.EWOULDBLOCK, errno.EAGAIN, errno.EINTR] ERRORS_TO_IGNORE = [errno.EWOULDBLOCK, errno.EAGAIN, errno.EINTR]
HANDSHAKE = 'do_handshake_on_connect' HANDSHAKE = 'do_handshake_on_connect'
def __init__(self, parameters=None, def __init__(self, parameters=None,
Expand Down Expand Up @@ -354,7 +353,7 @@ def _socket_connect(self):
LOGGER.info("Connecting fd %d to %s:%i%s", LOGGER.info("Connecting fd %d to %s:%i%s",
self.socket.fileno(), self.params.host, self.socket.fileno(), self.params.host,
self.params.port, ssl_text) 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.connect((self.params.host, self.params.port))
self.socket.setblocking(0) self.socket.setblocking(0)


Expand Down
1 change: 0 additions & 1 deletion pika/adapters/blocking_connection.py
Expand Up @@ -24,7 +24,6 @@ class BlockingConnection(base_connection.BaseConnection):
messages from Basic.Deliver, Basic.GetOk, and Basic.Return. messages from Basic.Deliver, Basic.GetOk, and Basic.Return.
""" """
SOCKET_TIMEOUT = 0.25
SOCKET_TIMEOUT_THRESHOLD = 10 SOCKET_TIMEOUT_THRESHOLD = 10
SOCKET_TIMEOUT_MESSAGE = "Timeout exceeded, disconnected" SOCKET_TIMEOUT_MESSAGE = "Timeout exceeded, disconnected"


Expand Down
11 changes: 6 additions & 5 deletions pika/connection.py
Expand Up @@ -38,7 +38,7 @@ def __init__(self,
ssl=False, ssl=False,
ssl_options=None, ssl_options=None,
connection_attempts=1, connection_attempts=1,
retry_delay=2, retry_delay=2.0,
socket_timeout=DEFAULT_SOCKET_TIMEOUT, socket_timeout=DEFAULT_SOCKET_TIMEOUT,
locale=DEFAULT_LOCALE): locale=DEFAULT_LOCALE):
"""Create a new ConnectionParameters instance. """Create a new ConnectionParameters instance.
Expand All @@ -64,8 +64,8 @@ def __init__(self,
described at http://docs.python.org/dev/library/ssl.html described at http://docs.python.org/dev/library/ssl.html
:param int connection_attempts: Maximum number of retry attempts. :param int connection_attempts: Maximum number of retry attempts.
None for infinite. Defaults to 1 None for infinite. Defaults to 1
:param int retry_delay: Time to wait in seconds, before the next attempt :param int|float retry_delay: Time to wait in seconds, before the next
Defaults to 2 attempt. Defaults to 2
:param int|float socket_timeout: Use for high latency networks :param int|float socket_timeout: Use for high latency networks
Defaults to 0.25 Defaults to 0.25
:param str locale: Set the locale value :param str locale: Set the locale value
Expand Down Expand Up @@ -102,8 +102,9 @@ def __init__(self,
if (connection_attempts is not None and if (connection_attempts is not None and
not isinstance(connection_attempts, int)): not isinstance(connection_attempts, int)):
raise TypeError("connection_attempts must be either None or int") raise TypeError("connection_attempts must be either None or int")
if not isinstance(retry_delay, int): if (not isinstance(retry_delay, int) and
raise TypeError("retry_delay must be an int") not isinstance(retry_delay, float)):
raise TypeError("retry_delay must be a float or int")
if (not isinstance(socket_timeout, int) and if (not isinstance(socket_timeout, int) and
not isinstance(socket_timeout, float)): not isinstance(socket_timeout, float)):
raise TypeError("socket_timeout must be a float or int") raise TypeError("socket_timeout must be a float or int")
Expand Down
38 changes: 38 additions & 0 deletions 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)

0 comments on commit 8e5f870

Please sign in to comment.