xp-framework / http

HTTP protocol support for the XP Framework
2 stars 2 forks source link

Keepalive #13

Open thekid opened 8 years ago

thekid commented 8 years ago

HTTP Connections should support Keep-Alive.

Further reading

http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.keepalive(v=vs.110).aspx https://github.com/nodejs/node-v0.x-archive/issues/4769 https://github.com/billywhizz/node-httpclient

thekid commented 8 years ago

:bulb: Idea no. 1

$conn= new HttpConnection(...);
$conn->useKeepAlive();  // Use default strategy
$conn->useKeepAlive(newinstance(KeepAlive::class, [], [
  'timeout' => function($request, $response) {
    if ('example.com' === $request->url->getHost()) {
      return 0.0;   // Close immediately
    } else {
      return 30.0;  // Keep open for 30.0 seconds
    }
  }
]));
thekid commented 8 years ago

:bulb: Idea no. 2

$conn= new HttpConnection(...);
$conn->usingKeepAlive(function() {
  $r1= $conn->get(...);
  $r1->abort();  // Do not process content until end

  $r2= $conn->post(...);
  Streams::readAll($r2->in());
});
thekid commented 8 years ago

:bulb: Idea no. 3

$conn= new HttpConnection(...);
$req= $conn->create(new HttpRequest())->keepAlive();
$req= $conn->create(new HttpRequest())->keepAlive(newinstance(KeepAlive::class, [], [
  // See idea #1 above
])));
$res= $conn->send($req);
thekid commented 8 years ago

:bulb: Idea no. 4

$conn= new HttpConnection(...);
$conn->get(..., function($res) {
  $res->abort();

  // or: Streams::readAll($res->in());
});
thekid commented 8 years ago

:bulb: Idea no. 5

$conn= new HttpConnection(...);
$pool= $conn->pool(5);
$pool->get()->then(function($res) {
  // Uses first socket
});
$pool->post()->then(function($res) {
  // Uses first *free* socket within the pool
});
thekid commented 6 years ago

See #22 - this is now implemented by passing Connection: keep-alive to requests. Sockets are reused when all data from a previous response has been consumed, except if the server issues a Connection: close.