Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce client poll timeout when no IFRs #1823

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kafka/client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ def poll(self, timeout_ms=None, future=None):
metadata_timeout_ms,
idle_connection_timeout_ms,
self.config['request_timeout_ms'])
# if there are no requests in flight, do not block longer than the retry backoff
if self.in_flight_request_count() == 0:
timeout = min(timeout, self.config['retry_backoff_ms'])
timeout = max(0, timeout / 1000) # avoid negative timeouts
jeffwidman marked this conversation as resolved.
Show resolved Hide resolved

self._poll(timeout)
Expand Down
12 changes: 12 additions & 0 deletions test/test_client_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ def test_send(cli, conn):
def test_poll(mocker):
metadata = mocker.patch.object(KafkaClient, '_maybe_refresh_metadata')
_poll = mocker.patch.object(KafkaClient, '_poll')
ifrs = mocker.patch.object(KafkaClient, 'in_flight_request_count')
ifrs.return_value = 1
cli = KafkaClient(api_version=(0, 9))

# metadata timeout wins
Expand All @@ -249,6 +251,11 @@ def test_poll(mocker):
cli.poll()
_poll.assert_called_with(cli.config['request_timeout_ms'] / 1000.0)

# If no in-flight-requests, drop timeout to retry_backoff_ms
ifrs.return_value = 0
cli.poll()
_poll.assert_called_with(cli.config['retry_backoff_ms'] / 1000.0)


def test__poll():
pass
Expand Down Expand Up @@ -304,12 +311,14 @@ def client(mocker):

def test_maybe_refresh_metadata_ttl(mocker, client):
client.cluster.ttl.return_value = 1234
mocker.patch.object(KafkaClient, 'in_flight_request_count', return_value=1)

client.poll(timeout_ms=12345678)
client._poll.assert_called_with(1.234)


def test_maybe_refresh_metadata_backoff(mocker, client):
mocker.patch.object(KafkaClient, 'in_flight_request_count', return_value=1)
now = time.time()
t = mocker.patch('time.time')
t.return_value = now
Expand All @@ -320,6 +329,7 @@ def test_maybe_refresh_metadata_backoff(mocker, client):

def test_maybe_refresh_metadata_in_progress(mocker, client):
client._metadata_refresh_in_progress = True
mocker.patch.object(KafkaClient, 'in_flight_request_count', return_value=1)

client.poll(timeout_ms=12345678)
client._poll.assert_called_with(9999.999) # request_timeout_ms
Expand All @@ -328,6 +338,7 @@ def test_maybe_refresh_metadata_in_progress(mocker, client):
def test_maybe_refresh_metadata_update(mocker, client):
mocker.patch.object(client, 'least_loaded_node', return_value='foobar')
mocker.patch.object(client, '_can_send_request', return_value=True)
mocker.patch.object(KafkaClient, 'in_flight_request_count', return_value=1)
send = mocker.patch.object(client, 'send')

client.poll(timeout_ms=12345678)
Expand All @@ -342,6 +353,7 @@ def test_maybe_refresh_metadata_cant_send(mocker, client):
mocker.patch.object(client, '_can_connect', return_value=True)
mocker.patch.object(client, '_maybe_connect', return_value=True)
mocker.patch.object(client, 'maybe_connect', return_value=True)
mocker.patch.object(KafkaClient, 'in_flight_request_count', return_value=1)

now = time.time()
t = mocker.patch('time.time')
Expand Down