proxygen
proxygen::Window Class Reference

#include <Window.h>

Public Member Functions

 Window (uint32_t capacity)
 
int32_t getSize () const
 
uint32_t getNonNegativeSize () const
 
uint32_t getCapacity () const
 
uint32_t getOutstanding () const
 
bool reserve (uint32_t amount, bool strict=true)
 
bool free (uint32_t amount)
 
bool setCapacity (uint32_t capacity)
 

Private Attributes

int32_t outstanding_ {0}
 
int32_t capacity_ {0}
 

Detailed Description

A class that implements SPDY & HTTP/2 window management. This class should be used for both connection and stream level flow control.

Definition at line 20 of file Window.h.

Constructor & Destructor Documentation

proxygen::Window::Window ( uint32_t  capacity)
explicit

Constructs a new Window.

Parameters
capacityThe initial capacity of this Window. The initial size will also be equal to this. This parameter must not be > 2^31 -1

Definition at line 19 of file Window.cpp.

References setCapacity().

19  {
20  CHECK(setCapacity(capacity));
21 }
bool setCapacity(uint32_t capacity)
Definition: Window.cpp:84

Member Function Documentation

bool proxygen::Window::free ( uint32_t  amount)

Increment the window size by amount. Decrease bytes outstanding by amount. The window size could become greater than the capacity here.

Definition at line 63 of file Window.cpp.

References capacity_, int32_t, max, min, and outstanding_.

Referenced by proxygen::FlowControlFilter::ingressBytesProcessed(), proxygen::FlowControlFilter::onBody(), proxygen::HTTPTransaction::onIngressBody(), proxygen::HTTPTransaction::onIngressWindowUpdate(), proxygen::FlowControlFilter::onWindowUpdate(), proxygen::HTTPTransaction::resumeIngress(), and TEST().

63  {
64  if (amount > std::numeric_limits<int32_t>::max()) {
65  VLOG(3) << "Cannot expand window by more than 2^31 - 1. "
66  << "Attempted increment of " << amount;
67  return false;
68  }
70  static_cast<int32_t>(amount);
71  if (outstanding_ < 0 && limit > outstanding_) {
72  VLOG(3) << "Underflow detected. Window change failed.";
73  return false;
74  }
75  const int32_t newOutstanding = outstanding_ - amount;
76  if (newOutstanding < capacity_ - std::numeric_limits<int32_t>::max()) {
77  VLOG(3) << "Window exceeded 2^31 - 1. Window change failed.";
78  return false;
79  }
80  outstanding_ = newOutstanding;
81  return true;
82 }
int32_t outstanding_
Definition: Window.h:80
LogLevel max
Definition: LogLevel.cpp:31
LogLevel min
Definition: LogLevel.cpp:30
int32_t capacity_
Definition: Window.h:81
uint32_t proxygen::Window::getCapacity ( ) const
uint32_t proxygen::Window::getNonNegativeSize ( ) const

Returns the number of bytes available to be consumed in this window. If that number went negative somehow, this function clamps the return value to zero.

Definition at line 27 of file Window.cpp.

References getSize(), and folly::size().

Referenced by proxygen::FlowControlFilter::generateBody(), proxygen::FlowControlFilter::getAvailableSend(), and proxygen::FlowControlFilter::onWindowUpdate().

27  {
28  auto size = getSize();
29  return size > 0 ? size : 0;
30 }
int32_t getSize() const
Definition: Window.cpp:23
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
uint32_t proxygen::Window::getOutstanding ( ) const

Returns the number of bytes reserved in this window. If multiple calls to free() caused this number to go negative, this function returns 0.

Definition at line 37 of file Window.cpp.

References outstanding_.

Referenced by proxygen::HTTPTransaction::onDelayedDestroy(), proxygen::HTTPTransaction::onIngressBody(), proxygen::HTTPTransaction::onIngressSetSendWindow(), proxygen::HTTPTransaction::onIngressWindowUpdate(), proxygen::FlowControlFilter::onWindowUpdate(), and TEST().

37  {
38  return outstanding_ < 0 ? 0 : outstanding_;
39 }
int32_t outstanding_
Definition: Window.h:80
bool proxygen::Window::reserve ( uint32_t  amount,
bool  strict = true 
)
Parameters
amountThe amount to reduce the window size by. Increases bytes outstanding by amount.
strictIf true, reserve() will return false if there is no space remaining in the window. If false, reserve() will return true until the integer overflows.

Definition at line 41 of file Window.cpp.

References capacity_, int32_t, max, and outstanding_.

Referenced by proxygen::FlowControlFilter::generateBody(), proxygen::FlowControlFilter::onBody(), proxygen::HTTPTransaction::onIngressBody(), proxygen::HTTPTransaction::sendBodyNow(), and TEST().

41  {
42  if (amount > std::numeric_limits<int32_t>::max()) {
43  VLOG(3) << "Cannot shrink window by more than 2^31 - 1. "
44  << "Attempted decrement of " << amount;
45  return false;
46  }
48  static_cast<int32_t>(amount);
49  if (outstanding_ > 0 && limit < outstanding_) {
50  VLOG(3) << "Overflow detected. Window change failed.";
51  return false;
52  }
53  const int32_t newOutstanding = outstanding_ + amount;
54  if (strict && newOutstanding > capacity_) {
55  VLOG(3) << "Outstanding bytes (" << newOutstanding << ") exceeded "
56  << "window capacity (" << capacity_ << ")";
57  return false;
58  }
59  outstanding_ = newOutstanding;
60  return true;
61 }
int32_t outstanding_
Definition: Window.h:80
LogLevel max
Definition: LogLevel.cpp:31
int32_t capacity_
Definition: Window.h:81
bool proxygen::Window::setCapacity ( uint32_t  capacity)

Sets a new initial window size. This will also apply the delta between the current window size and the new window size. Returns false iff applying the new initial window fails.

Definition at line 84 of file Window.cpp.

References capacity_, diff(), getSize(), int32_t, max, and folly::size().

Referenced by proxygen::FlowControlFilter::FlowControlFilter(), proxygen::HTTPTransaction::onIngressSetSendWindow(), proxygen::HTTPTransaction::reset(), proxygen::HTTPTransaction::setReceiveWindow(), proxygen::FlowControlFilter::setReceiveWindowSize(), TEST(), and Window().

84  {
85  if (capacity > std::numeric_limits<int32_t>::max()) {
86  VLOG(3) << "Cannot set initial window > 2^31 -1.";
87  return false;
88  }
89 
90  const int32_t diff = static_cast<int32_t>(capacity) - capacity_;
91  if (diff > 0) {
92  const int32_t size = getSize();
93  if (size > 0 && diff > (std::numeric_limits<int32_t>::max() - size)) {
94  VLOG(3) << "Increasing the capacity overflowed the window";
95  return false;
96  }
97  }
98 
99  capacity_ = static_cast<int32_t>(capacity);
100  return true;
101 }
LogLevel max
Definition: LogLevel.cpp:31
int32_t getSize() const
Definition: Window.cpp:23
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
uint64_t diff(uint64_t a, uint64_t b)
Definition: FutexTest.cpp:135
int32_t capacity_
Definition: Window.h:81

Member Data Documentation

int32_t proxygen::Window::capacity_ {0}
private

Definition at line 81 of file Window.h.

Referenced by free(), getCapacity(), getSize(), reserve(), and setCapacity().

int32_t proxygen::Window::outstanding_ {0}
private

Definition at line 80 of file Window.h.

Referenced by free(), getOutstanding(), getSize(), and reserve().


The documentation for this class was generated from the following files: