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.
|
|
|
|
*/
|
|
|
|
#ifndef SHRPX_WORKER_H
|
|
|
|
#define SHRPX_WORKER_H
|
|
|
|
|
|
|
|
#include "shrpx.h"
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <mutex>
|
2015-03-10 15:11:22 +01:00
|
|
|
#include <vector>
|
2016-01-15 15:04:58 +01:00
|
|
|
#include <random>
|
2016-02-06 15:07:00 +01:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <deque>
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <thread>
|
|
|
|
#ifndef NOTHREADS
|
|
|
|
#include <future>
|
|
|
|
#endif // NOTHREADS
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
#include <ev.h>
|
|
|
|
|
|
|
|
#include "shrpx_config.h"
|
|
|
|
#include "shrpx_downstream_connection_pool.h"
|
2015-04-07 15:13:01 +02:00
|
|
|
#include "memchunk.h"
|
2016-04-28 15:25:55 +02:00
|
|
|
#include "shrpx_ssl.h"
|
2016-09-04 16:30:24 +02:00
|
|
|
#include "shrpx_live_check.h"
|
|
|
|
#include "shrpx_connect_blocker.h"
|
2016-12-04 15:43:41 +01:00
|
|
|
#include "shrpx_dns_tracker.h"
|
2016-10-02 09:18:35 +02:00
|
|
|
#include "allocator.h"
|
2015-04-07 15:13:01 +02:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
2012-07-14 16:24:03 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
namespace shrpx {
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
class Http2Session;
|
|
|
|
class ConnectBlocker;
|
2015-07-25 15:22:17 +02:00
|
|
|
class MemcachedDispatcher;
|
2016-02-07 09:51:53 +01:00
|
|
|
struct UpstreamAddr;
|
2016-06-03 18:02:57 +02:00
|
|
|
class ConnectionHandler;
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
namespace mruby {
|
|
|
|
|
|
|
|
class MRubyContext;
|
|
|
|
|
|
|
|
} // namespace mruby
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
|
2015-01-12 16:18:27 +01:00
|
|
|
namespace ssl {
|
2015-02-02 14:47:12 +01:00
|
|
|
class CertLookupTree;
|
2015-01-12 16:18:27 +01:00
|
|
|
} // namespace ssl
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
struct DownstreamAddr {
|
|
|
|
Address addr;
|
|
|
|
// backend address. If |host_unix| is true, this is UNIX domain
|
|
|
|
// socket path.
|
2016-10-02 09:18:35 +02:00
|
|
|
StringRef host;
|
|
|
|
StringRef hostport;
|
2016-02-27 15:24:14 +01:00
|
|
|
// backend port. 0 if |host_unix| is true.
|
|
|
|
uint16_t port;
|
|
|
|
// true if |host| contains UNIX domain socket path.
|
|
|
|
bool host_unix;
|
|
|
|
|
2016-04-29 07:42:18 +02:00
|
|
|
// sni field to send remote server if TLS is enabled.
|
2016-10-02 09:18:35 +02:00
|
|
|
StringRef sni;
|
2016-04-29 07:42:18 +02:00
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
std::unique_ptr<ConnectBlocker> connect_blocker;
|
2016-04-07 18:04:16 +02:00
|
|
|
std::unique_ptr<LiveCheck> live_check;
|
2016-06-09 15:35:59 +02:00
|
|
|
// Connection pool for this particular address if session affinity
|
|
|
|
// is enabled
|
|
|
|
std::unique_ptr<DownstreamConnectionPool> dconn_pool;
|
2016-04-09 14:58:08 +02:00
|
|
|
size_t fall;
|
|
|
|
size_t rise;
|
2016-02-27 15:24:14 +01:00
|
|
|
// Client side TLS session cache
|
2016-04-28 15:25:55 +02:00
|
|
|
ssl::TLSSessionCache tls_session_cache;
|
2016-04-02 16:11:03 +02:00
|
|
|
// Http2Session object created for this address. This list chains
|
|
|
|
// all Http2Session objects that is not in group scope
|
|
|
|
// http2_avail_freelist, and is not reached in maximum concurrency.
|
2016-06-09 16:17:41 +02:00
|
|
|
//
|
|
|
|
// If session affinity is enabled, http2_avail_freelist is not used,
|
|
|
|
// and this list is solely used.
|
2016-04-02 16:11:03 +02:00
|
|
|
DList<Http2Session> http2_extra_freelist;
|
|
|
|
// true if Http2Session for this address is in group scope
|
|
|
|
// SharedDownstreamAddr.http2_avail_freelist
|
|
|
|
bool in_avail;
|
|
|
|
// total number of streams created in HTTP/2 connections for this
|
|
|
|
// address.
|
|
|
|
size_t num_dconn;
|
2016-05-24 16:36:43 +02:00
|
|
|
// Application protocol used in this backend
|
|
|
|
shrpx_proto proto;
|
|
|
|
// true if TLS is used in this backend
|
|
|
|
bool tls;
|
2016-12-04 15:43:41 +01:00
|
|
|
// true if dynamic DNS is enabled
|
|
|
|
bool dns;
|
2016-05-24 16:36:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Simplified weighted fair queuing. Actually we don't use queue here
|
|
|
|
// since we have just 2 items. This is the same algorithm used in
|
|
|
|
// stream priority, but ignores remainder.
|
|
|
|
struct WeightedPri {
|
|
|
|
// current cycle of this item. The lesser cycle has higher
|
|
|
|
// priority. This is unsigned 32 bit integer, so it may overflow.
|
|
|
|
// But with the same theory described in stream priority, it is no
|
|
|
|
// problem.
|
|
|
|
uint32_t cycle;
|
2016-05-25 16:07:04 +02:00
|
|
|
// weight, larger weight means more frequent use.
|
|
|
|
uint32_t weight;
|
2016-02-27 15:24:14 +01:00
|
|
|
};
|
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
struct SharedDownstreamAddr {
|
2016-09-04 16:30:24 +02:00
|
|
|
SharedDownstreamAddr()
|
2016-10-02 09:18:35 +02:00
|
|
|
: balloc(1024, 1024),
|
|
|
|
next{0},
|
|
|
|
http1_pri{},
|
|
|
|
http2_pri{},
|
2017-01-28 14:19:14 +01:00
|
|
|
affinity{AFFINITY_NONE},
|
|
|
|
require_upstream_tls{false} {}
|
2016-10-02 09:18:35 +02:00
|
|
|
|
|
|
|
SharedDownstreamAddr(const SharedDownstreamAddr &) = delete;
|
|
|
|
SharedDownstreamAddr(SharedDownstreamAddr &&) = delete;
|
|
|
|
SharedDownstreamAddr &operator=(const SharedDownstreamAddr &) = delete;
|
|
|
|
SharedDownstreamAddr &operator=(SharedDownstreamAddr &&) = delete;
|
|
|
|
|
|
|
|
BlockAllocator balloc;
|
2016-02-27 15:24:14 +01:00
|
|
|
std::vector<DownstreamAddr> addrs;
|
2016-07-06 15:31:28 +02:00
|
|
|
// Bunch of session affinity hash. Only used if affinity ==
|
|
|
|
// AFFINITY_IP.
|
|
|
|
std::vector<AffinityHash> affinity_hash;
|
2016-02-27 15:24:14 +01:00
|
|
|
// List of Http2Session which is not fully utilized (i.e., the
|
2017-01-28 14:18:17 +01:00
|
|
|
// server advertised maximum concurrency is not reached). We will
|
2016-02-27 15:24:14 +01:00
|
|
|
// coalesce as much stream as possible in one Http2Session to fully
|
|
|
|
// utilize TCP connection.
|
|
|
|
//
|
2016-06-09 16:17:41 +02:00
|
|
|
// If session affinity is enabled, this list is not used. Per
|
|
|
|
// address http2_extra_freelist is used instead.
|
|
|
|
//
|
2016-02-27 15:24:14 +01:00
|
|
|
// TODO Verify that this approach performs better in performance
|
|
|
|
// wise.
|
2016-04-02 16:11:03 +02:00
|
|
|
DList<Http2Session> http2_avail_freelist;
|
2016-02-27 15:24:14 +01:00
|
|
|
DownstreamConnectionPool dconn_pool;
|
2016-05-24 16:36:43 +02:00
|
|
|
// Next http/1.1 downstream address index in addrs.
|
2016-02-27 15:24:14 +01:00
|
|
|
size_t next;
|
2016-05-24 16:36:43 +02:00
|
|
|
// http1_pri and http2_pri are used to which protocols are used
|
|
|
|
// between HTTP/1.1 or HTTP/2 if they both are available in
|
|
|
|
// backends. They are choosed proportional to the number available
|
|
|
|
// backend. Usually, if http1_pri.cycle < http2_pri.cycle, choose
|
|
|
|
// HTTP/1.1. Otherwise, choose HTTP/2.
|
|
|
|
WeightedPri http1_pri;
|
|
|
|
WeightedPri http2_pri;
|
2016-06-09 15:35:59 +02:00
|
|
|
// Session affinity
|
|
|
|
shrpx_session_affinity affinity;
|
2017-01-28 14:19:14 +01:00
|
|
|
// true if this group requires that client connection must be TLS.
|
|
|
|
bool require_upstream_tls;
|
2016-02-27 15:24:14 +01:00
|
|
|
};
|
2014-06-26 15:55:22 +02:00
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
struct DownstreamAddrGroup {
|
2016-09-04 16:30:24 +02:00
|
|
|
DownstreamAddrGroup() : retired{false} {};
|
|
|
|
|
2016-10-02 09:18:35 +02:00
|
|
|
DownstreamAddrGroup(const DownstreamAddrGroup &) = delete;
|
|
|
|
DownstreamAddrGroup(DownstreamAddrGroup &&) = delete;
|
|
|
|
DownstreamAddrGroup &operator=(const DownstreamAddrGroup &) = delete;
|
|
|
|
DownstreamAddrGroup &operator=(DownstreamAddrGroup &&) = delete;
|
|
|
|
|
2016-03-22 15:51:00 +01:00
|
|
|
ImmutableString pattern;
|
|
|
|
std::shared_ptr<SharedDownstreamAddr> shared_addr;
|
2016-06-04 05:16:31 +02:00
|
|
|
// true if this group is no longer used for new request. If this is
|
|
|
|
// true, the connection made using one of address in shared_addr
|
|
|
|
// must not be pooled.
|
|
|
|
bool retired;
|
2016-03-22 15:51:00 +01:00
|
|
|
};
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
struct WorkerStat {
|
2014-06-26 15:55:22 +02:00
|
|
|
size_t num_connections;
|
|
|
|
};
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
enum WorkerEventType {
|
|
|
|
NEW_CONNECTION = 0x01,
|
|
|
|
REOPEN_LOG = 0x02,
|
|
|
|
GRACEFUL_SHUTDOWN = 0x03,
|
2016-06-03 18:02:57 +02:00
|
|
|
REPLACE_DOWNSTREAM = 0x04,
|
2014-12-27 18:59:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct WorkerEvent {
|
|
|
|
WorkerEventType type;
|
2015-01-08 13:15:45 +01:00
|
|
|
struct {
|
|
|
|
sockaddr_union client_addr;
|
|
|
|
size_t client_addrlen;
|
|
|
|
int client_fd;
|
2016-02-07 09:51:53 +01:00
|
|
|
const UpstreamAddr *faddr;
|
2014-12-27 18:59:06 +01:00
|
|
|
};
|
2015-01-08 13:15:45 +01:00
|
|
|
std::shared_ptr<TicketKeys> ticket_keys;
|
2016-06-03 18:02:57 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf;
|
2014-12-27 18:59:06 +01:00
|
|
|
};
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
class Worker {
|
|
|
|
public:
|
2015-02-11 11:18:41 +01:00
|
|
|
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,
|
2015-01-12 16:18:27 +01:00
|
|
|
ssl::CertLookupTree *cert_tree,
|
2016-06-03 18:02:57 +02:00
|
|
|
const std::shared_ptr<TicketKeys> &ticket_keys,
|
|
|
|
ConnectionHandler *conn_handler,
|
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf);
|
2012-06-05 18:26:04 +02:00
|
|
|
~Worker();
|
2015-02-11 11:18:41 +01:00
|
|
|
void run_async();
|
2014-12-27 18:59:06 +01:00
|
|
|
void wait();
|
|
|
|
void process_events();
|
|
|
|
void send(const WorkerEvent &event);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
ssl::CertLookupTree *get_cert_lookup_tree() const;
|
2015-07-23 16:13:29 +02:00
|
|
|
|
|
|
|
// These 2 functions make a lock m_ to get/set ticket keys
|
|
|
|
// atomically.
|
|
|
|
std::shared_ptr<TicketKeys> get_ticket_keys();
|
2015-02-11 11:18:41 +01:00
|
|
|
void set_ticket_keys(std::shared_ptr<TicketKeys> ticket_keys);
|
2015-07-23 16:13:29 +02:00
|
|
|
|
2015-02-11 11:18:41 +01:00
|
|
|
WorkerStat *get_worker_stat();
|
|
|
|
struct ev_loop *get_loop() const;
|
|
|
|
SSL_CTX *get_sv_ssl_ctx() const;
|
2015-03-10 13:54:29 +01:00
|
|
|
SSL_CTX *get_cl_ssl_ctx() const;
|
2015-02-11 11:18:41 +01:00
|
|
|
|
2015-02-25 14:53:23 +01:00
|
|
|
void set_graceful_shutdown(bool f);
|
|
|
|
bool get_graceful_shutdown() const;
|
|
|
|
|
2015-04-07 15:13:01 +02:00
|
|
|
MemchunkPool *get_mcpool();
|
2015-04-08 06:43:57 +02:00
|
|
|
void schedule_clear_mcpool();
|
2015-04-07 15:13:01 +02:00
|
|
|
|
2015-07-25 15:22:17 +02:00
|
|
|
MemcachedDispatcher *get_session_cache_memcached_dispatcher();
|
|
|
|
|
2016-01-15 15:04:58 +01:00
|
|
|
std::mt19937 &get_randgen();
|
|
|
|
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
int create_mruby_context();
|
|
|
|
|
|
|
|
mruby::MRubyContext *get_mruby_context() const;
|
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>> &
|
|
|
|
get_downstream_addr_groups();
|
2016-02-21 06:53:06 +01:00
|
|
|
|
2016-02-21 07:27:19 +01:00
|
|
|
ConnectBlocker *get_connect_blocker() const;
|
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
const DownstreamConfig *get_downstream_config() const;
|
|
|
|
|
2016-06-03 18:02:57 +02:00
|
|
|
void
|
|
|
|
replace_downstream_config(std::shared_ptr<DownstreamConfig> downstreamconf);
|
|
|
|
|
|
|
|
ConnectionHandler *get_connection_handler() const;
|
2016-06-02 18:20:49 +02:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
DNSTracker *get_dns_tracker();
|
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
private:
|
2014-12-27 18:59:06 +01:00
|
|
|
#ifndef NOTHREADS
|
|
|
|
std::future<void> fut_;
|
|
|
|
#endif // NOTHREADS
|
|
|
|
std::mutex m_;
|
2016-06-25 04:50:33 +02:00
|
|
|
std::deque<WorkerEvent> q_;
|
2016-01-15 15:04:58 +01:00
|
|
|
std::mt19937 randgen_;
|
2014-12-27 18:59:06 +01:00
|
|
|
ev_async w_;
|
2015-04-08 06:43:57 +02:00
|
|
|
ev_timer mcpool_clear_timer_;
|
2016-06-25 04:50:33 +02:00
|
|
|
ev_timer proc_wev_timer_;
|
2015-04-07 15:13:01 +02:00
|
|
|
MemchunkPool mcpool_;
|
2015-02-11 11:18:41 +01:00
|
|
|
WorkerStat worker_stat_;
|
2016-12-04 15:43:41 +01:00
|
|
|
DNSTracker dns_tracker_;
|
2016-02-07 08:52:40 +01:00
|
|
|
|
2016-06-03 12:13:02 +02:00
|
|
|
std::shared_ptr<DownstreamConfig> downstreamconf_;
|
2015-07-25 15:22:17 +02:00
|
|
|
std::unique_ptr<MemcachedDispatcher> session_cache_memcached_dispatcher_;
|
2015-09-03 17:54:41 +02:00
|
|
|
#ifdef HAVE_MRUBY
|
2015-09-01 17:19:32 +02:00
|
|
|
std::unique_ptr<mruby::MRubyContext> mruby_ctx_;
|
2015-09-03 17:54:41 +02:00
|
|
|
#endif // HAVE_MRUBY
|
2014-12-27 18:59:06 +01:00
|
|
|
struct ev_loop *loop_;
|
2015-02-11 11:18:41 +01:00
|
|
|
|
|
|
|
// Following fields are shared across threads if
|
|
|
|
// get_config()->tls_ctx_per_worker == true.
|
2013-02-07 13:13:36 +01:00
|
|
|
SSL_CTX *sv_ssl_ctx_;
|
|
|
|
SSL_CTX *cl_ssl_ctx_;
|
2015-02-11 11:18:41 +01:00
|
|
|
ssl::CertLookupTree *cert_tree_;
|
2016-06-03 18:02:57 +02:00
|
|
|
ConnectionHandler *conn_handler_;
|
2015-02-11 11:18:41 +01:00
|
|
|
|
2016-09-20 15:24:12 +02:00
|
|
|
#ifndef HAVE_ATOMIC_STD_SHARED_PTR
|
|
|
|
std::mutex ticket_keys_m_;
|
|
|
|
#endif // !HAVE_ATOMIC_STD_SHARED_PTR
|
2015-02-11 11:18:41 +01:00
|
|
|
std::shared_ptr<TicketKeys> ticket_keys_;
|
2016-06-02 18:20:49 +02:00
|
|
|
std::vector<std::shared_ptr<DownstreamAddrGroup>> downstream_addr_groups_;
|
2016-02-21 07:27:19 +01:00
|
|
|
// Worker level blocker for downstream connection. For example,
|
|
|
|
// this is used when file decriptor is exhausted.
|
|
|
|
std::unique_ptr<ConnectBlocker> connect_blocker_;
|
2015-02-25 14:53:23 +01:00
|
|
|
|
|
|
|
bool graceful_shutdown_;
|
2012-06-05 18:26:04 +02:00
|
|
|
};
|
|
|
|
|
2016-02-27 15:24:14 +01:00
|
|
|
// Selects group based on request's |hostport| and |path|. |hostport|
|
|
|
|
// is the value taken from :authority or host header field, and may
|
|
|
|
// contain port. The |path| may contain query part. We require the
|
|
|
|
// catch-all pattern in place, so this function always selects one
|
|
|
|
// group. The catch-all group index is given in |catch_all|. All
|
|
|
|
// patterns are given in |groups|.
|
|
|
|
size_t match_downstream_addr_group(
|
2016-06-11 11:21:37 +02:00
|
|
|
const RouterConfig &routerconfig, const StringRef &hostport,
|
|
|
|
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-02-27 15:24:14 +01:00
|
|
|
|
2016-12-04 15:43:41 +01:00
|
|
|
// Calls this function if connecting to backend failed. |raddr| is
|
|
|
|
// the actual address used to connect to backend, and it could be
|
|
|
|
// nullptr. This function may schedule live check.
|
|
|
|
void downstream_failure(DownstreamAddr *addr, const Address *raddr);
|
2016-04-07 18:04:16 +02:00
|
|
|
|
2012-06-05 18:26:04 +02:00
|
|
|
} // namespace shrpx
|
|
|
|
|
|
|
|
#endif // SHRPX_WORKER_H
|