From 20bab5efe6278475535e7a3e8b08e1e6b05c0db4 Mon Sep 17 00:00:00 2001 From: Jason Weathered Date: Mon, 31 Jan 2022 16:17:05 +1000 Subject: [PATCH] Fix high CPU usage when waiting with a timeout on Ruby < 3.0 --- ext/pg_connection.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/pg_connection.c b/ext/pg_connection.c index a3f8e3866..4f9138b17 100644 --- a/ext/pg_connection.c +++ b/ext/pg_connection.c @@ -2241,7 +2241,7 @@ pg_rb_thread_io_wait(VALUE io, VALUE events, VALUE timeout) { GetOpenFile((io), fptr); if( !NIL_P(timeout) ){ ptimeout.tv_sec = (time_t)(NUM2DBL(timeout)); - ptimeout.tv_usec = (time_t)(NUM2DBL(timeout) - (double)ptimeout.tv_sec); + ptimeout.tv_usec = (time_t)((NUM2DBL(timeout) - (double)ptimeout.tv_sec) * 1e6); gettimeofday(&currtime, NULL); timeradd(&currtime, &ptimeout, &aborttime); @@ -2332,7 +2332,7 @@ pg_rb_io_wait(VALUE io, VALUE events, VALUE timeout) { GetOpenFile((io), fptr); if( !NIL_P(timeout) ){ waittime.tv_sec = (time_t)(NUM2DBL(timeout)); - waittime.tv_usec = (time_t)(NUM2DBL(timeout) - (double)waittime.tv_sec); + waittime.tv_usec = (time_t)((NUM2DBL(timeout) - (double)waittime.tv_sec) * 1e6); } res = rb_wait_for_single_fd(fptr->fd, NUM2UINT(events), NIL_P(timeout) ? NULL : &waittime);