#1371 Wisp persistent connection

tonsky Thu 6 Jan 2011

Looking into WispActor, I’ve found that there’s a possible keep-alive misunderstanding:

// if using HTTP 1.1 and client specified using keep-alives,
// then setup response to be persistent for pipelining
if (req.version === ver11 && 
    req.headers.get("Connection", "").equalsIgnoreCase("keep-alive"))
{
  res.isPersistent = true
  res.headers["Connection"] = "keep-alive"
}

Here, you keep connection persistent only if it’s HTTP/1.1 and Connection: keep-alive header is present. But according to RFC 2616, for HTTP/1.1 all connections must be treated as persistent unless Connection: close is specified explicitly.

Connection: keep-alive header is only required for HTTP/1.0 protocol as it’s not a default behaviour to keep connection persistent in HTTP/1.0, so it must be explicitly negotiated (see here).

So the correct code may look like this:

if (req.version === ver11 && 
    !req.headers.get("Connection", "").equalsIgnoreCase("close"))
{
  res.isPersistent = true
}

if (req.version === ver10 && 
    req.headers.get("Connection", "").equalsIgnoreCase("keep-alive"))
{
  res.isPersistent = true
  res.headers["Connection"] = "Keep-Alive"
}

brian Thu 6 Jan 2011

The problem is most clients don't actually do pipelining, and you don't actually want to leave the socket open unless you are absolutely sure the client is going to reuse the socket. So the safest approach is to assume no pipelining and close the socket immediately after the request. It never hurts to assume no pipelining and be wrong, but vise versa can be a big problem.

SlimerDude Thu 23 May 2013

Supporting Brian's reasoning, here's an interesting article:

HTTP KeepAlive Considered Harmful

Isn’t that a wonderfully alarmist title? A better one would be “HTTP KeepAlive harmful for modern high-traffic low-latency low-footprint websites”. But then you’d have fallen asleep by now, wouldn’t you.

Login or Signup to reply.