Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / quic / quic_flow_controller.cc
blob59ae8c012b0e37ba42c1413656737c477ebb1bfb
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 (is_server_ ? "Server: " : " Client: ")
16 QuicFlowController::QuicFlowController(QuicConnection* connection,
17 QuicStreamId id,
18 bool is_server,
19 QuicStreamOffset send_window_offset,
20 QuicStreamOffset receive_window_offset,
21 QuicByteCount max_receive_window)
22 : connection_(connection),
23 id_(id),
24 is_enabled_(true),
25 is_server_(is_server),
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 if (!IsEnabled()) {
43 return;
46 bytes_consumed_ += bytes_consumed;
47 DVLOG(1) << ENDPOINT << "Stream " << id_ << " consumed: " << bytes_consumed_;
49 MaybeSendWindowUpdate();
52 bool QuicFlowController::UpdateHighestReceivedOffset(
53 QuicStreamOffset new_offset) {
54 if (!IsEnabled()) {
55 return false;
58 // Only update if offset has increased.
59 if (new_offset <= highest_received_byte_offset_) {
60 return false;
63 DVLOG(1) << ENDPOINT << "Stream " << id_
64 << " highest byte offset increased from: "
65 << highest_received_byte_offset_ << " to " << new_offset;
66 highest_received_byte_offset_ = new_offset;
67 return true;
70 void QuicFlowController::AddBytesSent(QuicByteCount bytes_sent) {
71 if (!IsEnabled()) {
72 return;
75 if (bytes_sent_ + bytes_sent > send_window_offset_) {
76 LOG(DFATAL) << ENDPOINT << "Stream " << id_ << " Trying to send an extra "
77 << bytes_sent << " bytes, when bytes_sent = " << bytes_sent_
78 << ", and send_window_offset_ = " << send_window_offset_;
79 bytes_sent_ = send_window_offset_;
81 // This is an error on our side, close the connection as soon as possible.
82 connection_->SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA);
83 return;
86 bytes_sent_ += bytes_sent;
87 DVLOG(1) << ENDPOINT << "Stream " << id_ << " sent: " << bytes_sent_;
90 bool QuicFlowController::FlowControlViolation() {
91 if (!IsEnabled()) {
92 return false;
95 if (highest_received_byte_offset_ > receive_window_offset_) {
96 LOG(ERROR) << ENDPOINT << "Flow control violation on stream "
97 << id_ << ", receive window offset: "
98 << receive_window_offset_
99 << ", highest received byte offset: "
100 << highest_received_byte_offset_;
101 return true;
103 return false;
106 void QuicFlowController::MaybeSendWindowUpdate() {
107 if (!IsEnabled()) {
108 return;
111 // Send WindowUpdate to increase receive window if
112 // (receive window offset - consumed bytes) < (max window / 2).
113 // This is behaviour copied from SPDY.
114 DCHECK_LT(bytes_consumed_, receive_window_offset_);
115 QuicStreamOffset consumed_window = receive_window_offset_ - bytes_consumed_;
116 QuicByteCount threshold = (max_receive_window_ / 2);
118 if (consumed_window < threshold) {
119 // Update our receive window.
120 receive_window_offset_ += (max_receive_window_ - consumed_window);
122 DVLOG(1) << ENDPOINT << "Sending WindowUpdate frame for stream " << id_
123 << ", consumed bytes: " << bytes_consumed_
124 << ", consumed window: " << consumed_window
125 << ", and threshold: " << threshold
126 << ", and max recvw: " << max_receive_window_
127 << ". New receive window offset is: " << receive_window_offset_;
129 // Inform the peer of our new receive window.
130 connection_->SendWindowUpdate(id_, receive_window_offset_);
134 void QuicFlowController::MaybeSendBlocked() {
135 if (!IsEnabled()) {
136 return;
139 if (SendWindowSize() == 0 &&
140 last_blocked_send_window_offset_ < send_window_offset_) {
141 DVLOG(1) << ENDPOINT << "Stream " << id_ << " is flow control blocked. "
142 << "Send window: " << SendWindowSize()
143 << ", bytes sent: " << bytes_sent_
144 << ", send limit: " << send_window_offset_;
145 // The entire send_window has been consumed, we are now flow control
146 // blocked.
147 connection_->SendBlocked(id_);
149 // Keep track of when we last sent a BLOCKED frame so that we only send one
150 // at a given send offset.
151 last_blocked_send_window_offset_ = send_window_offset_;
155 bool QuicFlowController::UpdateSendWindowOffset(
156 QuicStreamOffset new_send_window_offset) {
157 if (!IsEnabled()) {
158 return false;
161 // Only update if send window has increased.
162 if (new_send_window_offset <= send_window_offset_) {
163 return false;
166 DVLOG(1) << ENDPOINT << "UpdateSendWindowOffset for stream " << id_
167 << " with new offset " << new_send_window_offset
168 << " current offset: " << send_window_offset_
169 << " bytes_sent: " << bytes_sent_;
171 const bool blocked = IsBlocked();
172 send_window_offset_ = new_send_window_offset;
173 return blocked;
176 void QuicFlowController::Disable() {
177 is_enabled_ = false;
180 bool QuicFlowController::IsEnabled() const {
181 return is_enabled_;
184 bool QuicFlowController::IsBlocked() const {
185 return IsEnabled() && SendWindowSize() == 0;
188 uint64 QuicFlowController::SendWindowSize() const {
189 if (bytes_sent_ > send_window_offset_) {
190 return 0;
192 return send_window_offset_ - bytes_sent_;
195 } // namespace net