Imported Upstream version 1.0.0
[platform/upstream/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                      SSL *ssl)
40     : w_(w), loop_(loop), ssl_(ssl), rate_(rate), burst_(burst), avail_(burst),
41       startw_req_(false) {
42   ev_timer_init(&t_, regencb, 0., 1.);
43   t_.data = this;
44   if (rate_ > 0) {
45     ev_timer_again(loop_, &t_);
46   }
47 }
48
49 RateLimit::~RateLimit() { ev_timer_stop(loop_, &t_); }
50
51 size_t RateLimit::avail() const {
52   if (rate_ == 0) {
53     return std::numeric_limits<ssize_t>::max();
54   }
55   return avail_;
56 }
57
58 void RateLimit::drain(size_t n) {
59   if (rate_ == 0) {
60     return;
61   }
62   n = std::min(avail_, n);
63   avail_ -= n;
64   if (avail_ == 0) {
65     ev_io_stop(loop_, w_);
66   }
67 }
68
69 void RateLimit::regen() {
70   if (rate_ == 0) {
71     return;
72   }
73   if (avail_ + rate_ > burst_) {
74     avail_ = burst_;
75   } else {
76     avail_ += rate_;
77   }
78
79   if (avail_ > 0 && startw_req_) {
80     ev_io_start(loop_, w_);
81     handle_tls_pending_read();
82   }
83 }
84
85 void RateLimit::startw() {
86   startw_req_ = true;
87   if (rate_ == 0 || avail_ > 0) {
88     ev_io_start(loop_, w_);
89     handle_tls_pending_read();
90     return;
91   }
92 }
93
94 void RateLimit::stopw() {
95   startw_req_ = false;
96   ev_io_stop(loop_, w_);
97 }
98
99 void RateLimit::handle_tls_pending_read() {
100   if (!ssl_ || SSL_pending(ssl_) == 0) {
101     return;
102   }
103
104   // Note that ev_feed_event works without starting watcher, but we
105   // only call this function if watcher is active.
106   ev_feed_event(loop_, w_, EV_READ);
107 }
108
109 } // namespace shrpx