def _new_offset(self, tp: TP) -> Optional[int]: # get the new offset for this tp, by going through # its list of acked messages. acked = self._acked[tp] # We iterate over it until we find a gap # then return the offset before that. # For example if acked[tp] is: # 1 2 3 4 5 6 7 8 9 # the return value will be: 9 # If acked[tp] is: # 34 35 36 40 41 42 43 44 # ^--- gap # the return value will be: 36 if acked: max_offset = max(acked) gap_for_tp = self._gap[tp] if gap_for_tp: gap_index = next( (i for i, x in enumerate(gap_for_tp) if x > max_offset), len(gap_for_tp)) gaps = gap_for_tp[:gap_index] acked.extend(gaps) gap_for_tp[:gap_index] = [] acked.sort() # Note: acked is always kept sorted. # find first list of consecutive numbers batch = next(consecutive_numbers(acked)) # remove them from the list to clean up. acked[:len(batch) - 1] = [] self._acked_index[tp].difference_update(batch) # return the highest commit offset return batch[-1] return None
def test_consecutive_numbers(numbers, expected): assert next(consecutive_numbers(numbers), None) == expected