2012-06-05 18:26:04 +02:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-06-05 18:26:04 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_worker.h"
|
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2012-06-05 18:26:04 +02:00
|
|
|
#include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
2013-09-24 16:17:53 +02:00
|
|
|
|
2013-09-23 17:14:55 +02:00
|
|
|
#include <memory>
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
#include "shrpx_tls.h"
|
2012-06-05 18:26:04 +02:00
|
|
|
#include "shrpx_log.h"
|
2014-12-27 18:59:06 +01:00
|
|
|
#include "shrpx_client_handler.h"
|
2013-11-04 09:53:57 +01:00
|
|
|
#include "shrpx_http2_session.h"
|
2015-02-25 16:02:29 +01:00
|
|
|
#include "shrpx_log_config.h"
|
2015-07-25 15:22:17 +02:00
|
|
|
#include "shrpx_memcached_dispatcher.h"
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
#include "shrpx_mruby.h"
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2013-09-23 17:14:55 +02:00
|
|
|
#include "util.h"
|
2015-02-05 15:21:53 +01:00
|
|
|
#include "template.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
|
|
|
namespace {
|
2014-12-27 18:59:06 +01:00
|
|
|
void eventcb(struct ev_loop *loop, ev_async *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
worker->process_events();
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
namespace {
|
|
|
|
void mcpool_clear_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
if (worker->get_worker_stat()->num_connections != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker->get_mcpool()->clear();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
namespace {
|
|
|
|
void proc_wev_cb(struct ev_loop *loop, ev_timer *w, int revents) {
|
|
|
|
auto worker = static_cast<Worker *>(w->data);
|
|
|
|
worker->process_events();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
// DownstreamKey is used to index SharedDownstreamAddr in order to
|
|
|
|
// find the same configuration.
|
2018-01-08 10:08:01 +01:00
|
|
|
using DownstreamKey = std::tuple<
|
|
|
|
std::vector<std::tuple<StringRef, StringRef, size_t, size_t, shrpx_proto,
|
|
|
|
uint16_t, bool, bool, bool, bool>>,
|
|
|
|
bool, int, StringRef, StringRef, int>;
|
2016-03-22 15:51:00 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
namespace {
|
|
|
|
DownstreamKey create_downstream_key(
|
|
|
|
const std::shared_ptr<SharedDownstreamAddr> &shared_addr) {
|
|
|
|
DownstreamKey dkey;
|
|
|
|
|
|
|
|
auto &addrs = std::get<0>(dkey);
|
|
|
|
addrs.resize(shared_addr->addrs.size());
|
|
|
|
auto p = std::begin(addrs);
|
|
|
|
for (auto &a : shared_addr->addrs) {
|
|
|
|
std::get<0>(*p) = a.host;
|
|
|
|
std::get<1>(*p) = a.sni;
|
|
|
|
std::get<2>(*p) = a.fall;
|
|
|
|
std::get<3>(*p) = a.rise;
|
|
|
|
std::get<4>(*p) = a.proto;
|
|
|
|
std::get<5>(*p) = a.port;
|
|
|
|
std::get<6>(*p) = a.host_unix;
|
|
|
|
std::get<7>(*p) = a.tls;
|
|
|
|
std::get<8>(*p) = a.dns;
|
2018-01-08 10:08:01 +01:00
|
|
|
std::get<9>(*p) = a.upgrade_scheme;
|
2017-11-30 16:28:36 +01:00
|
|
|
++p;
|
2017-10-25 17:45:22 +02:00
|
|
|
}
|
2017-11-30 16:28:36 +01:00
|
|
|
std::sort(std::begin(addrs), std::end(addrs));
|
2017-10-25 17:45:22 +02:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
std::get<1>(dkey) = shared_addr->redirect_if_not_tls;
|
2016-03-23 14:13:53 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
auto &affinity = shared_addr->affinity;
|
|
|
|
std::get<2>(dkey) = affinity.type;
|
|
|
|
std::get<3>(dkey) = affinity.cookie.name;
|
|
|
|
std::get<4>(dkey) = affinity.cookie.path;
|
|
|
|
std::get<5>(dkey) = affinity.cookie.secure;
|
2016-03-23 14:13:53 +01:00
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
return dkey;
|
2016-03-22 15:51:00 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker::Worker(struct ev_loop *loop, SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
|
2016-02-11 14:56:45 +01:00
|
|
|
SSL_CTX *tls_session_cache_memcached_ssl_ctx,
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::CertLookupTree *cert_tree,
|
2016-06-03 18:02:57 +02:00
|
|
|
const std::shared_ptr<TicketKeys> &ticket_keys,
|
|
|
|
ConnectionHandler *conn_handler,
|
2017-03-26 14:14:34 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf)
|
|
|
|
: randgen_(util::make_mt19937()),
|
2016-02-27 15:24:14 +01:00
|
|
|
worker_stat_{},
|
2016-12-04 15:43:41 +01:00
|
|
|
dns_tracker_(loop),
|
2016-01-27 13:14:07 +01:00
|
|
|
loop_(loop),
|
|
|
|
sv_ssl_ctx_(sv_ssl_ctx),
|
|
|
|
cl_ssl_ctx_(cl_ssl_ctx),
|
|
|
|
cert_tree_(cert_tree),
|
2016-06-03 18:02:57 +02:00
|
|
|
conn_handler_(conn_handler),
|
2015-07-09 19:52:11 +02:00
|
|
|
ticket_keys_(ticket_keys),
|
2016-05-25 16:07:04 +02:00
|
|
|
connect_blocker_(
|
|
|
|
make_unique<ConnectBlocker>(randgen_, loop_, []() {}, []() {})),
|
2015-02-25 14:53:23 +01:00
|
|
|
graceful_shutdown_(false) {
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_async_init(&w_, eventcb);
|
|
|
|
w_.data = this;
|
|
|
|
ev_async_start(loop_, &w_);
|
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
ev_timer_init(&mcpool_clear_timer_, mcpool_clear_cb, 0., 0.);
|
|
|
|
mcpool_clear_timer_.data = this;
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_init(&proc_wev_timer_, proc_wev_cb, 0., 0.);
|
|
|
|
proc_wev_timer_.data = this;
|
|
|
|
|
2016-01-18 06:21:09 +01:00
|
|
|
auto &session_cacheconf = get_config()->tls.session_cache;
|
|
|
|
|
2016-02-14 12:59:10 +01:00
|
|
|
if (!session_cacheconf.memcached.host.empty()) {
|
2015-07-25 15:22:17 +02:00
|
|
|
session_cache_memcached_dispatcher_ = make_unique<MemcachedDispatcher>(
|
2016-02-11 14:56:45 +01:00
|
|
|
&session_cacheconf.memcached.addr, loop,
|
|
|
|
tls_session_cache_memcached_ssl_ctx,
|
2016-07-08 16:41:53 +02:00
|
|
|
StringRef{session_cacheconf.memcached.host}, &mcpool_, randgen_);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
replace_downstream_config(std::move(downstreamconf));
|
2016-06-03 12:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::replace_downstream_config(
|
2016-06-03 18:02:57 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf) {
|
2016-06-04 05:16:31 +02:00
|
|
|
for (auto &g : downstream_addr_groups_) {
|
|
|
|
g->retired = true;
|
2016-06-09 15:35:59 +02:00
|
|
|
|
|
|
|
auto &shared_addr = g->shared_addr;
|
|
|
|
|
2017-10-25 17:45:22 +02:00
|
|
|
if (shared_addr->affinity.type == AFFINITY_NONE) {
|
2016-06-09 15:35:59 +02:00
|
|
|
shared_addr->dconn_pool.remove_all();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
addr.dconn_pool->remove_all();
|
|
|
|
}
|
2016-06-04 05:16:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
downstreamconf_ = downstreamconf;
|
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
// Making a copy is much faster with multiple thread on
|
|
|
|
// backendconfig API call.
|
|
|
|
auto groups = downstreamconf->addr_groups;
|
2016-02-27 15:24:14 +01:00
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
downstream_addr_groups_ =
|
|
|
|
std::vector<std::shared_ptr<DownstreamAddrGroup>>(groups.size());
|
|
|
|
|
2017-11-30 16:28:36 +01:00
|
|
|
std::map<DownstreamKey, size_t> addr_groups_indexer;
|
|
|
|
|
2016-06-16 16:04:06 +02:00
|
|
|
for (size_t i = 0; i < groups.size(); ++i) {
|
|
|
|
auto &src = groups[i];
|
2016-02-27 15:24:14 +01:00
|
|
|
auto &dst = downstream_addr_groups_[i];
|
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
dst = std::make_shared<DownstreamAddrGroup>();
|
2016-10-02 10:44:45 +02:00
|
|
|
dst->pattern =
|
|
|
|
ImmutableString{std::begin(src.pattern), std::end(src.pattern)};
|
2016-03-22 15:51:00 +01:00
|
|
|
|
|
|
|
auto shared_addr = std::make_shared<SharedDownstreamAddr>();
|
2016-03-22 17:32:17 +01:00
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
shared_addr->addrs.resize(src.addrs.size());
|
2017-10-25 17:45:22 +02:00
|
|
|
shared_addr->affinity.type = src.affinity.type;
|
|
|
|
if (src.affinity.type == AFFINITY_COOKIE) {
|
|
|
|
shared_addr->affinity.cookie.name =
|
|
|
|
make_string_ref(shared_addr->balloc, src.affinity.cookie.name);
|
|
|
|
if (!src.affinity.cookie.path.empty()) {
|
|
|
|
shared_addr->affinity.cookie.path =
|
|
|
|
make_string_ref(shared_addr->balloc, src.affinity.cookie.path);
|
|
|
|
}
|
2017-11-21 14:24:38 +01:00
|
|
|
shared_addr->affinity.cookie.secure = src.affinity.cookie.secure;
|
2017-10-25 17:45:22 +02:00
|
|
|
}
|
2016-07-06 15:31:28 +02:00
|
|
|
shared_addr->affinity_hash = src.affinity_hash;
|
2017-02-18 10:23:06 +01:00
|
|
|
shared_addr->redirect_if_not_tls = src.redirect_if_not_tls;
|
2016-05-24 16:36:43 +02:00
|
|
|
|
|
|
|
size_t num_http1 = 0;
|
|
|
|
size_t num_http2 = 0;
|
2016-02-27 15:24:14 +01:00
|
|
|
|
|
|
|
for (size_t j = 0; j < src.addrs.size(); ++j) {
|
|
|
|
auto &src_addr = src.addrs[j];
|
2016-03-22 15:51:00 +01:00
|
|
|
auto &dst_addr = shared_addr->addrs[j];
|
2016-02-27 15:24:14 +01:00
|
|
|
|
|
|
|
dst_addr.addr = src_addr.addr;
|
2016-10-02 10:44:45 +02:00
|
|
|
dst_addr.host = make_string_ref(shared_addr->balloc, src_addr.host);
|
2016-10-02 09:18:35 +02:00
|
|
|
dst_addr.hostport =
|
2016-10-02 10:44:45 +02:00
|
|
|
make_string_ref(shared_addr->balloc, src_addr.hostport);
|
2016-02-27 15:24:14 +01:00
|
|
|
dst_addr.port = src_addr.port;
|
|
|
|
dst_addr.host_unix = src_addr.host_unix;
|
2016-05-24 16:36:43 +02:00
|
|
|
dst_addr.proto = src_addr.proto;
|
|
|
|
dst_addr.tls = src_addr.tls;
|
2016-10-02 10:44:45 +02:00
|
|
|
dst_addr.sni = make_string_ref(shared_addr->balloc, src_addr.sni);
|
2016-04-09 14:58:08 +02:00
|
|
|
dst_addr.fall = src_addr.fall;
|
|
|
|
dst_addr.rise = src_addr.rise;
|
2016-12-04 15:43:41 +01:00
|
|
|
dst_addr.dns = src_addr.dns;
|
2018-01-08 10:08:01 +01:00
|
|
|
dst_addr.upgrade_scheme = src_addr.upgrade_scheme;
|
2016-02-27 15:24:14 +01:00
|
|
|
|
2016-06-05 17:16:25 +02:00
|
|
|
auto shared_addr_ptr = shared_addr.get();
|
|
|
|
|
2016-05-25 16:07:04 +02:00
|
|
|
dst_addr.connect_blocker =
|
|
|
|
make_unique<ConnectBlocker>(randgen_, loop_,
|
2016-06-05 17:16:25 +02:00
|
|
|
[shared_addr_ptr, &dst_addr]() {
|
2016-05-25 16:07:04 +02:00
|
|
|
switch (dst_addr.proto) {
|
|
|
|
case PROTO_HTTP1:
|
2016-06-05 17:16:25 +02:00
|
|
|
--shared_addr_ptr->http1_pri.weight;
|
2016-05-25 16:07:04 +02:00
|
|
|
break;
|
|
|
|
case PROTO_HTTP2:
|
2016-06-05 17:16:25 +02:00
|
|
|
--shared_addr_ptr->http2_pri.weight;
|
2016-05-25 16:07:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
},
|
2016-06-05 17:16:25 +02:00
|
|
|
[shared_addr_ptr, &dst_addr]() {
|
2016-05-25 16:07:04 +02:00
|
|
|
switch (dst_addr.proto) {
|
|
|
|
case PROTO_HTTP1:
|
2016-06-05 17:16:25 +02:00
|
|
|
++shared_addr_ptr->http1_pri.weight;
|
2016-05-25 16:07:04 +02:00
|
|
|
break;
|
|
|
|
case PROTO_HTTP2:
|
2016-06-05 17:16:25 +02:00
|
|
|
++shared_addr_ptr->http2_pri.weight;
|
2016-05-25 16:07:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-24 16:36:43 +02:00
|
|
|
dst_addr.live_check =
|
|
|
|
make_unique<LiveCheck>(loop_, cl_ssl_ctx_, this, &dst_addr, randgen_);
|
|
|
|
|
|
|
|
if (dst_addr.proto == PROTO_HTTP2) {
|
|
|
|
++num_http2;
|
|
|
|
} else {
|
|
|
|
assert(dst_addr.proto == PROTO_HTTP1);
|
|
|
|
++num_http1;
|
|
|
|
}
|
2016-02-21 06:53:06 +01:00
|
|
|
}
|
2016-03-22 15:51:00 +01:00
|
|
|
|
|
|
|
// share the connection if patterns have the same set of backend
|
|
|
|
// addresses.
|
2017-11-30 16:28:36 +01:00
|
|
|
|
|
|
|
auto dkey = create_downstream_key(shared_addr);
|
|
|
|
auto it = addr_groups_indexer.find(dkey);
|
|
|
|
|
|
|
|
if (it == std::end(addr_groups_indexer)) {
|
2016-05-25 16:07:04 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "number of http/1.1 backend: " << num_http1
|
|
|
|
<< ", number of h2 backend: " << num_http2;
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:47:01 +02:00
|
|
|
shared_addr->http1_pri.weight = num_http1;
|
|
|
|
shared_addr->http2_pri.weight = num_http2;
|
2016-05-24 16:36:43 +02:00
|
|
|
|
2017-10-25 17:45:22 +02:00
|
|
|
if (shared_addr->affinity.type != AFFINITY_NONE) {
|
2016-06-09 15:35:59 +02:00
|
|
|
for (auto &addr : shared_addr->addrs) {
|
|
|
|
addr.dconn_pool = make_unique<DownstreamConnectionPool>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
dst->shared_addr = shared_addr;
|
2017-11-30 16:28:36 +01:00
|
|
|
|
|
|
|
addr_groups_indexer.emplace(std::move(dkey), i);
|
2016-03-22 15:51:00 +01:00
|
|
|
} else {
|
2017-11-30 16:28:36 +01:00
|
|
|
auto &g = *(std::begin(downstream_addr_groups_) + (*it).second);
|
2016-04-16 15:04:35 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-06-02 18:20:49 +02:00
|
|
|
LOG(INFO) << dst->pattern << " shares the same backend group with "
|
2017-11-30 16:28:36 +01:00
|
|
|
<< g->pattern;
|
2016-04-16 15:04:35 +02:00
|
|
|
}
|
2017-11-30 16:28:36 +01:00
|
|
|
dst->shared_addr = g->shared_addr;
|
2016-03-22 15:51:00 +01:00
|
|
|
}
|
2016-02-21 06:53:06 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2015-04-08 06:43:57 +02:00
|
|
|
Worker::~Worker() {
|
|
|
|
ev_async_stop(loop_, &w_);
|
|
|
|
ev_timer_stop(loop_, &mcpool_clear_timer_);
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_stop(loop_, &proc_wev_timer_);
|
2015-04-08 06:43:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::schedule_clear_mcpool() {
|
|
|
|
// libev manual says: "If the watcher is already active nothing will
|
|
|
|
// happen." Since we don't change any timeout here, we don't have
|
|
|
|
// to worry about querying ev_is_active.
|
|
|
|
ev_timer_start(loop_, &mcpool_clear_timer_);
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-01-07 17:57:59 +01:00
|
|
|
void Worker::wait() {
|
|
|
|
#ifndef NOTHREADS
|
|
|
|
fut_.get();
|
|
|
|
#endif // !NOTHREADS
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
void Worker::run_async() {
|
|
|
|
#ifndef NOTHREADS
|
|
|
|
fut_ = std::async(std::launch::async, [this] {
|
2017-02-16 14:46:22 +01:00
|
|
|
(void)reopen_log_files(get_config()->logging);
|
2015-02-11 11:18:41 +01:00
|
|
|
ev_run(loop_);
|
2017-02-08 14:14:23 +01:00
|
|
|
delete_log_config();
|
2015-02-11 11:18:41 +01:00
|
|
|
});
|
|
|
|
#endif // !NOTHREADS
|
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
void Worker::send(const WorkerEvent &event) {
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2014-07-05 11:22:40 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
q_.push_back(event);
|
2013-09-24 16:17:53 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
ev_async_send(loop_, &w_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::process_events() {
|
2016-06-25 04:50:33 +02:00
|
|
|
WorkerEvent wev;
|
2014-12-27 18:59:06 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2016-01-19 08:56:12 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
// Process event one at a time. This is important for
|
|
|
|
// NEW_CONNECTION event since accepting large number of new
|
|
|
|
// connections at once may delay time to 1st byte for existing
|
|
|
|
// connections.
|
2016-01-19 08:56:12 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (q_.empty()) {
|
|
|
|
ev_timer_stop(loop_, &proc_wev_timer_);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
wev = q_.front();
|
|
|
|
q_.pop_front();
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer_start(loop_, &proc_wev_timer_);
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
auto config = get_config();
|
|
|
|
|
|
|
|
auto worker_connections = config->conn.upstream.worker_connections;
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
switch (wev.type) {
|
|
|
|
case NEW_CONNECTION: {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "WorkerEvent: client_fd=" << wev.client_fd
|
|
|
|
<< ", addrlen=" << wev.client_addrlen;
|
|
|
|
}
|
2015-01-08 13:20:17 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (worker_stat_.num_connections >= worker_connections) {
|
2015-01-08 13:20:17 +01:00
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
2016-06-25 04:50:33 +02:00
|
|
|
WLOG(INFO, this) << "Too many connections >= " << worker_connections;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
close(wev.client_fd);
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
auto client_handler =
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::accept_connection(this, wev.client_fd, &wev.client_addr.sa,
|
2016-06-25 04:50:33 +02:00
|
|
|
wev.client_addrlen, wev.faddr);
|
|
|
|
if (!client_handler) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(ERROR, this) << "ClientHandler creation failed";
|
|
|
|
}
|
|
|
|
close(wev.client_fd);
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2016-06-25 04:50:33 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "CLIENT_HANDLER:" << client_handler << " created ";
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REOPEN_LOG:
|
|
|
|
WLOG(NOTICE, this) << "Reopening log files: worker process (thread " << this
|
|
|
|
<< ")";
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2017-02-16 14:46:22 +01:00
|
|
|
reopen_log_files(config->logging);
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
break;
|
|
|
|
case GRACEFUL_SHUTDOWN:
|
|
|
|
WLOG(NOTICE, this) << "Graceful shutdown commencing";
|
2016-06-03 18:02:57 +02:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
graceful_shutdown_ = true;
|
2016-06-03 18:02:57 +02:00
|
|
|
|
2016-06-25 04:50:33 +02:00
|
|
|
if (worker_stat_.num_connections == 0) {
|
|
|
|
ev_break(loop_);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case REPLACE_DOWNSTREAM:
|
|
|
|
WLOG(NOTICE, this) << "Replace downstream";
|
|
|
|
|
|
|
|
replace_downstream_config(wev.downstreamconf);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "unknown event type " << wev.type;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
}
|
|
|
|
|
2017-04-01 08:07:32 +02:00
|
|
|
tls::CertLookupTree *Worker::get_cert_lookup_tree() const { return cert_tree_; }
|
2015-02-11 11:18:41 +01:00
|
|
|
|
2015-07-23 16:13:29 +02:00
|
|
|
std::shared_ptr<TicketKeys> Worker::get_ticket_keys() {
|
2016-09-20 15:24:12 +02:00
|
|
|
#ifdef HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
return std::atomic_load_explicit(&ticket_keys_, std::memory_order_acquire);
|
|
|
|
#else // !HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
std::lock_guard<std::mutex> g(ticket_keys_m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
return ticket_keys_;
|
2016-09-20 15:24:12 +02:00
|
|
|
#endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::set_ticket_keys(std::shared_ptr<TicketKeys> ticket_keys) {
|
2016-09-20 15:24:12 +02:00
|
|
|
#ifdef HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
// This is single writer
|
|
|
|
std::atomic_store_explicit(&ticket_keys_, std::move(ticket_keys),
|
|
|
|
std::memory_order_release);
|
|
|
|
#else // !HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
std::lock_guard<std::mutex> g(ticket_keys_m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
ticket_keys_ = std::move(ticket_keys);
|
2016-09-20 15:24:12 +02:00
|
|
|
#endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerStat *Worker::get_worker_stat() { return &worker_stat_; }
|
|
|
|
|
|
|
|
struct ev_loop *Worker::get_loop() const {
|
|
|
|
return loop_;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX *Worker::get_sv_ssl_ctx() const { return sv_ssl_ctx_; }
|
|
|
|
|
2015-03-10 13:54:29 +01:00
|
|
|
SSL_CTX *Worker::get_cl_ssl_ctx() const { return cl_ssl_ctx_; }
|
|
|
|
|
2015-02-25 14:53:23 +01:00
|
|
|
void Worker::set_graceful_shutdown(bool f) { graceful_shutdown_ = f; }
|
|
|
|
|
|
|
|
bool Worker::get_graceful_shutdown() const { return graceful_shutdown_; }
|
|
|
|
|
2015-04-07 15:13:01 +02:00
|
|
|
MemchunkPool *Worker::get_mcpool() { return &mcpool_; }
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
MemcachedDispatcher *Worker::get_session_cache_memcached_dispatcher() {
|
|
|
|
return session_cache_memcached_dispatcher_.get();
|
|
|
|
}
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
std::mt19937 &Worker::get_randgen() { return randgen_; }
|
|
|
|
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
int Worker::create_mruby_context() {
|
2016-02-14 14:27:59 +01:00
|
|
|
mruby_ctx_ = mruby::create_mruby_context(StringRef{get_config()->mruby_file});
|
2015-09-01 17:19:32 +02:00
|
|
|
if (!mruby_ctx_) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mruby::MRubyContext *Worker::get_mruby_context() const {
|
|
|
|
return mruby_ctx_.get();
|
|
|
|
}
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2016-06-02 18:20:49 +02:00
|
|
|
std::vector<std::shared_ptr<DownstreamAddrGroup>> &
|
|
|
|
Worker::get_downstream_addr_groups() {
|
2016-02-21 06:53:06 +01:00
|
|
|
return downstream_addr_groups_;
|
|
|
|
}
|
|
|
|
|
2016-02-21 07:27:19 +01:00
|
|
|
ConnectBlocker *Worker::get_connect_blocker() const {
|
|
|
|
return connect_blocker_.get();
|
|
|
|
}
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
const DownstreamConfig *Worker::get_downstream_config() const {
|
|
|
|
return downstreamconf_.get();
|
2016-06-02 18:20:49 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
ConnectionHandler *Worker::get_connection_handler() const {
|
|
|
|
return conn_handler_;
|
|
|
|
}
|
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
DNSTracker *Worker::get_dns_tracker() { return &dns_tracker_; }
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
namespace {
|
|
|
|
size_t match_downstream_addr_group_host(
|
2016-06-11 11:21:37 +02:00
|
|
|
const RouterConfig &routerconf, const StringRef &host,
|
|
|
|
const StringRef &path,
|
2016-06-02 18:20:49 +02:00
|
|
|
const std::vector<std::shared_ptr<DownstreamAddrGroup>> &groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
size_t catch_all, BlockAllocator &balloc) {
|
2016-06-11 11:21:37 +02:00
|
|
|
|
|
|
|
const auto &router = routerconf.router;
|
|
|
|
const auto &rev_wildcard_router = routerconf.rev_wildcard_router;
|
|
|
|
const auto &wildcard_patterns = routerconf.wildcard_patterns;
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Perform mapping selection, using host=" << host
|
|
|
|
<< ", path=" << path;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto group = router.match(host, path);
|
|
|
|
if (group != -1) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found pattern with query " << host << path
|
2016-06-02 18:20:49 +02:00
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2016-06-10 16:13:40 +02:00
|
|
|
if (!wildcard_patterns.empty() && !host.empty()) {
|
2016-06-11 11:41:43 +02:00
|
|
|
auto rev_host_src = make_byte_ref(balloc, host.size() - 1);
|
|
|
|
auto ep =
|
|
|
|
std::copy(std::begin(host) + 1, std::end(host), rev_host_src.base);
|
|
|
|
std::reverse(rev_host_src.base, ep);
|
|
|
|
auto rev_host = StringRef{rev_host_src.base, ep};
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
ssize_t best_group = -1;
|
|
|
|
const RNode *last_node = nullptr;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
size_t nread = 0;
|
|
|
|
auto wcidx =
|
|
|
|
rev_wildcard_router.match_prefix(&nread, &last_node, rev_host);
|
|
|
|
if (wcidx == -1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rev_host = StringRef{std::begin(rev_host) + nread, std::end(rev_host)};
|
2016-06-10 16:13:40 +02:00
|
|
|
|
|
|
|
auto &wc = wildcard_patterns[wcidx];
|
|
|
|
auto group = wc.router.match(StringRef{}, path);
|
|
|
|
if (group != -1) {
|
|
|
|
// We sorted wildcard_patterns in a way that first match is the
|
|
|
|
// longest host pattern.
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found wildcard pattern with query " << host << path
|
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
|
|
|
}
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
best_group = group;
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-11 06:31:04 +02:00
|
|
|
|
|
|
|
if (best_group != -1) {
|
|
|
|
return best_group;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
|
|
|
|
2016-02-28 13:35:26 +01:00
|
|
|
group = router.match(StringRef::from_lit(""), path);
|
2016-02-27 15:24:14 +01:00
|
|
|
if (group != -1) {
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "Found pattern with query " << path
|
2016-06-02 18:20:49 +02:00
|
|
|
<< ", matched pattern=" << groups[group]->pattern;
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
LOG(INFO) << "None match. Use catch-all pattern";
|
|
|
|
}
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
size_t match_downstream_addr_group(
|
2016-06-11 11:21:37 +02:00
|
|
|
const RouterConfig &routerconf, const StringRef &hostport,
|
|
|
|
const StringRef &raw_path,
|
2016-06-02 18:20:49 +02:00
|
|
|
const std::vector<std::shared_ptr<DownstreamAddrGroup>> &groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
size_t catch_all, BlockAllocator &balloc) {
|
2016-02-27 15:24:14 +01:00
|
|
|
if (std::find(std::begin(hostport), std::end(hostport), '/') !=
|
|
|
|
std::end(hostport)) {
|
|
|
|
// We use '/' specially, and if '/' is included in host, it breaks
|
|
|
|
// our code. Select catch-all case.
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fragment = std::find(std::begin(raw_path), std::end(raw_path), '#');
|
|
|
|
auto query = std::find(std::begin(raw_path), fragment, '?');
|
|
|
|
auto path = StringRef{std::begin(raw_path), query};
|
|
|
|
|
2017-04-18 14:03:50 +02:00
|
|
|
if (path.empty() || path[0] != '/') {
|
|
|
|
path = StringRef::from_lit("/");
|
|
|
|
}
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
if (hostport.empty()) {
|
2016-06-11 11:21:37 +02:00
|
|
|
return match_downstream_addr_group_host(routerconf, hostport, path, groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
catch_all, balloc);
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:59:25 +01:00
|
|
|
StringRef host;
|
2016-02-27 15:24:14 +01:00
|
|
|
if (hostport[0] == '[') {
|
|
|
|
// assume this is IPv6 numeric address
|
|
|
|
auto p = std::find(std::begin(hostport), std::end(hostport), ']');
|
|
|
|
if (p == std::end(hostport)) {
|
|
|
|
return catch_all;
|
|
|
|
}
|
|
|
|
if (p + 1 < std::end(hostport) && *(p + 1) != ':') {
|
|
|
|
return catch_all;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
host = StringRef{std::begin(hostport), p + 1};
|
2016-02-27 15:24:14 +01:00
|
|
|
} else {
|
|
|
|
auto p = std::find(std::begin(hostport), std::end(hostport), ':');
|
|
|
|
if (p == std::begin(hostport)) {
|
|
|
|
return catch_all;
|
|
|
|
}
|
2016-03-12 16:59:25 +01:00
|
|
|
host = StringRef{std::begin(hostport), p};
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:59:25 +01:00
|
|
|
if (std::find_if(std::begin(host), std::end(host), [](char c) {
|
|
|
|
return 'A' <= c || c <= 'Z';
|
|
|
|
}) != std::end(host)) {
|
2016-06-11 11:41:43 +02:00
|
|
|
auto low_host = make_byte_ref(balloc, host.size() + 1);
|
|
|
|
auto ep = std::copy(std::begin(host), std::end(host), low_host.base);
|
|
|
|
*ep = '\0';
|
|
|
|
util::inp_strlower(low_host.base, ep);
|
|
|
|
host = StringRef{low_host.base, ep};
|
2016-03-12 16:59:25 +01:00
|
|
|
}
|
2016-06-11 11:21:37 +02:00
|
|
|
return match_downstream_addr_group_host(routerconf, host, path, groups,
|
2016-06-11 11:41:43 +02:00
|
|
|
catch_all, balloc);
|
2016-02-27 15:24:14 +01:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
void downstream_failure(DownstreamAddr *addr, const Address *raddr) {
|
2016-04-07 18:04:16 +02:00
|
|
|
const auto &connect_blocker = addr->connect_blocker;
|
|
|
|
|
2016-05-21 03:28:16 +02:00
|
|
|
if (connect_blocker->in_offline()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:04:16 +02:00
|
|
|
connect_blocker->on_failure();
|
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
if (addr->fall == 0) {
|
2016-04-08 15:35:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
auto fail_count = connect_blocker->get_fail_count();
|
|
|
|
|
|
|
|
if (fail_count >= addr->fall) {
|
2016-12-04 15:43:41 +01:00
|
|
|
if (raddr) {
|
|
|
|
LOG(WARN) << "Could not connect to " << util::to_numeric_addr(raddr)
|
|
|
|
<< " " << fail_count
|
|
|
|
<< " times in a row; considered as offline";
|
|
|
|
} else {
|
|
|
|
LOG(WARN) << "Could not connect to " << addr->host << ":" << addr->port
|
|
|
|
<< " " << fail_count
|
|
|
|
<< " times in a row; considered as offline";
|
|
|
|
}
|
2016-04-07 18:04:16 +02:00
|
|
|
|
|
|
|
connect_blocker->offline();
|
2016-04-08 15:35:45 +02:00
|
|
|
|
2016-04-09 14:58:08 +02:00
|
|
|
if (addr->rise) {
|
2016-04-08 15:35:45 +02:00
|
|
|
addr->live_check->schedule();
|
|
|
|
}
|
2016-04-07 18:04:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
} // namespace shrpx
|