@@ -678,12 +678,14 @@ def _poll_once(self, timeout_ms, max_records, update_offsets=True):
678678 Returns:
679679 dict: Map of topic to list of records (may be empty).
680680 """
681+ end_time = time .time () + timeout_ms / 1000
681682 self ._coordinator .poll ()
682683
683684 # Fetch positions if we have partitions we're subscribed to that we
684685 # don't know the offset for
685686 if not self ._subscription .has_all_fetch_positions ():
686- self ._update_fetch_positions (self ._subscription .missing_fetch_positions ())
687+ update_timeout_ms = 1000 * (end_time - time .time ())
688+ self ._update_fetch_positions (self ._subscription .missing_fetch_positions (), update_timeout_ms )
687689
688690 # If data is available already, e.g. from a previous network client
689691 # poll() call to commit, then just return it immediately
@@ -714,7 +716,7 @@ def _poll_once(self, timeout_ms, max_records, update_offsets=True):
714716 records , _ = self ._fetcher .fetched_records (max_records , update_offsets = update_offsets )
715717 return records
716718
717- def position (self , partition ):
719+ def position (self , partition , timeout_ms = float ( "inf" ) ):
718720 """Get the offset of the next record that will be fetched
719721
720722 Arguments:
@@ -728,7 +730,7 @@ def position(self, partition):
728730 assert self ._subscription .is_assigned (partition ), 'Partition is not assigned'
729731 offset = self ._subscription .assignment [partition ].position
730732 if offset is None :
731- self ._update_fetch_positions ([partition ])
733+ self ._update_fetch_positions ([partition ], timeout_ms )
732734 offset = self ._subscription .assignment [partition ].position
733735 return offset
734736
@@ -1087,7 +1089,7 @@ def _use_consumer_group(self):
10871089 return False
10881090 return True
10891091
1090- def _update_fetch_positions (self , partitions ):
1092+ def _update_fetch_positions (self , partitions , timeout_ms ):
10911093 """Set the fetch position to the committed position (if there is one)
10921094 or reset it using the offset reset policy the user has configured.
10931095
@@ -1099,12 +1101,13 @@ def _update_fetch_positions(self, partitions):
10991101 NoOffsetForPartitionError: If no offset is stored for a given
11001102 partition and no offset reset policy is defined.
11011103 """
1104+ end_time = time .time () + timeout_ms / 1000
11021105 # Lookup any positions for partitions which are awaiting reset (which may be the
11031106 # case if the user called :meth:`seek_to_beginning` or :meth:`seek_to_end`. We do
11041107 # this check first to avoid an unnecessary lookup of committed offsets (which
11051108 # typically occurs when the user is manually assigning partitions and managing
11061109 # their own offsets).
1107- self ._fetcher .reset_offsets_if_needed (partitions )
1110+ self ._fetcher .reset_offsets_if_needed (partitions , timeout_ms )
11081111
11091112 if not self ._subscription .has_all_fetch_positions ():
11101113 # if we still don't have offsets for all partitions, then we should either seek
@@ -1115,7 +1118,8 @@ def _update_fetch_positions(self, partitions):
11151118 self ._coordinator .refresh_committed_offsets_if_needed ()
11161119
11171120 # Then, do any offset lookups in case some positions are not known
1118- self ._fetcher .update_fetch_positions (partitions )
1121+ update_timeout_ms = 1000 * (end_time - time .time ())
1122+ self ._fetcher .update_fetch_positions (partitions , update_timeout_ms )
11191123
11201124 def _message_generator_v2 (self ):
11211125 timeout_ms = 1000 * (self ._consumer_timeout - time .time ())
@@ -1145,7 +1149,8 @@ def _message_generator(self):
11451149 # Fetch offsets for any subscribed partitions that we arent tracking yet
11461150 if not self ._subscription .has_all_fetch_positions ():
11471151 partitions = self ._subscription .missing_fetch_positions ()
1148- self ._update_fetch_positions (partitions )
1152+ update_timeout_ms = 1000 * (self ._consumer_timeout - time .time ())
1153+ self ._update_fetch_positions (partitions , update_timeout_ms )
11491154
11501155 poll_ms = min ((1000 * (self ._consumer_timeout - time .time ())), self .config ['retry_backoff_ms' ])
11511156 self ._client .poll (timeout_ms = poll_ms )
0 commit comments