xref: /unit/src/nxt_router.h (revision 2166:64a3527f65ad)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_ROUTER_H_INCLUDED_
8 #define _NXT_ROUTER_H_INCLUDED_
9 
10 
11 #include <nxt_main.h>
12 #include <nxt_runtime.h>
13 #include <nxt_main_process.h>
14 
15 typedef struct nxt_http_request_s  nxt_http_request_t;
16 #include <nxt_application.h>
17 
18 
19 typedef struct nxt_http_action_s        nxt_http_action_t;
20 typedef struct nxt_http_routes_s        nxt_http_routes_t;
21 typedef struct nxt_http_forward_s       nxt_http_forward_t;
22 typedef struct nxt_upstream_s           nxt_upstream_t;
23 typedef struct nxt_upstreams_s          nxt_upstreams_t;
24 typedef struct nxt_router_access_log_s  nxt_router_access_log_t;
25 
26 
27 #define NXT_HTTP_ACTION_ERROR  ((nxt_http_action_t *) -1)
28 
29 
30 typedef struct {
31     nxt_thread_spinlock_t    lock;
32     nxt_queue_t              engines;
33 
34     nxt_queue_t              sockets;  /* of nxt_socket_conf_t */
35     nxt_queue_t              apps;     /* of nxt_app_t */
36 
37     nxt_router_access_log_t  *access_log;
38 } nxt_router_t;
39 
40 
41 typedef struct {
42     uint32_t                 count;
43     uint32_t                 threads;
44 
45     nxt_mp_t                 *mem_pool;
46     nxt_array_t              *var_fields;  /* of nxt_var_field_t */
47 
48     nxt_router_t             *router;
49     nxt_http_routes_t        *routes;
50     nxt_upstreams_t          *upstreams;
51 
52     nxt_lvlhsh_t             mtypes_hash;
53     nxt_lvlhsh_t             apps_hash;
54 
55     nxt_router_access_log_t  *access_log;
56     nxt_var_t                *log_format;
57 } nxt_router_conf_t;
58 
59 
60 typedef struct {
61     nxt_event_engine_t     *engine;
62     nxt_work_t             *jobs;
63 
64     enum {
65         NXT_ROUTER_ENGINE_KEEP = 0,
66         NXT_ROUTER_ENGINE_ADD,
67         NXT_ROUTER_ENGINE_DELETE,
68     }                      action;
69 } nxt_router_engine_conf_t;
70 
71 
72 typedef struct {
73 #if (NXT_TLS)
74     nxt_queue_t            tls;        /* of nxt_router_tlssock_t */
75 #endif
76 
77     nxt_queue_t            apps;       /* of nxt_app_t */
78     nxt_queue_t            previous;   /* of nxt_app_t */
79 
80     uint32_t               new_threads;
81     uint32_t               stream;
82     uint32_t               count;
83 
84     nxt_event_engine_t     *engine;
85     nxt_port_t             *port;
86     nxt_array_t            *engines;
87     nxt_router_conf_t      *router_conf;
88     nxt_mp_t               *mem_pool;
89 } nxt_router_temp_conf_t;
90 
91 
92 typedef struct {
93     nxt_task_t              task;
94     nxt_work_t              work;
95     nxt_router_temp_conf_t  *tmcf;
96 } nxt_joint_job_t;
97 
98 
99 typedef struct {
100     uint32_t               use_count;
101     nxt_app_t              *app;
102     nxt_timer_t            idle_timer;
103     nxt_work_t             free_app_work;
104 } nxt_app_joint_t;
105 
106 
107 struct nxt_app_s {
108     nxt_thread_mutex_t     mutex;       /* Protects ports queue. */
109     nxt_queue_t            ports;       /* of nxt_port_t.app_link */
110     nxt_lvlhsh_t           port_hash;   /* of nxt_port_t */
111 
112     nxt_queue_t            spare_ports; /* of nxt_port_t.idle_link */
113     nxt_queue_t            idle_ports;  /* of nxt_port_t.idle_link */
114     nxt_work_t             adjust_idle_work;
115     nxt_event_engine_t     *engine;
116 
117     nxt_str_t              name;
118 
119     uint32_t               port_hash_count;
120 
121     uint32_t               active_requests;
122     uint32_t               pending_processes;
123     uint32_t               processes;
124     uint32_t               idle_processes;
125 
126     uint32_t               max_processes;
127     uint32_t               spare_processes;
128     uint32_t               max_pending_processes;
129 
130     uint32_t               generation;
131     uint32_t               proto_port_requests;
132 
133     nxt_msec_t             timeout;
134     nxt_msec_t             idle_timeout;
135 
136     nxt_str_t              *targets;
137 
138     nxt_app_type_t         type:8;
139 
140     nxt_mp_t               *mem_pool;
141     nxt_queue_link_t       link;
142 
143     nxt_str_t              conf;
144 
145     nxt_atomic_t           use_count;
146     nxt_queue_t            ack_waiting_req; /* of nxt_http_request_t.app_link */
147 
148     nxt_app_joint_t        *joint;
149     nxt_port_t             *shared_port;
150     nxt_port_t             *proto_port;
151 
152     nxt_port_mmaps_t       outgoing;
153 };
154 
155 
156 typedef struct {
157     size_t                 max_frame_size;
158     nxt_msec_t             read_timeout;
159     nxt_msec_t             keepalive_interval;
160 } nxt_websocket_conf_t;
161 
162 
163 typedef struct {
164     uint32_t               count;
165     nxt_queue_link_t       link;
166     nxt_router_conf_t      *router_conf;
167 
168     nxt_http_action_t      *action;
169 
170     /*
171      * A listen socket time can be shorter than socket configuration life
172      * time, so a copy of the non-wildcard socket sockaddr is stored here
173      * to be used as a local sockaddr in connections.
174      */
175     nxt_sockaddr_t         *sockaddr;
176 
177     nxt_listen_socket_t    *listen;
178 
179     size_t                 header_buffer_size;
180     size_t                 large_header_buffer_size;
181     size_t                 large_header_buffers;
182     size_t                 body_buffer_size;
183     size_t                 max_body_size;
184     size_t                 proxy_header_buffer_size;
185     size_t                 proxy_buffer_size;
186     size_t                 proxy_buffers;
187 
188     nxt_msec_t             idle_timeout;
189     nxt_msec_t             header_read_timeout;
190     nxt_msec_t             body_read_timeout;
191     nxt_msec_t             send_timeout;
192     nxt_msec_t             proxy_timeout;
193     nxt_msec_t             proxy_send_timeout;
194     nxt_msec_t             proxy_read_timeout;
195 
196     nxt_websocket_conf_t   websocket_conf;
197 
198     nxt_str_t              body_temp_path;
199 
200     uint8_t                discard_unsafe_fields;  /* 1 bit */
201 
202     nxt_http_forward_t     *forwarded;
203     nxt_http_forward_t     *client_ip;
204 
205 #if (NXT_TLS)
206     nxt_tls_conf_t         *tls;
207 #endif
208 } nxt_socket_conf_t;
209 
210 
211 typedef struct {
212     uint32_t               count;
213     nxt_queue_link_t       link;
214     nxt_event_engine_t     *engine;
215     nxt_socket_conf_t      *socket_conf;
216 
217     nxt_joint_job_t        *close_job;
218 
219     nxt_upstream_t         **upstreams;
220 
221     /* Modules configuraitons. */
222 } nxt_socket_conf_joint_t;
223 
224 
225 struct nxt_router_access_log_s {
226     void                   (*handler)(nxt_task_t *task, nxt_http_request_t *r,
227                                       nxt_router_access_log_t *access_log,
228                                       nxt_var_t *format);
229     nxt_fd_t               fd;
230     nxt_str_t              path;
231     uint32_t               count;
232 };
233 
234 
235 void nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r,
236     nxt_http_action_t *action);
237 void nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port);
238 nxt_int_t nxt_router_application_init(nxt_router_conf_t *rtcf, nxt_str_t *name,
239     nxt_str_t *target, nxt_http_action_t *action);
240 void nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev,
241     nxt_socket_conf_joint_t *joint);
242 
243 void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data);
244 void nxt_router_conf_error(nxt_task_t *task, nxt_router_temp_conf_t *tmcf);
245 void nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint);
246 
247 nxt_int_t nxt_router_access_log_create(nxt_task_t *task,
248     nxt_router_conf_t *rtcf, nxt_conf_value_t *value);
249 void nxt_router_access_log_open(nxt_task_t *task, nxt_router_temp_conf_t *tmcf);
250 void nxt_router_access_log_use(nxt_thread_spinlock_t *lock,
251     nxt_router_access_log_t *access_log);
252 void nxt_router_access_log_release(nxt_task_t *task,
253     nxt_thread_spinlock_t *lock, nxt_router_access_log_t *access_log);
254 void nxt_router_access_log_reopen_handler(nxt_task_t *task,
255     nxt_port_recv_msg_t *msg);
256 
257 
258 extern nxt_router_t  *nxt_router;
259 
260 
261 #endif  /* _NXT_ROUTER_H_INCLUDED_ */
262