Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / quic / quic_flow_controller.cc
blobd95541edbcc98ddda7ded242ec74637d27954b24
1 // Copyright 2014 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.
5 #include "net/quic/quic_flow_controller.h"
7 #include "base/basictypes.h"
8 #include "net/quic/quic_connection.h"
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_protocol.h"
12 namespace net {
14 #define ENDPOINT \
15 (perspective_ == Perspective::IS_SERVER ? "Server: " : "Client: ")
17 QuicFlowController::QuicFlowController(QuicConnection* connection,
18 QuicStreamId id,
19 Perspective perspective,
20 QuicStreamOffset send_window_offset,
21 QuicStreamOffset receive_window_offset,
22 QuicByteCount max_receive_window)
23 : connection_(connection),
24 id_(id),
25 perspective_(perspective),
26 bytes_consumed_(0),
27 highest_received_byte_offset_(0),
28 bytes_sent_(0),
29 send_window_offset_(send_window_offset),
30 receive_window_offset_(receive_window_offset),
31 max_receive_window_(max_receive_window),
32 last_blocked_send_window_offset_(0) {
33 DVLOG(1) << ENDPOINT << "Created flow controller for stream " << id_
34 << ", setting initial receive window offset to: "
35 << receive_window_offset_
36 << ", max receive window to: "
37 << max_receive_window_
38 << ", setting send window offset to: " << send_window_offset_;
41 void QuicFlowController::AddBytesConsumed(QuicByteCount bytes_consumed) {
42 bytes_consumed_ += bytes_consumed;
43 DVLOG(1) << ENDPOINT << "Stream " << id_ << " consumed: " << bytes_consumed_;
45 MaybeSendWindowUpdate();
48 bool QuicFlowController::UpdateHighestReceivedOffset(
49 QuicStreamOffset new_offset) {
50 // Only update if offset has increased.
51 if (new_offset <= highest_received_byte_offset_) {
52 return false;
55 DVLOG(1) << ENDPOINT << "Stream " << id_
56 << " highest byte offset increased from: "
57 << highest_received_byte_offset_ << " to " << new_offset;
58 highest_received_byte_offset_ = new_offset;
59 return true;
62 void QuicFlowController::AddBytesSent(QuicByteCount bytes_sent) {
63 if (bytes_sent_ + bytes_sent > send_window_offset_) {
64 LOG(DFATAL) << ENDPOINT << "Stream " << id_ << " Trying to send an extra "
65 << bytes_sent << " bytes, when bytes_sent = " << bytes_sent_
66 << ", and send_window_offset_ = " << send_window_offset_;
67 bytes_sent_ = send_window_offset_;
69 // This is an error on our side, close the connection as soon as possible.
70 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA);
71 return;
74 bytes_sent_ += bytes_sent;
75 DVLOG(1) << ENDPOINT << "Stream " << id_ << " sent: " << bytes_sent_;
78 bool QuicFlowController::FlowControlViolation() {
79 if (highest_received_byte_offset_ > receive_window_offset_) {
80 LOG(ERROR) << ENDPOINT << "Flow control violation on stream "
81 << id_ << ", receive window offset: "
82 << receive_window_offset_
83 << ", highest received byte offset: "
84 << highest_received_byte_offset_;
85 return true;
87 return false;
90 void QuicFlowController::MaybeSendWindowUpdate() {
91 // Send WindowUpdate to increase receive window if
92 // (receive window offset - consumed bytes) < (max window / 2).
93 // This is behaviour copied from SPDY.
94 DCHECK_LT(bytes_consumed_, receive_window_offset_);
95 QuicStreamOffset consumed_window = receive_window_offset_ - bytes_consumed_;
96 QuicByteCount threshold = (max_receive_window_ / 2);
98 if (consumed_window < threshold) {
99 // Update our receive window.
100 receive_window_offset_ += (max_receive_window_ - consumed_window);
102 DVLOG(1) << ENDPOINT << "Sending WindowUpdate frame for stream " << id_
103 << ", consumed bytes: " << bytes_consumed_
104 << ", consumed window: " << consumed_window
105 << ", and threshold: " << threshold
106 << ", and max recvw: " << max_receive_window_
107 << ". New receive window offset is: " << receive_window_offset_;
109 // Inform the peer of our new receive window.
110 connection_->SendWindowUpdate(id_, receive_window_offset_);
114 void QuicFlowController::MaybeSendBlocked() {
115 if (SendWindowSize() == 0 &&
116 last_blocked_send_window_offset_ < send_window_offset_) {
117 DVLOG(1) << ENDPOINT << "Stream " << id_ << " is flow control blocked. "
118 << "Send window: " << SendWindowSize()
119 << ", bytes sent: " << bytes_sent_
120 << ", send limit: " << send_window_offset_;
121 // The entire send_window has been consumed, we are now flow control
122 // blocked.
123 connection_->SendBlocked(id_);
125 // Keep track of when we last sent a BLOCKED frame so that we only send one
126 // at a given send offset.
127 last_blocked_send_window_offset_ = send_window_offset_;
131 bool QuicFlowController::UpdateSendWindowOffset(
132 QuicStreamOffset new_send_window_offset) {
133 // Only update if send window has increased.
134 if (new_send_window_offset <= send_window_offset_) {
135 return false;
138 DVLOG(1) << ENDPOINT << "UpdateSendWindowOffset for stream " << id_
139 << " with new offset " << new_send_window_offset
140 << " current offset: " << send_window_offset_
141 << " bytes_sent: " << bytes_sent_;
143 const bool blocked = IsBlocked();
144 send_window_offset_ = new_send_window_offset;
145 return blocked;
148 bool QuicFlowController::IsBlocked() const {
149 return SendWindowSize() == 0;
152 uint64 QuicFlowController::SendWindowSize() const {
153 if (bytes_sent_ > send_window_offset_) {
154 return 0;
156 return send_window_offset_ - bytes_sent_;
159 } // namespace net