tizen 2.4 release
[external/nghttp2.git] / src / shrpx_rate_limit.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2015 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "shrpx_rate_limit.h"
26
27 #include <limits>
28
29 namespace shrpx {
30
31 namespace {
32 void regencb(struct ev_loop *loop, ev_timer *w, int revents) {
33   auto r = static_cast<RateLimit *>(w->data);
34   r->regen();
35 }
36 } // namespace
37
38 RateLimit::RateLimit(struct ev_loop *loop, ev_io *w, size_t rate, size_t burst)
39     : w_(w), loop_(loop), rate_(rate), burst_(burst), avail_(burst),
40       startw_req_(false) {
41   ev_timer_init(&t_, regencb, 0., 1.);
42   t_.data = this;
43   if (rate_ > 0) {
44     ev_timer_again(loop_, &t_);
45   }
46 }
47
48 RateLimit::~RateLimit() {
49   ev_timer_stop(loop_, &t_);
50 }
51
52 size_t RateLimit::avail() const {
53   if (rate_ == 0) {
54     return std::numeric_limits<ssize_t>::max();
55   }
56   return avail_;
57 }
58
59 void RateLimit::drain(size_t n) {
60   if (rate_ == 0) {
61     return;
62   }
63   n = std::min(avail_, n);
64   avail_ -= n;
65   if (avail_ == 0) {
66     ev_io_stop(loop_, w_);
67   }
68 }
69
70 void RateLimit::regen() {
71   if (rate_ == 0) {
72     return;
73   }
74   if (avail_ + rate_ > burst_) {
75     avail_ = burst_;
76   } else {
77     avail_ += rate_;
78   }
79
80   if (avail_ > 0 && startw_req_) {
81     ev_io_start(loop_, w_);
82   }
83 }
84
85 void RateLimit::startw() {
86   startw_req_ = true;
87   if (rate_ == 0 || avail_ > 0) {
88     ev_io_start(loop_, w_);
89     return;
90   }
91 }
92
93 void RateLimit::stopw() {
94   startw_req_ = false;
95   ev_io_stop(loop_, w_);
96 }
97
98 } // namespace shrpx