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
|
|
|
|
|
|
|
#include "shrpx_ssl.h"
|
|
|
|
#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"
|
2014-08-19 16:36:04 +02:00
|
|
|
#include "shrpx_connect_blocker.h"
|
2015-07-25 15:22:17 +02:00
|
|
|
#include "shrpx_memcached_dispatcher.h"
|
2015-09-01 17:19:32 +02:00
|
|
|
#include "shrpx_mruby.h"
|
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
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
Worker::Worker(struct ev_loop *loop, SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
|
2015-01-12 16:18:27 +01:00
|
|
|
ssl::CertLookupTree *cert_tree,
|
2015-01-08 13:15:45 +01:00
|
|
|
const std::shared_ptr<TicketKeys> &ticket_keys)
|
2015-07-12 15:16:20 +02:00
|
|
|
: dconn_pool_(get_config()->downstream_addr_groups.size()),
|
|
|
|
worker_stat_(get_config()->downstream_addr_groups.size()),
|
|
|
|
dgrps_(get_config()->downstream_addr_groups.size()), loop_(loop),
|
2015-07-09 19:52:11 +02:00
|
|
|
sv_ssl_ctx_(sv_ssl_ctx), cl_ssl_ctx_(cl_ssl_ctx), cert_tree_(cert_tree),
|
|
|
|
ticket_keys_(ticket_keys),
|
2015-03-10 15:11:22 +01:00
|
|
|
connect_blocker_(make_unique<ConnectBlocker>(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;
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
if (get_config()->session_cache_memcached_host) {
|
|
|
|
session_cache_memcached_dispatcher_ = make_unique<MemcachedDispatcher>(
|
2015-07-26 18:41:10 +02:00
|
|
|
&get_config()->session_cache_memcached_addr, loop);
|
2015-07-25 15:22:17 +02:00
|
|
|
}
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
if (get_config()->downstream_proto == PROTO_HTTP2) {
|
2015-03-10 15:11:22 +01:00
|
|
|
auto n = get_config()->http2_downstream_connections_per_worker;
|
2015-07-12 15:16:20 +02:00
|
|
|
size_t group = 0;
|
|
|
|
for (auto &dgrp : dgrps_) {
|
|
|
|
auto m = n;
|
|
|
|
if (m == 0) {
|
|
|
|
m = get_config()->downstream_addr_groups[group].addrs.size();
|
|
|
|
}
|
2015-07-13 14:31:37 +02:00
|
|
|
for (size_t idx = 0; idx < m; ++idx) {
|
2015-07-12 15:16:20 +02:00
|
|
|
dgrp.http2sessions.push_back(make_unique<Http2Session>(
|
2015-07-13 14:31:37 +02:00
|
|
|
loop_, cl_ssl_ctx, connect_blocker_.get(), this, group, idx));
|
2015-07-12 15:16:20 +02:00
|
|
|
}
|
|
|
|
++group;
|
2015-03-10 15:11:22 +01:00
|
|
|
}
|
2015-02-11 11:18:41 +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_);
|
2015-09-01 19:07:29 +02:00
|
|
|
ev_loop_destroy(loop_);
|
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] {
|
|
|
|
(void)reopen_log_files();
|
|
|
|
ev_run(loop_);
|
2015-09-01 19:07:29 +02: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() {
|
2015-06-12 14:28:24 +02:00
|
|
|
std::vector<WorkerEvent> q;
|
2014-12-27 18:59:06 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
|
|
|
q.swap(q_);
|
2013-09-24 16:17:53 +02:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
for (auto &wev : q) {
|
2015-01-08 14:23:30 +01:00
|
|
|
switch (wev.type) {
|
|
|
|
case NEW_CONNECTION: {
|
2015-01-08 13:20:17 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "WorkerEvent: client_fd=" << wev.client_fd
|
|
|
|
<< ", addrlen=" << wev.client_addrlen;
|
|
|
|
}
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
if (worker_stat_.num_connections >=
|
2015-01-08 13:20:17 +01:00
|
|
|
get_config()->worker_frontend_connections) {
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "Too many connections >= "
|
|
|
|
<< get_config()->worker_frontend_connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(wev.client_fd);
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto client_handler = ssl::accept_connection(
|
2015-02-11 11:18:41 +01:00
|
|
|
this, wev.client_fd, &wev.client_addr.sa, wev.client_addrlen);
|
2015-01-08 13:20:17 +01:00
|
|
|
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;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (LOG_ENABLED(INFO)) {
|
|
|
|
WLOG(INFO, this) << "CLIENT_HANDLER:" << client_handler << " created ";
|
|
|
|
}
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
2015-01-08 13:20:17 +01:00
|
|
|
}
|
2015-01-08 14:23:30 +01:00
|
|
|
case REOPEN_LOG:
|
2015-04-10 17:08:28 +02:00
|
|
|
WLOG(NOTICE, this) << "Reopening log files: worker(" << this << ")";
|
2014-12-27 18:59:06 +01:00
|
|
|
|
|
|
|
reopen_log_files();
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
|
|
|
case GRACEFUL_SHUTDOWN:
|
2015-01-08 13:20:17 +01:00
|
|
|
WLOG(NOTICE, this) << "Graceful shutdown commencing";
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-25 14:53:23 +01:00
|
|
|
graceful_shutdown_ = true;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
if (worker_stat_.num_connections == 0) {
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_break(loop_);
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
return;
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
ssl::CertLookupTree *Worker::get_cert_lookup_tree() const { return cert_tree_; }
|
|
|
|
|
2015-07-23 16:13:29 +02:00
|
|
|
std::shared_ptr<TicketKeys> Worker::get_ticket_keys() {
|
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
return ticket_keys_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::set_ticket_keys(std::shared_ptr<TicketKeys> ticket_keys) {
|
2015-07-23 16:13:29 +02:00
|
|
|
std::lock_guard<std::mutex> g(m_);
|
2015-02-11 11:18:41 +01:00
|
|
|
ticket_keys_ = std::move(ticket_keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerStat *Worker::get_worker_stat() { return &worker_stat_; }
|
|
|
|
|
|
|
|
DownstreamConnectionPool *Worker::get_dconn_pool() { return &dconn_pool_; }
|
|
|
|
|
2015-07-12 15:16:20 +02:00
|
|
|
Http2Session *Worker::next_http2_session(size_t group) {
|
|
|
|
auto &dgrp = dgrps_[group];
|
|
|
|
auto &http2sessions = dgrp.http2sessions;
|
|
|
|
if (http2sessions.empty()) {
|
2015-03-10 15:11:22 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-12 15:16:20 +02:00
|
|
|
auto res = http2sessions[dgrp.next_http2session].get();
|
|
|
|
++dgrp.next_http2session;
|
|
|
|
if (dgrp.next_http2session >= http2sessions.size()) {
|
|
|
|
dgrp.next_http2session = 0;
|
2015-03-10 15:11:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-02-11 11:18:41 +01:00
|
|
|
|
2015-03-10 15:11:22 +01:00
|
|
|
ConnectBlocker *Worker::get_connect_blocker() const {
|
|
|
|
return connect_blocker_.get();
|
2015-02-11 11:18:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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-12 15:16:20 +02:00
|
|
|
DownstreamGroup *Worker::get_dgrp(size_t group) {
|
|
|
|
assert(group < dgrps_.size());
|
|
|
|
return &dgrps_[group];
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
MemcachedDispatcher *Worker::get_session_cache_memcached_dispatcher() {
|
|
|
|
return session_cache_memcached_dispatcher_.get();
|
|
|
|
}
|
|
|
|
|
2015-09-01 17:19:32 +02:00
|
|
|
int Worker::create_mruby_context() {
|
|
|
|
mruby_ctx_ = mruby::create_mruby_context();
|
|
|
|
if (!mruby_ctx_) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mruby::MRubyContext *Worker::get_mruby_context() const {
|
|
|
|
return mruby_ctx_.get();
|
|
|
|
}
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
} // namespace shrpx
|