xref: /unit/src/nxt_router.h (revision 1131:ec7d924d8dfb)
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_pass_s          nxt_http_pass_t;
20 typedef struct nxt_http_routes_s        nxt_http_routes_t;
21 typedef struct nxt_router_access_log_s  nxt_router_access_log_t;
22 
23 
24 #define NXT_HTTP_PASS_ERROR  ((nxt_http_pass_t *) -1)
25 
26 
27 typedef struct {
28     nxt_thread_spinlock_t    lock;
29     nxt_queue_t              engines;
30 
31     nxt_queue_t              sockets;  /* of nxt_socket_conf_t */
32     nxt_queue_t              apps;     /* of nxt_app_t */
33 
34     nxt_router_access_log_t  *access_log;
35 } nxt_router_t;
36 
37 
38 typedef struct {
39     uint32_t                 count;
40     uint32_t                 threads;
41     nxt_router_t             *router;
42     nxt_http_routes_t        *routes;
43     nxt_mp_t                 *mem_pool;
44 
45     nxt_router_access_log_t  *access_log;
46 } nxt_router_conf_t;
47 
48 
49 typedef struct {
50     nxt_event_engine_t     *engine;
51     nxt_work_t             *jobs;
52 
53     enum {
54         NXT_ROUTER_ENGINE_KEEP = 0,
55         NXT_ROUTER_ENGINE_ADD,
56         NXT_ROUTER_ENGINE_DELETE,
57     }                      action;
58 } nxt_router_engine_conf_t;
59 
60 
61 typedef struct {
62     nxt_queue_t            creating;   /* of nxt_socket_conf_t */
63     nxt_queue_t            pending;    /* of nxt_socket_conf_t */
64     nxt_queue_t            updating;   /* of nxt_socket_conf_t */
65     nxt_queue_t            keeping;    /* of nxt_socket_conf_t */
66     nxt_queue_t            deleting;   /* of nxt_socket_conf_t */
67 
68 #if (NXT_TLS)
69     nxt_queue_t            tls;        /* of nxt_router_tlssock_t */
70 #endif
71 
72     nxt_queue_t            apps;       /* of nxt_app_t */
73     nxt_queue_t            previous;   /* of nxt_app_t */
74 
75     uint32_t               new_threads;
76     uint32_t               stream;
77     uint32_t               count;
78 
79     nxt_event_engine_t     *engine;
80     nxt_port_t             *port;
81     nxt_array_t            *engines;
82     nxt_router_conf_t      *router_conf;
83     nxt_mp_t               *mem_pool;
84 } nxt_router_temp_conf_t;
85 
86 
87 typedef struct {
88     nxt_task_t              task;
89     nxt_work_t              work;
90     nxt_router_temp_conf_t  *tmcf;
91 } nxt_joint_job_t;
92 
93 
94 typedef struct {
95     uint32_t               use_count;
96     nxt_app_t              *app;
97     nxt_timer_t            idle_timer;
98     nxt_work_t             free_app_work;
99 } nxt_app_joint_t;
100 
101 
102 struct nxt_app_s {
103     nxt_thread_mutex_t     mutex;    /* Protects ports queue. */
104     nxt_queue_t            ports;    /* of nxt_port_t.app_link */
105 
106     nxt_queue_t            spare_ports; /* of nxt_port_t.idle_link */
107     nxt_queue_t            idle_ports;  /* of nxt_port_t.idle_link */
108     nxt_work_t             adjust_idle_work;
109     nxt_event_engine_t     *engine;
110 
111     nxt_queue_t            requests; /* of nxt_request_app_link_t */
112     nxt_queue_t            pending;  /* of nxt_request_app_link_t */
113     nxt_str_t              name;
114 
115     uint32_t               pending_processes;
116     uint32_t               processes;
117     uint32_t               idle_processes;
118 
119     uint32_t               max_processes;
120     uint32_t               spare_processes;
121     uint32_t               max_pending_processes;
122     uint32_t               max_pending_responses;
123     uint32_t               max_requests;
124 
125     nxt_msec_t             timeout;
126     nxt_nsec_t             res_timeout;
127     nxt_msec_t             idle_timeout;
128 
129     nxt_app_type_t         type:8;
130 
131     nxt_queue_link_t       link;
132 
133     nxt_str_t              conf;
134 
135     nxt_atomic_t           use_count;
136 
137     nxt_app_joint_t        *joint;
138 };
139 
140 
141 typedef struct {
142     size_t                 max_frame_size;
143     nxt_msec_t             read_timeout;
144     nxt_msec_t             keepalive_interval;
145 } nxt_websocket_conf_t;
146 
147 
148 typedef struct {
149     uint32_t               count;
150     nxt_queue_link_t       link;
151     nxt_router_conf_t      *router_conf;
152 
153     nxt_http_pass_t        *pass;
154 
155     /*
156      * A listen socket time can be shorter than socket configuration life
157      * time, so a copy of the non-wildcard socket sockaddr is stored here
158      * to be used as a local sockaddr in connections.
159      */
160     nxt_sockaddr_t         *sockaddr;
161 
162     nxt_listen_socket_t    *listen;
163 
164     size_t                 header_buffer_size;
165     size_t                 large_header_buffer_size;
166     size_t                 large_header_buffers;
167     size_t                 body_buffer_size;
168     size_t                 max_body_size;
169     nxt_msec_t             idle_timeout;
170     nxt_msec_t             header_read_timeout;
171     nxt_msec_t             body_read_timeout;
172     nxt_msec_t             send_timeout;
173 
174     nxt_websocket_conf_t   websocket_conf;
175 
176 #if (NXT_TLS)
177     nxt_tls_conf_t         *tls;
178 #endif
179 } nxt_socket_conf_t;
180 
181 
182 typedef struct {
183     uint32_t               count;
184     nxt_queue_link_t       link;
185     nxt_event_engine_t     *engine;
186     nxt_socket_conf_t      *socket_conf;
187 
188     /* Modules configuraitons. */
189 } nxt_socket_conf_joint_t;
190 
191 
192 struct nxt_router_access_log_s {
193     void                   (*handler)(nxt_task_t *task, nxt_http_request_t *r,
194                                       nxt_router_access_log_t *access_log);
195     nxt_fd_t               fd;
196     nxt_str_t              path;
197     uint32_t               count;
198 };
199 
200 
201 void nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
202 void nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
203 void nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg);
204 void nxt_router_access_log_reopen_handler(nxt_task_t *task,
205     nxt_port_recv_msg_t *msg);
206 
207 void nxt_router_process_http_request(nxt_task_t *task, nxt_http_request_t *r,
208     nxt_app_t *app);
209 void nxt_router_app_port_close(nxt_task_t *task, nxt_port_t *port);
210 nxt_app_t *nxt_router_listener_application(nxt_router_temp_conf_t *tmcf,
211     nxt_str_t *name);
212 void nxt_router_app_use(nxt_task_t *task, nxt_app_t *app, int i);
213 void nxt_router_listen_event_release(nxt_task_t *task, nxt_listen_event_t *lev,
214     nxt_socket_conf_joint_t *joint);
215 void nxt_router_conf_release(nxt_task_t *task, nxt_socket_conf_joint_t *joint);
216 
217 
218 #endif  /* _NXT_ROUTER_H_INCLUDED_ */
219