Skip to content

Commit 9a33090

Browse files
author
Pavlos Parissis
committed
Add support for setting the timeout
1 parent 56a69a8 commit 9a33090

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

haproxyadmin/haproxy.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ class HAProxy(object):
8989
:type retry: ``integer`` or ``None``
9090
:param retry_interval: sleep time between the retries.
9191
:type retry_interval: ``integer``
92+
:param timeout: timeout for the connection
93+
:type timeout: ``float``
9294
:return: a user-created :class:`HAProxy` object.
9395
:rtype: :class:`HAProxy`
9496
"""
9597

96-
def __init__(self, socket_dir=None,
98+
def __init__(self,
99+
socket_dir=None,
97100
socket_file=None,
98101
retry=2,
99-
retry_interval=2):
102+
retry_interval=2,
103+
timeout=1,
104+
):
100105

101106
self._hap_processes = []
102107
socket_files = []
@@ -121,7 +126,12 @@ def __init__(self, socket_dir=None,
121126

122127
for so_file in socket_files:
123128
self._hap_processes.append(
124-
_HAProxyProcess(so_file, retry, retry_interval)
129+
_HAProxyProcess(
130+
socket_file=so_file,
131+
retry=retry,
132+
retry_interval=retry_interval,
133+
timeout=timeout
134+
)
125135
)
126136

127137
@should_die

haproxyadmin/internal/haproxy.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,22 @@ class _HAProxyProcess:
3131
HAProxy process using UNIX stats socket.
3232
3333
:param socket_file: Full path of socket file.
34-
:type socket_file: string
34+
:type socket_file: ``string``
3535
:param retry: (optional) Number of connect retries (defaults to 3)
36-
:type retry: integer
36+
:type retry: ``integer``
3737
:param retry_interval: (optional) Interval time in seconds between retries
3838
(defaults to 2)
39-
:type retry_interval: integer
39+
:param timeout: timeout for the connection
40+
:type timeout: ``float``
41+
:type retry_interval: ``integer``
4042
"""
41-
def __init__(self, socket_file, retry=3, retry_interval=2):
43+
def __init__(self, socket_file, retry=3, retry_interval=2, timeout=1):
4244
self.socket_file = socket_file
4345
self.hap_stats = {}
4446
self.hap_info = {}
4547
self.retry = retry
4648
self.retry_interval = retry_interval
49+
self.timeout = timeout
4750
# process number associated with this object
4851
self.process_nb = self.metric('Process_num')
4952

@@ -73,15 +76,10 @@ def command(self, command, full_output=False):
7376
# any other value means retry N times
7477
attempt = self.retry + 1
7578
while attempt != 0:
79+
print(time.ctime(), attempt, self.retry)
7680
try:
7781
unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
78-
# I haven't seen a case where a running process which holds a
79-
# UNIX socket will take more than few nanoseconds to accept a
80-
# connection. But, I have seen cases where it takes ~0.5secs
81-
# to get a respone from the socket. Thus I hard-code a timeout
82-
# of 0.5ms
83-
# TODO: consider having a configuration file for it
84-
unix_socket.settimeout(0.5)
82+
unix_socket.settimeout(self.timeout)
8583
unix_socket.connect(self.socket_file)
8684
unix_socket.send(six.b(command + '\n'))
8785
file_handle = unix_socket.makefile()

0 commit comments

Comments
 (0)