Skip to content

Commit ef24c11

Browse files
committed
Change default socket timeout to 120 seconds in both the client and connection
1 parent 94f4c9b commit ef24c11

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

kafka/client.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
BrokerResponseError, PartitionUnavailableError,
1111
KafkaUnavailableError, KafkaRequestError)
1212

13-
from kafka.conn import KafkaConnection
13+
from kafka.conn import KafkaConnection, DEFAULT_SOCKET_TIMEOUT_SECONDS
1414
from kafka.protocol import KafkaProtocol
1515

1616
log = logging.getLogger("kafka")
@@ -21,7 +21,11 @@ class KafkaClient(object):
2121
CLIENT_ID = "kafka-python"
2222
ID_GEN = count()
2323

24-
def __init__(self, host, port, client_id=CLIENT_ID, timeout=10):
24+
# NOTE: The timeout given to the client should always be greater than the
25+
# one passed to SimpleConsumer.get_message(), otherwise you can get a
26+
# socket timeout.
27+
def __init__(self, host, port, client_id=CLIENT_ID,
28+
timeout=DEFAULT_SOCKET_TIMEOUT_SECONDS):
2529
# We need one connection to bootstrap
2630
self.client_id = client_id
2731
self.timeout = timeout

kafka/conn.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
log = logging.getLogger("kafka")
1010

11+
DEFAULT_SOCKET_TIMEOUT_SECONDS = 120
1112

1213
class KafkaConnection(local):
1314
"""
@@ -20,10 +21,10 @@ class KafkaConnection(local):
2021
2122
host: the host name or IP address of a kafka broker
2223
port: the port number the kafka broker is listening on
23-
timeout: default None. The socket timeout for sending and receiving data.
24-
None means no timeout, so a request can block forever.
24+
timeout: default 120. The socket timeout for sending and receiving data
25+
in seconds. None means no timeout, so a request can block forever.
2526
"""
26-
def __init__(self, host, port, timeout=None):
27+
def __init__(self, host, port, timeout=DEFAULT_SOCKET_TIMEOUT_SECONDS):
2728
super(KafkaConnection, self).__init__()
2829
self.host = host
2930
self.port = port

0 commit comments

Comments
 (0)