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"
|
|
|
|
|
|
|
|
#include <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"
|
2014-07-05 11:22:40 +02:00
|
|
|
#include "shrpx_worker_config.h"
|
2014-08-19 16:36:04 +02:00
|
|
|
#include "shrpx_connect_blocker.h"
|
2013-09-23 17:14:55 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
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-01-08 13:15:45 +01:00
|
|
|
Worker::Worker(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)
|
2014-12-27 18:59:06 +01:00
|
|
|
: loop_(ev_loop_new(0)), sv_ssl_ctx_(sv_ssl_ctx), cl_ssl_ctx_(cl_ssl_ctx),
|
|
|
|
worker_stat_(util::make_unique<WorkerStat>()) {
|
|
|
|
ev_async_init(&w_, eventcb);
|
|
|
|
w_.data = this;
|
|
|
|
ev_async_start(loop_, &w_);
|
|
|
|
|
2015-01-10 16:28:00 +01:00
|
|
|
#ifndef NOTHREADS
|
2015-01-12 16:18:27 +01:00
|
|
|
fut_ = std::async(std::launch::async, [this, cert_tree, &ticket_keys] {
|
|
|
|
if (get_config()->tls_ctx_per_worker) {
|
|
|
|
sv_ssl_ctx_ = ssl::setup_server_ssl_context();
|
|
|
|
cl_ssl_ctx_ = ssl::setup_client_ssl_context();
|
|
|
|
} else {
|
|
|
|
worker_config->cert_tree = cert_tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_config()->downstream_proto == PROTO_HTTP2) {
|
|
|
|
http2session_ = util::make_unique<Http2Session>(loop_, cl_ssl_ctx_);
|
|
|
|
} else {
|
|
|
|
http1_connect_blocker_ = util::make_unique<ConnectBlocker>(loop_);
|
|
|
|
}
|
|
|
|
|
2015-01-08 13:15:45 +01:00
|
|
|
worker_config->ticket_keys = ticket_keys;
|
|
|
|
(void)reopen_log_files();
|
|
|
|
ev_run(loop_);
|
|
|
|
});
|
2015-01-10 16:28:00 +01:00
|
|
|
#endif // !NOTHREADS
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
2012-06-05 18:26:04 +02:00
|
|
|
|
2015-01-08 13:15:45 +01:00
|
|
|
Worker::~Worker() { ev_async_stop(loop_, &w_); }
|
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
|
|
|
|
|
|
|
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() {
|
|
|
|
std::deque<WorkerEvent> q;
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (worker_stat_->num_connections >=
|
|
|
|
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(
|
|
|
|
loop_, sv_ssl_ctx_, wev.client_fd, &wev.client_addr.sa,
|
|
|
|
wev.client_addrlen, worker_stat_.get(), &dconn_pool_);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
client_handler->set_http2_session(http2session_.get());
|
|
|
|
client_handler->set_http1_connect_blocker(http1_connect_blocker_.get());
|
|
|
|
|
|
|
|
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 RENEW_TICKET_KEYS:
|
2015-01-08 13:15:45 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2015-01-08 13:20:17 +01:00
|
|
|
WLOG(INFO, this) << "Renew ticket keys: worker_info(" << worker_config
|
|
|
|
<< ")";
|
2015-01-08 13:15:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
worker_config->ticket_keys = wev.ticket_keys;
|
|
|
|
|
2015-01-08 14:23:30 +01:00
|
|
|
break;
|
|
|
|
case REOPEN_LOG:
|
2014-12-27 18:59:06 +01:00
|
|
|
if (LOG_ENABLED(INFO)) {
|
2015-01-08 13:20:17 +01:00
|
|
|
WLOG(INFO, this) << "Reopening log files: worker_info(" << worker_config
|
|
|
|
<< ")";
|
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
|
|
|
|
|
|
|
worker_config->graceful_shutdown = true;
|
|
|
|
|
|
|
|
if (worker_stat_->num_connections == 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace shrpx
|