blob: 0d4f92126b7c3bd7ee7c09bfcff41455d8bb57f0 [file] [log] [blame]
Inna Palantff3f07a2019-07-11 16:15:26 -07001/*
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
32namespace shrpx {
33
34namespace {
35void 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
41RateLimit::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
57RateLimit::~RateLimit() { ev_timer_stop(loop_, &t_); }
58
59size_t RateLimit::avail() const {
60 if (rate_ == 0) {
61 return std::numeric_limits<ssize_t>::max();
62 }
63 return avail_;
64}
65
66void 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
77void 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
93void 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
105void RateLimit::stopw() {
106 startw_req_ = false;
107 ev_io_stop(loop_, w_);
108}
109
110void RateLimit::handle_tls_pending_read() {
Chris Wailescd1aefd2023-07-13 13:36:21 -0700111 if (!conn_ || !conn_->tls.ssl ||
Inna Palantff3f07a2019-07-11 16:15:26 -0700112 (SSL_pending(conn_->tls.ssl) == 0 && conn_->tls.rbuf.rleft() == 0 &&
Chris Wailescd1aefd2023-07-13 13:36:21 -0700113 (!conn_->tls.initial_handshake_done ||
114 conn_->tls.earlybuf.rleft() == 0))) {
Inna Palantff3f07a2019-07-11 16:15:26 -0700115 return;
116 }
117
118 // Note that ev_feed_event works without starting watcher, but we
119 // only call this function if watcher is active.
120 ev_feed_event(loop_, w_, EV_READ);
121}
122
123} // namespace shrpx