# [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping

5 messages from 2011-10-29 to 2011-11-02. Participants: Mika Fischer, Daniel Stenberg, Junio C Hamano.
Thread: https://gitlist.dev/t/28797

## Mika Fischer, 2011-10-29 15:20

Subject: [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping
Message-ID: <1319901621-482-1-git-send-email-mika.fischer@zoopnet.de>
URL: https://gitlist.dev/e/1319901621-482-1-git-send-email-mika.fischer%40zoopnet.de

```
Previously, when nothing could be read from the connections curl had
open, git would just sleep unconditionally for 50ms. This patch changes
this behavior and instead obtains the recommended timeout and the actual
file descriptors from curl. This should eliminate time spent sleeping when
data could actually be read/written on the socket.

Signed-off-by: Mika Fischer <mika.fischer@zoopnet.de>
---
 http.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/http.c b/http.c
index a4bc770..12180f3 100644
--- a/http.c
+++ b/http.c
@@ -649,6 +649,7 @@ void run_active_slot(struct active_request_slot *slot)
 	fd_set excfds;
 	int max_fd;
 	struct timeval select_timeout;
+	long int curl_timeout;
 	int finished = 0;
 
 	slot->finished = &finished;
@@ -664,14 +665,24 @@ void run_active_slot(struct active_request_slot *slot)
 		}
 
 		if (slot->in_use && !data_received) {
-			max_fd = 0;
+			curl_multi_timeout(curlm, &curl_timeout);
+			if (curl_timeout == 0) {
+				continue;
+			} else if (curl_timeout == -1) {
+				select_timeout.tv_sec  = 0;
+				select_timeout.tv_usec = 50000;
+			} else {
+				select_timeout.tv_sec  =  curl_timeout / 1000;
+				select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
+			}
+
+			max_fd = -1;
 			FD_ZERO(&readfds);
 			FD_ZERO(&writefds);
 			FD_ZERO(&excfds);
-			select_timeout.tv_sec = 0;
-			select_timeout.tv_usec = 50000;
-			select(max_fd, &readfds, &writefds,
-			       &excfds, &select_timeout);
+			curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
+
+			select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
 		}
 	}
 #else
-- 
1.7.7.1.489.g1fee

```

## Daniel Stenberg, 2011-10-29 20:33

Subject: Re: [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping
Message-ID: <alpine.DEB.2.00.1110292230500.28196@tvnag.unkk.fr>
URL: https://gitlist.dev/e/alpine.DEB.2.00.1110292230500.28196%40tvnag.unkk.fr
In-Reply-To: <1319901621-482-1-git-send-email-mika.fischer@zoopnet.de>

```
On Sat, 29 Oct 2011, Mika Fischer wrote:

> Previously, when nothing could be read from the connections curl had open, 
> git would just sleep unconditionally for 50ms. This patch changes this 
> behavior and instead obtains the recommended timeout and the actual file 
> descriptors from curl. This should eliminate time spent sleeping when data 
> could actually be read/written on the socket.

It looks fine to me, from a libcurl perspective. I only have one comment about 
this:

> +			curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
> +
> +			select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);

At times, curl_multi_fdset() might return -1 in max_fd, as when there's no 
internal socket around to provide to the application to wait for.

Calling select() with max_fd+1 (== 0) will then not be appreciated by all 
implementations of select() so that case should probably also be covered by 
the 50ms sleep approach...

-- 

  / daniel.haxx.se

```

## Mika Fischer, 2011-10-30 07:19

Subject: Re: [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping
Message-ID: <CAOs=hR+YuF+HP0n0132Ktm3RdeWsnVp0Bgt89LNn+VyT6W0mcw@mail.gmail.com>
URL: https://gitlist.dev/e/CAOs%3DhR%2BYuF%2BHP0n0132Ktm3RdeWsnVp0Bgt89LNn%2BVyT6W0mcw%40mail.gmail.com
In-Reply-To: <alpine.DEB.2.00.1110292230500.28196@tvnag.unkk.fr>

```
On Sat, Oct 29, 2011 at 22:33, Daniel Stenberg <daniel@haxx.se> wrote:
>> +                       curl_multi_fdset(curlm, &readfds, &writefds,
>> &excfds, &max_fd);
>> +
>> +                       select(max_fd+1, &readfds, &writefds, &excfds,
>> &select_timeout);
>
> At times, curl_multi_fdset() might return -1 in max_fd, as when there's no
> internal socket around to provide to the application to wait for.
>
> Calling select() with max_fd+1 (== 0) will then not be appreciated by all
> implementations of select() so that case should probably also be covered by
> the 50ms sleep approach...

Actually, the 50ms sleep was also implemented using select(0, ...)
before the patch. I tried to keep the previous behavior when curl does
not give us any information.
I assumed that the select(0, ...) was some portable way to sleep with
microsecond granularity.
Is there some other way to tell select not to check any fds, or should
I just call select(1, ...)?

Best,
 Mika

```

## Mika Fischer, 2011-11-02 08:21

Subject: Re: [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping
Message-ID: <CAOs=hR+u_MrHK4iNFZj4pLVhZ6-_75YpqN7tqWnSjh+di8Lzxw@mail.gmail.com>
URL: https://gitlist.dev/e/CAOs%3DhR%2Bu_MrHK4iNFZj4pLVhZ6-_75YpqN7tqWnSjh%2Bdi8Lzxw%40mail.gmail.com
In-Reply-To: <CAOs=hR+YuF+HP0n0132Ktm3RdeWsnVp0Bgt89LNn+VyT6W0mcw@mail.gmail.com>

```
>> Calling select() with max_fd+1 (== 0) will then not be appreciated by all
>> implementations of select() so that case should probably also be covered by
>> the 50ms sleep approach...
>
> Actually, the 50ms sleep was also implemented using select(0, ...)
> before the patch. I tried to keep the previous behavior when curl does
> not give us any information.
> I assumed that the select(0, ...) was some portable way to sleep with
> microsecond granularity.

Upon a bit of research, it seems that select(0, ...) is indeed quite
commonly used. So I'd just keep it as it was unless you know of a
problem it causes.

Since I'm new here, I don't really know what the next steps are for
the patch, should I just wait? Or send it directly to someone?

Best,
 Mika

```

## Junio C Hamano, 2011-11-02 09:17

Subject: Re: [PATCH] http.c: Use curl_multi_fdset to select on curl fds instead of just sleeping
Message-ID: <7v39e6lw71.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v39e6lw71.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CAOs=hR+u_MrHK4iNFZj4pLVhZ6-_75YpqN7tqWnSjh+di8Lzxw@mail.gmail.com>

```
Mika Fischer <mika.fischer@zoopnet.de> writes:

> Since I'm new here, I don't really know what the next steps are for
> the patch, should I just wait? Or send it directly to someone?

Resend to the list for re-evaluation, and then we can take it from there.

Thanks.

```
