Skip to content

Commit

Permalink
Add support for connect and write timeouts
Browse files Browse the repository at this point in the history
Differentiate timeouts for connect and write on TCP socket in the
Graphite handler. This allows users to set more reasonable timeouts.
  • Loading branch information
Pavlos Parissis committed Feb 28, 2016
1 parent e1736eb commit 89d5e55
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion haproxystats.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ server = 127.0.0.1
port = 3002
retries = 2
interval = 0.8
timeout = 0.9
delay = 10
backoff = 2
namespace = loadbalancers
Expand Down
1 change: 0 additions & 1 deletion haproxystats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
'port': '3002',
'retries': '3',
'interval': '1.8',
'timeout': '0.9',
'delay': '10',
'backoff': '2',
'namespace': 'loadbalancers',
Expand Down
10 changes: 9 additions & 1 deletion haproxystats/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,18 @@ def run(self):
dispatcher.register('flush', self.file_handler.flush)
dispatcher.register('loop', self.file_handler.loop)

timeout = self.config.getfloat('graphite', 'timeout')
connect_timeout = self.config.getfloat('graphite',
'connect-timeout',
fallback=timeout)
write_timeout = self.config.getfloat('graphite',
'write-timeout',
fallback=timeout)
graphite = GraphiteHandler(
server=self.config.get('graphite', 'server'),
port=self.config.getint('graphite', 'port'),
timeout=self.config.getfloat('graphite', 'timeout'),
connect_timeout=connect_timeout,
write_timeout=write_timeout,
retries=self.config.getint('graphite', 'retries'),
interval=self.config.getfloat('graphite', 'interval'),
delay=self.config.getfloat('graphite', 'delay'),
Expand Down
16 changes: 11 additions & 5 deletions haproxystats/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ class GraphiteHandler():
port (int): Port to connect to
retries (int): Numbers to retry on connection failure
interval (float): Time to sleep between retries
timeout (float): Timeout on connection
connect_timeout (float): Timeout on connection
write_timeout (float): Timeout on sending data
delay (float): Time to delay a connection attempt after last failure
backoff (float): Multiply interval by this factor after each failure
queue_size (int): Maximum size of the queue
Expand All @@ -230,15 +231,17 @@ def __init__(self,
port=3002,
retries=1,
interval=2,
timeout=10,
connect_timeout=1,
write_timeout=1,
delay=4,
backoff=2,
queue_size=1000000):
self.server = server
self.port = port
self.retries = retries
self.interval = interval
self.timeout = timeout
self.connect_timeout = connect_timeout
self.write_timeout = write_timeout
self.delay = delay
self.backoff = backoff
self.queue_size = queue_size
Expand All @@ -250,6 +253,9 @@ def __init__(self,
ConnectionAbortedError, BrokenPipeError, OSError,
socket.timeout)

log.debug('connect timeout %.2fsecs write timeout %.2fsecs',
self.connect_timeout, self.write_timeout)

def open(self):
"""Open a connection to graphite relay."""
try:
Expand All @@ -258,7 +264,7 @@ def open(self):
log.error('failed to connect to %s on port %s: %s', self.server,
self.port, error.raised)
else:
self.connection.settimeout(self.timeout)
self.connection.settimeout(self.write_timeout)
log.info('successfully connected to %s on port %s', self.server,
self.port)

Expand All @@ -277,7 +283,7 @@ def _create_connection():
log.info('connecting to %s on port %s', self.server, self.port)
self.connection =\
socket.create_connection((self.server, self.port),
timeout=self.timeout)
timeout=self.connect_timeout)

return _create_connection

Expand Down

0 comments on commit 89d5e55

Please sign in to comment.