OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 1737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1748 _SocketConnection(String this._host, | 1748 _SocketConnection(String this._host, |
1749 int this._port, | 1749 int this._port, |
1750 Socket this._socket); | 1750 Socket this._socket); |
1751 | 1751 |
1752 void _markReturned() { | 1752 void _markReturned() { |
1753 // Any activity on the socket while waiting in the pool will | 1753 // Any activity on the socket while waiting in the pool will |
1754 // invalidate the connection os that it is not reused. | 1754 // invalidate the connection os that it is not reused. |
1755 _socket.onData = _invalidate; | 1755 _socket.onData = _invalidate; |
1756 _socket.onClosed = _invalidate; | 1756 _socket.onClosed = _invalidate; |
1757 _socket.onError = (_) => _invalidate(); | 1757 _socket.onError = (_) => _invalidate(); |
1758 _returnTime = new Date.now(); | 1758 _returnTime = new DateTime.now(); |
1759 _httpClientConnection = null; | 1759 _httpClientConnection = null; |
1760 } | 1760 } |
1761 | 1761 |
1762 void _markRetrieved() { | 1762 void _markRetrieved() { |
1763 _socket.onData = null; | 1763 _socket.onData = null; |
1764 _socket.onClosed = null; | 1764 _socket.onClosed = null; |
1765 _socket.onError = null; | 1765 _socket.onError = null; |
1766 _httpClientConnection = null; | 1766 _httpClientConnection = null; |
1767 } | 1767 } |
1768 | 1768 |
1769 void _close() { | 1769 void _close() { |
1770 _socket.onData = null; | 1770 _socket.onData = null; |
1771 _socket.onClosed = null; | 1771 _socket.onClosed = null; |
1772 _socket.onError = null; | 1772 _socket.onError = null; |
1773 _httpClientConnection = null; | 1773 _httpClientConnection = null; |
1774 _socket.close(); | 1774 _socket.close(); |
1775 } | 1775 } |
1776 | 1776 |
1777 Duration _idleTime(Date now) => now.difference(_returnTime); | 1777 Duration _idleTime(DateTime now) => now.difference(_returnTime); |
1778 | 1778 |
1779 bool get _fromPool => _returnTime != null; | 1779 bool get _fromPool => _returnTime != null; |
1780 | 1780 |
1781 void _invalidate() { | 1781 void _invalidate() { |
1782 _valid = false; | 1782 _valid = false; |
1783 _close(); | 1783 _close(); |
1784 } | 1784 } |
1785 | 1785 |
1786 int get hashCode => _socket.hashCode; | 1786 int get hashCode => _socket.hashCode; |
1787 | 1787 |
1788 String _host; | 1788 String _host; |
1789 int _port; | 1789 int _port; |
1790 Socket _socket; | 1790 Socket _socket; |
1791 Date _returnTime; | 1791 DateTime _returnTime; |
1792 bool _valid = true; | 1792 bool _valid = true; |
1793 HttpClientConnection _httpClientConnection; | 1793 HttpClientConnection _httpClientConnection; |
1794 } | 1794 } |
1795 | 1795 |
1796 class _ProxyConfiguration { | 1796 class _ProxyConfiguration { |
1797 static const String PROXY_PREFIX = "PROXY "; | 1797 static const String PROXY_PREFIX = "PROXY "; |
1798 static const String DIRECT_PREFIX = "DIRECT"; | 1798 static const String DIRECT_PREFIX = "DIRECT"; |
1799 | 1799 |
1800 _ProxyConfiguration(String configuration) : proxies = new List<_Proxy>() { | 1800 _ProxyConfiguration(String configuration) : proxies = new List<_Proxy>() { |
1801 if (configuration == null) { | 1801 if (configuration == null) { |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2118 // Get or create the connection list for this key. | 2118 // Get or create the connection list for this key. |
2119 Queue sockets = _openSockets[key]; | 2119 Queue sockets = _openSockets[key]; |
2120 if (sockets == null) { | 2120 if (sockets == null) { |
2121 sockets = new Queue(); | 2121 sockets = new Queue(); |
2122 _openSockets[key] = sockets; | 2122 _openSockets[key] = sockets; |
2123 } | 2123 } |
2124 | 2124 |
2125 // If there is currently no eviction timer start one. | 2125 // If there is currently no eviction timer start one. |
2126 if (_evictionTimer == null) { | 2126 if (_evictionTimer == null) { |
2127 void _handleEviction(Timer timer) { | 2127 void _handleEviction(Timer timer) { |
2128 Date now = new Date.now(); | 2128 DateTime now = new DateTime.now(); |
2129 List<String> emptyKeys = new List<String>(); | 2129 List<String> emptyKeys = new List<String>(); |
2130 _openSockets.forEach( | 2130 _openSockets.forEach( |
2131 (String key, Queue<_SocketConnection> connections) { | 2131 (String key, Queue<_SocketConnection> connections) { |
2132 // As returned connections are added at the head of the | 2132 // As returned connections are added at the head of the |
2133 // list remove from the tail. | 2133 // list remove from the tail. |
2134 while (!connections.isEmpty) { | 2134 while (!connections.isEmpty) { |
2135 _SocketConnection socketConn = connections.last; | 2135 _SocketConnection socketConn = connections.last; |
2136 if (socketConn._idleTime(now).inMilliseconds > | 2136 if (socketConn._idleTime(now).inMilliseconds > |
2137 DEFAULT_EVICTION_TIMEOUT) { | 2137 DEFAULT_EVICTION_TIMEOUT) { |
2138 connections.removeLast(); | 2138 connections.removeLast(); |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2322 | 2322 |
2323 | 2323 |
2324 class _RedirectInfo implements RedirectInfo { | 2324 class _RedirectInfo implements RedirectInfo { |
2325 const _RedirectInfo(int this.statusCode, | 2325 const _RedirectInfo(int this.statusCode, |
2326 String this.method, | 2326 String this.method, |
2327 Uri this.location); | 2327 Uri this.location); |
2328 final int statusCode; | 2328 final int statusCode; |
2329 final String method; | 2329 final String method; |
2330 final Uri location; | 2330 final Uri location; |
2331 } | 2331 } |
OLD | NEW |