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 #include "shrpx_connection.h"
30 #include "shrpx_log.h"
31 
32 namespace shrpx {
33 
34 namespace {
regencb(struct ev_loop * loop,ev_timer * w,int revents)35 void regencb(struct ev_loop *loop, ev_timer *w, int revents) {
36   auto r = static_cast<RateLimit *>(w->data);
37   r->regen();
38 }
39 } // namespace
40 
RateLimit(struct ev_loop * loop,ev_io * w,size_t rate,size_t burst,Connection * conn)41 RateLimit::RateLimit(struct ev_loop *loop, ev_io *w, size_t rate, size_t burst,
42                      Connection *conn)
43     : w_(w),
44       loop_(loop),
45       conn_(conn),
46       rate_(rate),
47       burst_(burst),
48       avail_(burst),
49       startw_req_(false) {
50   ev_timer_init(&t_, regencb, 0., 1.);
51   t_.data = this;
52   if (rate_ > 0) {
53     ev_timer_again(loop_, &t_);
54   }
55 }
56 
~RateLimit()57 RateLimit::~RateLimit() { ev_timer_stop(loop_, &t_); }
58 
avail() const59 size_t RateLimit::avail() const {
60   if (rate_ == 0) {
61     return std::numeric_limits<ssize_t>::max();
62   }
63   return avail_;
64 }
65 
drain(size_t n)66 void RateLimit::drain(size_t n) {
67   if (rate_ == 0) {
68     return;
69   }
70   n = std::min(avail_, n);
71   avail_ -= n;
72   if (avail_ == 0) {
73     ev_io_stop(loop_, w_);
74   }
75 }
76 
regen()77 void RateLimit::regen() {
78   if (rate_ == 0) {
79     return;
80   }
81   if (avail_ + rate_ > burst_) {
82     avail_ = burst_;
83   } else {
84     avail_ += rate_;
85   }
86 
87   if (w_->fd >= 0 && avail_ > 0 && startw_req_) {
88     ev_io_start(loop_, w_);
89     handle_tls_pending_read();
90   }
91 }
92 
startw()93 void RateLimit::startw() {
94   if (w_->fd < 0) {
95     return;
96   }
97   startw_req_ = true;
98   if (rate_ == 0 || avail_ > 0) {
99     ev_io_start(loop_, w_);
100     handle_tls_pending_read();
101     return;
102   }
103 }
104 
stopw()105 void RateLimit::stopw() {
106   startw_req_ = false;
107   ev_io_stop(loop_, w_);
108 }
109 
handle_tls_pending_read()110 void RateLimit::handle_tls_pending_read() {
111   if (!conn_ || !conn_->tls.ssl || !conn_->tls.initial_handshake_done ||
112       (SSL_pending(conn_->tls.ssl) == 0 && conn_->tls.rbuf.rleft() == 0 &&
113        conn_->tls.earlybuf.rleft() == 0)) {
114     return;
115   }
116 
117   // Note that ev_feed_event works without starting watcher, but we
118   // only call this function if watcher is active.
119   ev_feed_event(loop_, w_, EV_READ);
120 }
121 
122 } // namespace shrpx
123