OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 // The close queue handles graceful closing of HTTP connections. When | 7 // The close queue handles graceful closing of HTTP connections. When |
8 // a connection is added to the queue it will enter a wait state | 8 // a connection is added to the queue it will enter a wait state |
9 // waiting for all data written and possibly socket shutdown from | 9 // waiting for all data written and possibly socket shutdown from |
10 // peer. | 10 // peer. |
(...skipping 1715 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1726 _SocketConnection(String this._host, | 1726 _SocketConnection(String this._host, |
1727 int this._port, | 1727 int this._port, |
1728 Socket this._socket); | 1728 Socket this._socket); |
1729 | 1729 |
1730 void _markReturned() { | 1730 void _markReturned() { |
1731 // Any activity on the socket while waiting in the pool will | 1731 // Any activity on the socket while waiting in the pool will |
1732 // invalidate the connection os that it is not reused. | 1732 // invalidate the connection os that it is not reused. |
1733 _socket.onData = _invalidate; | 1733 _socket.onData = _invalidate; |
1734 _socket.onClosed = _invalidate; | 1734 _socket.onClosed = _invalidate; |
1735 _socket.onError = (_) => _invalidate(); | 1735 _socket.onError = (_) => _invalidate(); |
1736 _returnTime = new Date.now(); | 1736 _returnTime = new DateTime.now(); |
1737 _httpClientConnection = null; | 1737 _httpClientConnection = null; |
1738 } | 1738 } |
1739 | 1739 |
1740 void _markRetrieved() { | 1740 void _markRetrieved() { |
1741 _socket.onData = null; | 1741 _socket.onData = null; |
1742 _socket.onClosed = null; | 1742 _socket.onClosed = null; |
1743 _socket.onError = null; | 1743 _socket.onError = null; |
1744 _httpClientConnection = null; | 1744 _httpClientConnection = null; |
1745 } | 1745 } |
1746 | 1746 |
1747 void _close() { | 1747 void _close() { |
1748 _socket.onData = null; | 1748 _socket.onData = null; |
1749 _socket.onClosed = null; | 1749 _socket.onClosed = null; |
1750 _socket.onError = null; | 1750 _socket.onError = null; |
1751 _httpClientConnection = null; | 1751 _httpClientConnection = null; |
1752 _socket.close(); | 1752 _socket.close(); |
1753 } | 1753 } |
1754 | 1754 |
1755 Duration _idleTime(Date now) => now.difference(_returnTime); | 1755 Duration _idleTime(DateTime now) => now.difference(_returnTime); |
1756 | 1756 |
1757 bool get _fromPool => _returnTime != null; | 1757 bool get _fromPool => _returnTime != null; |
1758 | 1758 |
1759 void _invalidate() { | 1759 void _invalidate() { |
1760 _valid = false; | 1760 _valid = false; |
1761 _close(); | 1761 _close(); |
1762 } | 1762 } |
1763 | 1763 |
1764 int get hashCode => _socket.hashCode; | 1764 int get hashCode => _socket.hashCode; |
1765 | 1765 |
1766 String _host; | 1766 String _host; |
1767 int _port; | 1767 int _port; |
1768 Socket _socket; | 1768 Socket _socket; |
1769 Date _returnTime; | 1769 DateTime _returnTime; |
1770 bool _valid = true; | 1770 bool _valid = true; |
1771 HttpClientConnection _httpClientConnection; | 1771 HttpClientConnection _httpClientConnection; |
1772 } | 1772 } |
1773 | 1773 |
1774 class _ProxyConfiguration { | 1774 class _ProxyConfiguration { |
1775 static const String PROXY_PREFIX = "PROXY "; | 1775 static const String PROXY_PREFIX = "PROXY "; |
1776 static const String DIRECT_PREFIX = "DIRECT"; | 1776 static const String DIRECT_PREFIX = "DIRECT"; |
1777 | 1777 |
1778 _ProxyConfiguration(String configuration) : proxies = new List<_Proxy>() { | 1778 _ProxyConfiguration(String configuration) : proxies = new List<_Proxy>() { |
1779 if (configuration == null) { | 1779 if (configuration == null) { |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2096 // Get or create the connection list for this key. | 2096 // Get or create the connection list for this key. |
2097 Queue sockets = _openSockets[key]; | 2097 Queue sockets = _openSockets[key]; |
2098 if (sockets == null) { | 2098 if (sockets == null) { |
2099 sockets = new Queue(); | 2099 sockets = new Queue(); |
2100 _openSockets[key] = sockets; | 2100 _openSockets[key] = sockets; |
2101 } | 2101 } |
2102 | 2102 |
2103 // If there is currently no eviction timer start one. | 2103 // If there is currently no eviction timer start one. |
2104 if (_evictionTimer == null) { | 2104 if (_evictionTimer == null) { |
2105 void _handleEviction(Timer timer) { | 2105 void _handleEviction(Timer timer) { |
2106 Date now = new Date.now(); | 2106 DateTime now = new DateTime.now(); |
2107 List<String> emptyKeys = new List<String>(); | 2107 List<String> emptyKeys = new List<String>(); |
2108 _openSockets.forEach( | 2108 _openSockets.forEach( |
2109 (String key, Queue<_SocketConnection> connections) { | 2109 (String key, Queue<_SocketConnection> connections) { |
2110 // As returned connections are added at the head of the | 2110 // As returned connections are added at the head of the |
2111 // list remove from the tail. | 2111 // list remove from the tail. |
2112 while (!connections.isEmpty) { | 2112 while (!connections.isEmpty) { |
2113 _SocketConnection socketConn = connections.last; | 2113 _SocketConnection socketConn = connections.last; |
2114 if (socketConn._idleTime(now).inMilliseconds > | 2114 if (socketConn._idleTime(now).inMilliseconds > |
2115 DEFAULT_EVICTION_TIMEOUT) { | 2115 DEFAULT_EVICTION_TIMEOUT) { |
2116 connections.removeLast(); | 2116 connections.removeLast(); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2300 | 2300 |
2301 | 2301 |
2302 class _RedirectInfo implements RedirectInfo { | 2302 class _RedirectInfo implements RedirectInfo { |
2303 const _RedirectInfo(int this.statusCode, | 2303 const _RedirectInfo(int this.statusCode, |
2304 String this.method, | 2304 String this.method, |
2305 Uri this.location); | 2305 Uri this.location); |
2306 final int statusCode; | 2306 final int statusCode; |
2307 final String method; | 2307 final String method; |
2308 final Uri location; | 2308 final Uri location; |
2309 } | 2309 } |
OLD | NEW |