3

I'm playing with async methods in an ApiController, and the behaviour surprises me. I invoke the following method:

// GET api/values/{id}
public async Task<object> Get(int id)
{
    var received = DateTime.Now;
    await Task.Delay(10000);
    var finished = DateTime.Now;
    return new { id, received, finished };
}

from four browser windows with the following URLs in order, with about a one second delay between each:

http://localhost:55571/api/values/1
http://localhost:55571/api/values/2
http://localhost:55571/api/values/3
http://localhost:55571/api/values/2

The first URL comes back, as expected, ten seconds after I launch it, followed shortly thereafter by the second and third URLs. However, the fourth URL, which is identical to the second URL, returns ten seconds after the second URL returns.

This suggests to me that something between the browser and my code is serialising requests either to the same URL, or the same set of parameters, or something else (and what about POST, PUT, etc.?). Rather than reverse-engineering this, I'd like to ask...

Is this behaviour documented somewhere?

EDIT: I tried clutching at this straw, to no avail.

EDIT: As requested in a comment, the timings are (roughly) as follows:

 0 s: GET …/values/1
 1 s: GET …/values/2
 2 s: GET …/values/3
 3 s: GET …/values/2 (2nd instance)
10 s: GOT …/values/1
11 s: GOT …/values/2
12 s: GOT …/values/3
21 s: GOT …/values/2 (2nd instance)
Community
  • 1
  • 1
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Could you show us an example of the timing, including when did the client start the request and when was it finished? – svick Oct 08 '12 at 09:48

1 Answers1

2

Browsers will voluntarily limit the number of connections they have to a host. RFC 2616 (HTTP 1.1) recommends a limit of two persistent connections; it appears your browser is using a limit of three.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • That's a good observation. However, I see the same behaviour if I load just the one URL in two different browser windows. – Marcelo Cantos Oct 08 '12 at 11:49
  • What browser? I would expect IE, for example, to honor the restriction, since all browser windows use WinINet.dll which does the actual limiting. – Stephen Cleary Oct 08 '12 at 11:53
  • I use Google Chrome. I'm at the wrong computer right now, but I'll try more combinations tomorrow along the lines of your suggestion. – Marcelo Cantos Oct 08 '12 at 12:08
  • I finally got around to testing this, and you were right. I must have hit upon just the right combination to fool myself (plus, it didn't occur to me that it might be the client (slaps forehead)). With four wget invocations, the problem vanished. – Marcelo Cantos Oct 22 '12 at 23:20