From 2e7cf538914fa4579d80143deed449698b410d52 Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 13:26:11 +0300 Subject: [PATCH 1/6] - Allow setting timeout on PBC transports by creating new transport class through static method on RiakPbcTransport --- riak/transports/connection.py | 5 +++++ riak/transports/pbc.py | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/riak/transports/connection.py b/riak/transports/connection.py index d26b85f4..6ce5e4ff 100644 --- a/riak/transports/connection.py +++ b/riak/transports/connection.py @@ -152,6 +152,9 @@ def _new_connection(self): class Socket(object): + # Allow extending classes to affect how sockets are created through options + options = {} + def __init__(self, host, port): self.host = host self.port = port @@ -161,6 +164,8 @@ def __init__(self, host, port): def maybe_connect(self): if self.sock is None: self.sock = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + if "timeout" in options: + self.sock.settimeout(self.options["timeout"]) try: s.connect((self.host, self.port)) diff --git a/riak/transports/pbc.py b/riak/transports/pbc.py index 19afe85f..49c2e3c4 100644 --- a/riak/transports/pbc.py +++ b/riak/transports/pbc.py @@ -22,6 +22,7 @@ import errno import socket import struct +import types try: import json @@ -160,6 +161,12 @@ class RiakPbcTransport(RiakTransport): # The ConnectionManager class that this transport prefers. default_cm = connection.cm_using(SocketWithId) + @staticmethod + def with_timeout(timeout): + socket_with_timeout = type("SocketWithIdAndTimeout", [SocketWithId], {"options":{"timeout":timeout}}) + cm_with_timeout = connection.cm_using(socket_with_timeout) + return type("RiackPbcTransportWithTimeout", [RiakPbcTransport], {"default_cm":cm_with_timeout}) + def __init__(self, cm, client_id=None, max_attempts=1, **unused_options): """ Construct a new RiakPbcTransport object. From d1b8be2aa870aeb168e7a78a203e433e40dfaebf Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 14:35:47 +0300 Subject: [PATCH 2/6] - Fixed bases-tuples to be tuples rather than lists --- riak/transports/pbc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/riak/transports/pbc.py b/riak/transports/pbc.py index 49c2e3c4..708ac0f1 100644 --- a/riak/transports/pbc.py +++ b/riak/transports/pbc.py @@ -163,9 +163,9 @@ class RiakPbcTransport(RiakTransport): @staticmethod def with_timeout(timeout): - socket_with_timeout = type("SocketWithIdAndTimeout", [SocketWithId], {"options":{"timeout":timeout}}) + socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"options":{"timeout":timeout}}) cm_with_timeout = connection.cm_using(socket_with_timeout) - return type("RiackPbcTransportWithTimeout", [RiakPbcTransport], {"default_cm":cm_with_timeout}) + return type("RiackPbcTransportWithTimeout", (RiakPbcTransport,), {"default_cm":cm_with_timeout}) def __init__(self, cm, client_id=None, max_attempts=1, **unused_options): """ From 2978c7eb6de2fa48f736827b3dd607c743b54d2a Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 14:37:55 +0300 Subject: [PATCH 3/6] - Fixed reference to options --- riak/transports/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riak/transports/connection.py b/riak/transports/connection.py index 6ce5e4ff..48ac4a48 100644 --- a/riak/transports/connection.py +++ b/riak/transports/connection.py @@ -164,7 +164,7 @@ def __init__(self, host, port): def maybe_connect(self): if self.sock is None: self.sock = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - if "timeout" in options: + if "timeout" in self.options: self.sock.settimeout(self.options["timeout"]) try: From cf700c9128b141367b66b702969bad16e71cfbcf Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 14:40:11 +0300 Subject: [PATCH 4/6] - Renamed options _options - with_timeout now copies options of Socket and adds to the copy, rather than ignoring any options already set --- riak/transports/connection.py | 6 +++--- riak/transports/pbc.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/riak/transports/connection.py b/riak/transports/connection.py index 48ac4a48..cfa23f6e 100644 --- a/riak/transports/connection.py +++ b/riak/transports/connection.py @@ -153,7 +153,7 @@ def _new_connection(self): class Socket(object): # Allow extending classes to affect how sockets are created through options - options = {} + _options = {} def __init__(self, host, port): self.host = host @@ -164,8 +164,8 @@ def __init__(self, host, port): def maybe_connect(self): if self.sock is None: self.sock = s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - if "timeout" in self.options: - self.sock.settimeout(self.options["timeout"]) + if "timeout" in self._options: + self.sock.settimeout(self._options["timeout"]) try: s.connect((self.host, self.port)) diff --git a/riak/transports/pbc.py b/riak/transports/pbc.py index 708ac0f1..6b510d35 100644 --- a/riak/transports/pbc.py +++ b/riak/transports/pbc.py @@ -163,7 +163,8 @@ class RiakPbcTransport(RiakTransport): @staticmethod def with_timeout(timeout): - socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"options":{"timeout":timeout}}) + socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"_options":dict(SocketWithId.options)}}) + socket_with_timeout._options["timeout"] = timeout cm_with_timeout = connection.cm_using(socket_with_timeout) return type("RiackPbcTransportWithTimeout", (RiakPbcTransport,), {"default_cm":cm_with_timeout}) From 9258aa49d5d3d3cdeba903d68eccd0471f9942f0 Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 14:41:30 +0300 Subject: [PATCH 5/6] - Fixed syntax error --- riak/transports/pbc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riak/transports/pbc.py b/riak/transports/pbc.py index 6b510d35..c450d798 100644 --- a/riak/transports/pbc.py +++ b/riak/transports/pbc.py @@ -163,7 +163,7 @@ class RiakPbcTransport(RiakTransport): @staticmethod def with_timeout(timeout): - socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"_options":dict(SocketWithId.options)}}) + socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"_options":dict(SocketWithId.options)}) socket_with_timeout._options["timeout"] = timeout cm_with_timeout = connection.cm_using(socket_with_timeout) return type("RiackPbcTransportWithTimeout", (RiakPbcTransport,), {"default_cm":cm_with_timeout}) From c4affa40f2828324fe5af2d69757c32a6abf0854 Mon Sep 17 00:00:00 2001 From: Tuure Laurinolli Date: Mon, 24 Sep 2012 14:42:14 +0300 Subject: [PATCH 6/6] - Fixed _options reference --- riak/transports/pbc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riak/transports/pbc.py b/riak/transports/pbc.py index c450d798..706f6963 100644 --- a/riak/transports/pbc.py +++ b/riak/transports/pbc.py @@ -163,7 +163,7 @@ class RiakPbcTransport(RiakTransport): @staticmethod def with_timeout(timeout): - socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"_options":dict(SocketWithId.options)}) + socket_with_timeout = type("SocketWithIdAndTimeout", (SocketWithId,), {"_options":dict(SocketWithId._options)}) socket_with_timeout._options["timeout"] = timeout cm_with_timeout = connection.cm_using(socket_with_timeout) return type("RiackPbcTransportWithTimeout", (RiakPbcTransport,), {"default_cm":cm_with_timeout})