websocket_throttler.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2018 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. #include "services/network/websocket_throttler.h"
  5. #include <algorithm>
  6. #include "base/rand_util.h"
  7. #include "services/network/public/mojom/network_context.mojom-forward.h"
  8. namespace network {
  9. constexpr int WebSocketPerProcessThrottler::kMaxPendingWebSocketConnections;
  10. WebSocketPerProcessThrottler::PendingConnection::PendingConnection(
  11. base::WeakPtr<WebSocketPerProcessThrottler> throttler)
  12. : throttler_(std::move(throttler)) {
  13. DCHECK(throttler_);
  14. ++throttler_->num_pending_connections_;
  15. }
  16. WebSocketPerProcessThrottler::PendingConnection::PendingConnection(
  17. PendingConnection&& other)
  18. : throttler_(std::move(other.throttler_)) {
  19. other.throttler_ = nullptr;
  20. }
  21. WebSocketPerProcessThrottler::PendingConnection::~PendingConnection() {
  22. if (!throttler_)
  23. return;
  24. --throttler_->num_pending_connections_;
  25. ++throttler_->num_current_failed_connections_;
  26. }
  27. void WebSocketPerProcessThrottler::PendingConnection::OnCompleteHandshake() {
  28. DCHECK(throttler_);
  29. --throttler_->num_pending_connections_;
  30. ++throttler_->num_current_succeeded_connections_;
  31. throttler_ = nullptr;
  32. }
  33. WebSocketPerProcessThrottler::WebSocketPerProcessThrottler() {}
  34. WebSocketPerProcessThrottler::~WebSocketPerProcessThrottler() {}
  35. base::TimeDelta WebSocketPerProcessThrottler::CalculateDelay() const {
  36. int64_t f =
  37. num_previous_failed_connections_ + num_current_failed_connections_;
  38. int64_t s =
  39. num_previous_succeeded_connections_ + num_current_succeeded_connections_;
  40. int p = num_pending_connections_;
  41. return base::Milliseconds(base::RandInt(1000, 5000) *
  42. (1 << std::min(p + f / (s + 1), INT64_C(16))) /
  43. 65536);
  44. }
  45. WebSocketPerProcessThrottler::PendingConnection
  46. WebSocketPerProcessThrottler::IssuePendingConnectionTracker() {
  47. return PendingConnection(weak_factory_.GetWeakPtr());
  48. }
  49. bool WebSocketPerProcessThrottler::IsClean() const {
  50. return num_pending_connections_ == 0 &&
  51. num_current_succeeded_connections_ == 0 &&
  52. num_previous_succeeded_connections_ == 0 &&
  53. num_current_failed_connections_ == 0 &&
  54. num_previous_succeeded_connections_ == 0;
  55. }
  56. void WebSocketPerProcessThrottler::Roll() {
  57. num_previous_succeeded_connections_ = num_current_succeeded_connections_;
  58. num_previous_failed_connections_ = num_current_failed_connections_;
  59. num_current_succeeded_connections_ = 0;
  60. num_current_failed_connections_ = 0;
  61. }
  62. WebSocketThrottler::WebSocketThrottler() {}
  63. WebSocketThrottler::~WebSocketThrottler() {}
  64. bool WebSocketThrottler::HasTooManyPendingConnections(int process_id) const {
  65. auto it = per_process_throttlers_.find(process_id);
  66. if (it == per_process_throttlers_.end())
  67. return false;
  68. return it->second->HasTooManyPendingConnections();
  69. }
  70. base::TimeDelta WebSocketThrottler::CalculateDelay(int process_id) const {
  71. auto it = per_process_throttlers_.find(process_id);
  72. if (it == per_process_throttlers_.end())
  73. return base::TimeDelta();
  74. return it->second->CalculateDelay();
  75. }
  76. absl::optional<WebSocketThrottler::PendingConnection>
  77. WebSocketThrottler::IssuePendingConnectionTracker(int process_id) {
  78. if (process_id == mojom::kBrowserProcessId) {
  79. // The browser process is not throttled.
  80. return absl::nullopt;
  81. }
  82. auto it = per_process_throttlers_.find(process_id);
  83. if (it == per_process_throttlers_.end()) {
  84. it = per_process_throttlers_
  85. .insert(std::make_pair(
  86. process_id, std::make_unique<WebSocketPerProcessThrottler>()))
  87. .first;
  88. }
  89. if (!throttling_period_timer_.IsRunning()) {
  90. throttling_period_timer_.Start(FROM_HERE, base::Minutes(2), this,
  91. &WebSocketThrottler::OnTimer);
  92. }
  93. return it->second->IssuePendingConnectionTracker();
  94. }
  95. void WebSocketThrottler::OnTimer() {
  96. auto it = per_process_throttlers_.begin();
  97. while (it != per_process_throttlers_.end()) {
  98. it->second->Roll();
  99. if (it->second->IsClean()) {
  100. // We don't need the entry. Erase it.
  101. it = per_process_throttlers_.erase(it);
  102. } else {
  103. ++it;
  104. }
  105. }
  106. if (per_process_throttlers_.empty())
  107. throttling_period_timer_.Stop();
  108. }
  109. } // namespace network