Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(523)

Unified Diff: sdk/lib/io/http_impl.dart

Issue 22839013: Add HttpServer.idleTimeout for closing idle keep-alive connections. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/io/http.dart ('k') | tests/standalone/io/http_server_idle_timeout_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/io/http_impl.dart
diff --git a/sdk/lib/io/http_impl.dart b/sdk/lib/io/http_impl.dart
index 615c59aeb873ab305fed79cb737feeef988eff91..7eaf153f32f879ecb7e82a556766b7bf0448e542 100644
--- a/sdk/lib/io/http_impl.dart
+++ b/sdk/lib/io/http_impl.dart
@@ -1824,14 +1824,17 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> {
final _HttpServer _httpServer;
final _HttpParser _httpParser;
StreamSubscription _subscription;
+ Timer _idleTimer;
Future _streamFuture;
_HttpConnection(Socket this._socket, _HttpServer this._httpServer)
: _httpParser = new _HttpParser.requestParser() {
+ _startTimeout();
_socket.pipe(_httpParser);
_subscription = _httpParser.listen(
(incoming) {
+ _stopTimeout();
// If the incoming was closed, close the connection.
incoming.dataDone.then((closing) {
if (closing) destroy();
@@ -1854,6 +1857,7 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> {
request.persistentConnection &&
incoming.fullBodyRead) {
_state = _IDLE;
+ _startTimeout();
// Resume the subscription for incoming requests as the
// request is now processed.
_subscription.resume();
@@ -1879,7 +1883,21 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> {
});
}
+ void _startTimeout() {
+ assert(_state == _IDLE);
+ _stopTimeout();
+ if (_httpServer.idleTimeout == null) return;
+ _idleTimer = new Timer(_httpServer.idleTimeout, () {
+ destroy();
+ });
+ }
+
+ void _stopTimeout() {
+ if (_idleTimer != null) _idleTimer.cancel();
+ }
+
void destroy() {
+ _stopTimeout();
if (_state == _CLOSING || _state == _DETACHED) return;
_state = _CLOSING;
_socket.destroy();
@@ -1887,6 +1905,7 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> {
}
Future<Socket> detachSocket() {
+ _stopTimeout();
_state = _DETACHED;
// Remove connection from server.
_httpServer._connectionClosed(this);
@@ -1911,6 +1930,8 @@ class _HttpConnection extends LinkedListEntry<_HttpConnection> {
class _HttpServer extends Stream<HttpRequest> implements HttpServer {
String serverHeader = _getHttpVersion();
+ Duration idleTimeout = const Duration(seconds: 120);
+
static Future<HttpServer> bind(address, int port, int backlog) {
return ServerSocket.bind(address, port, backlog: backlog).then((socket) {
return new _HttpServer._(socket, true);
« no previous file with comments | « sdk/lib/io/http.dart ('k') | tests/standalone/io/http_server_idle_timeout_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698