File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed
Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ use std::mem::ManuallyDrop;
55use std:: os:: raw:: c_void;
66use std:: ptr;
77use std:: sync:: Arc ;
8- use std:: time:: Duration ;
8+ use std:: time:: { Duration , Instant } ;
99
1010use log:: { error, warn} ;
1111use rdkafka_sys as rdsys;
@@ -123,6 +123,7 @@ where
123123 queue : & NativeQueue ,
124124 timeout : T ,
125125 ) -> Option < KafkaResult < BorrowedMessage < ' _ > > > {
126+ let now = Instant :: now ( ) ;
126127 let mut timeout = timeout. into ( ) ;
127128 let min_poll_interval = self . context ( ) . main_queue_min_poll_interval ( ) ;
128129 loop {
@@ -158,10 +159,10 @@ where
158159 }
159160 }
160161
161- if op_timeout >= timeout {
162+ timeout = timeout. saturating_sub ( now. elapsed ( ) ) ;
163+ if timeout. is_zero ( ) {
162164 return None ;
163165 }
164- timeout -= op_timeout;
165166 }
166167 }
167168
Original file line number Diff line number Diff line change @@ -48,6 +48,22 @@ impl Timeout {
4848 Timeout :: Never => -1 ,
4949 }
5050 }
51+
52+ /// Saturating `Duration` subtraction to Timeout.
53+ pub ( crate ) fn saturating_sub ( & self , rhs : Duration ) -> Timeout {
54+ match ( self , rhs) {
55+ ( Timeout :: After ( lhs) , rhs) => Timeout :: After ( lhs. saturating_sub ( rhs) ) ,
56+ ( Timeout :: Never , _) => Timeout :: Never ,
57+ }
58+ }
59+
60+ /// Returns `true` if the timeout is zero.
61+ pub ( crate ) fn is_zero ( & self ) -> bool {
62+ match self {
63+ Timeout :: After ( d) => d. is_zero ( ) ,
64+ Timeout :: Never => false ,
65+ }
66+ }
5167}
5268
5369impl std:: ops:: SubAssign for Timeout {
You can’t perform that action at this time.
0 commit comments