OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/websockets/websocket_manager.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include <algorithm> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/callback.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/numerics/safe_conversions.h" |
| 16 #include "base/rand_util.h" |
| 17 #include "base/stl_util.h" |
| 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "net/url_request/url_request_context_getter.h" |
| 20 |
| 21 namespace content { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Max number of pending connections per WebSocketManager used for per-renderer |
| 26 // WebSocket throttling. |
| 27 const int kMaxPendingWebSocketConnections = 255; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 WebSocketManager::WebSocketManager(int process_id, |
| 32 StoragePartition* storage_partition) |
| 33 : process_id_(process_id), |
| 34 storage_partition_(storage_partition), |
| 35 num_pending_connections_(0), |
| 36 num_current_succeeded_connections_(0), |
| 37 num_previous_succeeded_connections_(0), |
| 38 num_current_failed_connections_(0), |
| 39 num_previous_failed_connections_(0) {} |
| 40 |
| 41 WebSocketManager::~WebSocketManager() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 |
| 44 for (auto impl : impls_) { |
| 45 impl->GoAway(); |
| 46 delete impl; |
| 47 } |
| 48 } |
| 49 |
| 50 void WebSocketManager::CreateWebSocket(int render_frame_id, |
| 51 mojom::WebSocketRequest request) { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 53 |
| 54 if (num_pending_connections_ >= kMaxPendingWebSocketConnections) { |
| 55 // Too many websockets! By returning here, we let |request| die, which |
| 56 // will be observed by the client as Mojo connection error. |
| 57 return; |
| 58 } |
| 59 |
| 60 // Keep all WebSocketImpls alive until either the client drops its |
| 61 // connection (see OnLostConnectionToClient) or we need to shutdown. |
| 62 |
| 63 impls_.insert(new WebSocketImpl(this, |
| 64 std::move(request), |
| 65 render_frame_id, |
| 66 CalculateDelay())); |
| 67 ++num_pending_connections_; |
| 68 |
| 69 if (!throttling_period_timer_.IsRunning()) { |
| 70 throttling_period_timer_.Start( |
| 71 FROM_HERE, |
| 72 base::TimeDelta::FromMinutes(2), |
| 73 this, |
| 74 &WebSocketManager::ThrottlingPeriodTimerCallback); |
| 75 } |
| 76 } |
| 77 |
| 78 // Calculate delay as described in the per-renderer WebSocket throttling |
| 79 // design doc: https://goo.gl/tldFNn |
| 80 base::TimeDelta WebSocketManager::CalculateDelay() const { |
| 81 int64_t f = num_previous_failed_connections_ + |
| 82 num_current_failed_connections_; |
| 83 int64_t s = num_previous_succeeded_connections_ + |
| 84 num_current_succeeded_connections_; |
| 85 int p = num_pending_connections_; |
| 86 return base::TimeDelta::FromMilliseconds( |
| 87 base::RandInt(1000, 5000) * |
| 88 (1 << std::min(p + f / (s + 1), INT64_C(16))) / 65536); |
| 89 } |
| 90 |
| 91 void WebSocketManager::ThrottlingPeriodTimerCallback() { |
| 92 num_previous_failed_connections_ = num_current_failed_connections_; |
| 93 num_current_failed_connections_ = 0; |
| 94 |
| 95 num_previous_succeeded_connections_ = num_current_succeeded_connections_; |
| 96 num_current_succeeded_connections_ = 0; |
| 97 |
| 98 if (num_pending_connections_ == 0 && |
| 99 num_previous_failed_connections_ == 0 && |
| 100 num_previous_succeeded_connections_ == 0) { |
| 101 throttling_period_timer_.Stop(); |
| 102 } |
| 103 } |
| 104 |
| 105 int WebSocketManager::GetClientProcessId() { |
| 106 return process_id_; |
| 107 } |
| 108 |
| 109 StoragePartition* WebSocketManager::GetStoragePartition() { |
| 110 return storage_partition_; |
| 111 } |
| 112 |
| 113 void WebSocketManager::OnReceivedResponseFromServer(WebSocketImpl* impl) { |
| 114 // The server accepted this WebSocket connection. |
| 115 impl->OnHandshakeSucceeded(); |
| 116 --num_pending_connections_; |
| 117 DCHECK_GE(num_pending_connections_, 0); |
| 118 ++num_current_succeeded_connections_; |
| 119 } |
| 120 |
| 121 void WebSocketManager::OnLostConnectionToClient(WebSocketImpl* impl) { |
| 122 // The client is no longer interested in this WebSocket. |
| 123 if (!impl->handshake_succeeded()) { |
| 124 // Update throttling counters (failure). |
| 125 --num_pending_connections_; |
| 126 DCHECK_GE(num_pending_connections_, 0); |
| 127 ++num_current_failed_connections_; |
| 128 } |
| 129 impls_.erase(impl); |
| 130 delete impl; |
| 131 } |
| 132 |
| 133 } // namespace content |
OLD | NEW |