tizen 2.4 release
[external/nghttp2.git] / src / shrpx_worker.h
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 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 #ifndef SHRPX_WORKER_H
26 #define SHRPX_WORKER_H
27
28 #include "shrpx.h"
29
30 #include <mutex>
31 #include <deque>
32 #include <thread>
33 #ifndef NOTHREADS
34 #include <future>
35 #endif // NOTHREADS
36
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
39
40 #include <ev.h>
41
42 #include "shrpx_config.h"
43 #include "shrpx_downstream_connection_pool.h"
44
45 namespace shrpx {
46
47 class Http2Session;
48 class ConnectBlocker;
49
50 namespace ssl {
51 class CertLookupTree;
52 } // namespace ssl
53
54 struct WorkerStat {
55   WorkerStat() : num_connections(0), next_downstream(0) {}
56
57   size_t num_connections;
58   // Next downstream index in Config::downstream_addrs.  For HTTP/2
59   // downstream connections, this is always 0.  For HTTP/1, this is
60   // used as load balancing.
61   size_t next_downstream;
62 };
63
64 enum WorkerEventType {
65   NEW_CONNECTION = 0x01,
66   REOPEN_LOG = 0x02,
67   GRACEFUL_SHUTDOWN = 0x03,
68   RENEW_TICKET_KEYS = 0x04,
69 };
70
71 struct WorkerEvent {
72   WorkerEventType type;
73   struct {
74     sockaddr_union client_addr;
75     size_t client_addrlen;
76     int client_fd;
77   };
78   std::shared_ptr<TicketKeys> ticket_keys;
79 };
80
81 class Worker {
82 public:
83   Worker(SSL_CTX *sv_ssl_ctx, SSL_CTX *cl_ssl_ctx,
84          ssl::CertLookupTree *cert_tree,
85          const std::shared_ptr<TicketKeys> &ticket_keys);
86   ~Worker();
87   void wait();
88   void process_events();
89   void send(const WorkerEvent &event);
90
91 private:
92 #ifndef NOTHREADS
93   std::future<void> fut_;
94 #endif // NOTHREADS
95   std::mutex m_;
96   std::deque<WorkerEvent> q_;
97   ev_async w_;
98   DownstreamConnectionPool dconn_pool_;
99   struct ev_loop *loop_;
100   SSL_CTX *sv_ssl_ctx_;
101   SSL_CTX *cl_ssl_ctx_;
102   std::unique_ptr<Http2Session> http2session_;
103   std::unique_ptr<ConnectBlocker> http1_connect_blocker_;
104   std::unique_ptr<WorkerStat> worker_stat_;
105 };
106
107 } // namespace shrpx
108
109 #endif // SHRPX_WORKER_H