Skip to content

Commit 1904e72

Browse files
committed
Added a timeout to the cli and TCPTransport (closes #10)
1 parent 47a71bd commit 1904e72

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

riemann_client/command.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@
1414

1515

1616
def udp_transport_factory(args):
17+
if args.timeout is not None:
18+
parser.error('--timeout cannot be used with the UDP transport')
1719
return riemann_client.transport.UDPTransport(args.host, args.port)
1820

1921

2022
def tcp_transport_factory(args):
21-
return riemann_client.transport.TCPTransport(args.host, args.port)
23+
return riemann_client.transport.TCPTransport(
24+
args.host, args.port, args.timeout)
2225

2326

2427
def tls_transport_factory(args):
@@ -68,6 +71,10 @@ def tls_transport_factory(args):
6871
'-t', '--transport', choices=TRANSPORT_FACTORIES.keys(), default='tcp',
6972
help="The transport to use (default: %(default)s)")
7073

74+
parser.add_argument(
75+
'-T', '--timeout', type=float,
76+
help="Timeout for TCP connections (default: %(default)s)")
77+
7178
subparsers = parser.add_subparsers(dest='subparser')
7279

7380

@@ -79,7 +86,7 @@ def send_function(args, client):
7986
'host': args.event_host,
8087
'description': args.description,
8188
'service': args.service,
82-
'tags': map(str.strip, args.tags.split(',')),
89+
'tags': args.tags and map(str.strip, args.tags.split(',')),
8390
'ttl': args.ttl,
8491
'metric_f': args.metric})
8592

riemann_client/transport.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
import riemann_client.riemann_pb2
1111

1212

13+
# Default arguments
14+
HOST = 'localhost'
15+
PORT = 5555
16+
TIMEOUT = None
17+
18+
1319
def socket_recvall(socket, length, bufsize=4096):
1420
"""Recives bytes from a socket until the buffer is the requested length"""
1521
data = ""
@@ -51,7 +57,7 @@ def send(self):
5157
class SocketTransport(Transport):
5258
"""Provides common functionality for Transports using sockets"""
5359

54-
def __init__(self, host='localhost', port=5555):
60+
def __init__(self, host=HOST, port=PORT):
5561
self.host = host
5662
self.port = port
5763

@@ -74,6 +80,7 @@ def socket(self, value):
7480
class UDPTransport(SocketTransport):
7581
def connect(self):
7682
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
83+
self.socket.settimeout(self.timeout)
7784

7885
def disconnect(self):
7986
self.socket.close()
@@ -84,9 +91,12 @@ def send(self, message):
8491

8592

8693
class TCPTransport(SocketTransport):
94+
def __init__(self, host=HOST, port=PORT, timeout=TIMEOUT):
95+
super(TCPTransport, self).__init__(host, port)
96+
self.timeout = timeout
97+
8798
def connect(self):
88-
self.socket = socket.create_connection(self.address)
89-
self.socket.setblocking(True)
99+
self.socket = socket.create_connection(self.address, self.timeout)
90100

91101
def disconnect(self):
92102
self.socket.close()
@@ -106,8 +116,8 @@ def send(self, message):
106116

107117

108118
class TLSTransport(TCPTransport):
109-
def __init__(self, host='localhost', port=5554, ca_certs=None):
110-
super(TLSTransport, self).__init__(host, port)
119+
def __init__(self, host=HOST, port=PORT, timeout=TIMEOUT, ca_certs=None):
120+
super(TLSTransport, self).__init__(host, port, timeout)
111121
self.ca_certs = ca_certs
112122

113123
def connect(self):

0 commit comments

Comments
 (0)