@@ -125,7 +125,8 @@ static bool timed_write(int sock, void *data, size_t len, int timeout_ms)
125125 if (newbytes == -1 )
126126 {
127127 if (errno == EAGAIN ) {
128- if (poll_until_writable (sock , timeout_ms - msec (now - start ))) {
128+ int remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
129+ if (poll_until_writable (sock , remaining_ms )) {
129130 continue ;
130131 }
131132 }
@@ -157,7 +158,8 @@ static bool timed_read(int sock, void *data, size_t len, int timeout_ms)
157158 if (newbytes == -1 )
158159 {
159160 if (errno == EAGAIN ) {
160- if (poll_until_readable (sock , timeout_ms - msec (now - start ))) {
161+ int remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
162+ if (poll_until_readable (sock , remaining_ms )) {
161163 continue ;
162164 }
163165 }
@@ -224,8 +226,9 @@ static bool connect_leader(int timeout_ms)
224226 while ((elapsed_ms <= timeout_ms ) || (timeout_ms == -1 ))
225227 {
226228 TimestampTz past = now ;
229+ int remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - elapsed_ms ;
227230
228- if (poll_until_writable (sd , timeout_ms - elapsed_ms ))
231+ if (poll_until_writable (sd , remaining_ms ))
229232 {
230233 int err ;
231234 socklen_t optlen = sizeof (err );
@@ -301,7 +304,7 @@ raftable_sql_get(PG_FUNCTION_ARGS)
301304
302305static bool try_sending_update (RaftableUpdate * ru , size_t size , int timeout_ms )
303306{
304- int s , status ;
307+ int s , status , remaining_ms ;
305308 TimestampTz start , now ;
306309
307310 now = start = GetCurrentTimestamp ();
@@ -310,45 +313,49 @@ static bool try_sending_update(RaftableUpdate *ru, size_t size, int timeout_ms)
310313 if (s < 0 ) return false;
311314
312315 now = GetCurrentTimestamp ();
316+ remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
313317 if ((timeout_ms != -1 ) && (msec (now - start ) > timeout_ms ))
314318 {
315319 elog (WARNING , "update: connect() timed out" );
316320 return false;
317321 }
318322
319- if (!timed_write (s , & size , sizeof (size ), timeout_ms - msec ( now - start ) ))
323+ if (!timed_write (s , & size , sizeof (size ), remaining_ms ))
320324 {
321325 elog (WARNING , "failed to send the update size to the leader" );
322326 return false;
323327 }
324328
325329 now = GetCurrentTimestamp ();
330+ remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
326331 if ((timeout_ms != -1 ) && (msec (now - start ) > timeout_ms ))
327332 {
328333 elog (WARNING , "update: send(size) timed out" );
329334 return false;
330335 }
331336
332- if (!timed_write (s , ru , size , timeout_ms - msec ( now - start ) ))
337+ if (!timed_write (s , ru , size , remaining_ms ))
333338 {
334339 elog (WARNING , "failed to send the update to the leader" );
335340 return false;
336341 }
337342
338343 now = GetCurrentTimestamp ();
344+ remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
339345 if ((timeout_ms != -1 ) && (msec (now - start ) > timeout_ms ))
340346 {
341347 elog (WARNING , "update: send(body) timed out" );
342348 return false;
343349 }
344350
345- if (!timed_read (s , & status , sizeof (status ), timeout_ms - msec ( now - start ) ))
351+ if (!timed_read (s , & status , sizeof (status ), remaining_ms ))
346352 {
347353 elog (WARNING , "failed to recv the update status from the leader" );
348354 return false;
349355 }
350356
351357 now = GetCurrentTimestamp ();
358+ remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - msec (now - start );
352359 if ((timeout_ms != -1 ) && (msec (now - start ) > timeout_ms ))
353360 {
354361 elog (WARNING , "update: recv(status) timed out" );
@@ -396,7 +403,8 @@ bool raftable_set(const char *key, const char *value, size_t vallen, int timeout
396403 while ((elapsed_ms <= timeout_ms ) || (timeout_ms == -1 ))
397404 {
398405 TimestampTz past = now ;
399- if (try_sending_update (ru , size , timeout_ms - elapsed_ms ))
406+ int remaining_ms = (timeout_ms == -1 ) ? -1 : timeout_ms - elapsed_ms ;
407+ if (try_sending_update (ru , size , remaining_ms ))
400408 {
401409 pfree (ru );
402410 return true;
0 commit comments