1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2014 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_connect_blocker.h"
26 #include "shrpx_config.h"
27 #include "shrpx_log.h"
28 
29 namespace shrpx {
30 
31 namespace {
connect_blocker_cb(struct ev_loop * loop,ev_timer * w,int revents)32 void connect_blocker_cb(struct ev_loop *loop, ev_timer *w, int revents) {
33   auto connect_blocker = static_cast<ConnectBlocker *>(w->data);
34   if (LOG_ENABLED(INFO)) {
35     LOG(INFO) << "Unblock";
36   }
37 
38   connect_blocker->call_unblock_func();
39 }
40 } // namespace
41 
ConnectBlocker(std::mt19937 & gen,struct ev_loop * loop,std::function<void ()> block_func,std::function<void ()> unblock_func)42 ConnectBlocker::ConnectBlocker(std::mt19937 &gen, struct ev_loop *loop,
43                                std::function<void()> block_func,
44                                std::function<void()> unblock_func)
45     : gen_(gen),
46       block_func_(block_func),
47       unblock_func_(unblock_func),
48       loop_(loop),
49       fail_count_(0),
50       offline_(false) {
51   ev_timer_init(&timer_, connect_blocker_cb, 0., 0.);
52   timer_.data = this;
53 }
54 
~ConnectBlocker()55 ConnectBlocker::~ConnectBlocker() { ev_timer_stop(loop_, &timer_); }
56 
blocked() const57 bool ConnectBlocker::blocked() const { return ev_is_active(&timer_); }
58 
on_success()59 void ConnectBlocker::on_success() {
60   if (ev_is_active(&timer_)) {
61     return;
62   }
63 
64   fail_count_ = 0;
65 }
66 
67 // Use the similar backoff algorithm described in
68 // https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md
69 namespace {
70 constexpr size_t MAX_BACKOFF_EXP = 10;
71 constexpr auto MULTIPLIER = 1.6;
72 constexpr auto JITTER = 0.2;
73 } // namespace
74 
on_failure()75 void ConnectBlocker::on_failure() {
76   if (ev_is_active(&timer_)) {
77     return;
78   }
79 
80   call_block_func();
81 
82   ++fail_count_;
83 
84   auto base_backoff =
85       util::int_pow(MULTIPLIER, std::min(MAX_BACKOFF_EXP, fail_count_));
86   auto dist = std::uniform_real_distribution<>(-JITTER * base_backoff,
87                                                JITTER * base_backoff);
88 
89   auto &downstreamconf = *get_config()->conn.downstream;
90 
91   auto backoff =
92       std::min(downstreamconf.timeout.max_backoff, base_backoff + dist(gen_));
93 
94   LOG(WARN) << "Could not connect " << fail_count_
95             << " times in a row; sleep for " << backoff << " seconds";
96 
97   ev_timer_set(&timer_, backoff, 0.);
98   ev_timer_start(loop_, &timer_);
99 }
100 
get_fail_count() const101 size_t ConnectBlocker::get_fail_count() const { return fail_count_; }
102 
offline()103 void ConnectBlocker::offline() {
104   if (offline_) {
105     return;
106   }
107 
108   if (!ev_is_active(&timer_)) {
109     call_block_func();
110   }
111 
112   offline_ = true;
113 
114   ev_timer_stop(loop_, &timer_);
115   ev_timer_set(&timer_, std::numeric_limits<double>::max(), 0.);
116   ev_timer_start(loop_, &timer_);
117 }
118 
online()119 void ConnectBlocker::online() {
120   ev_timer_stop(loop_, &timer_);
121 
122   call_unblock_func();
123 
124   fail_count_ = 0;
125 
126   offline_ = false;
127 }
128 
in_offline() const129 bool ConnectBlocker::in_offline() const { return offline_; }
130 
call_block_func()131 void ConnectBlocker::call_block_func() {
132   if (block_func_) {
133     block_func_();
134   }
135 }
136 
call_unblock_func()137 void ConnectBlocker::call_unblock_func() {
138   if (unblock_func_) {
139     unblock_func_();
140   }
141 }
142 
143 } // namespace shrpx
144