xref: /unit/src/nxt_router.c (revision 426)
120Sigor@sysoev.ru 
220Sigor@sysoev.ru /*
320Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
420Sigor@sysoev.ru  * Copyright (C) Valentin V. Bartenev
520Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
620Sigor@sysoev.ru  */
720Sigor@sysoev.ru 
853Sigor@sysoev.ru #include <nxt_router.h>
9115Sigor@sysoev.ru #include <nxt_conf.h>
1020Sigor@sysoev.ru 
1120Sigor@sysoev.ru 
12115Sigor@sysoev.ru typedef struct {
13318Smax.romanov@nginx.com     nxt_str_t         type;
14318Smax.romanov@nginx.com     uint32_t          workers;
15318Smax.romanov@nginx.com     nxt_msec_t        timeout;
16318Smax.romanov@nginx.com     uint32_t          requests;
17318Smax.romanov@nginx.com     nxt_conf_value_t  *limits_value;
18133Sigor@sysoev.ru } nxt_router_app_conf_t;
19133Sigor@sysoev.ru 
20133Sigor@sysoev.ru 
21133Sigor@sysoev.ru typedef struct {
22133Sigor@sysoev.ru     nxt_str_t  application;
23115Sigor@sysoev.ru } nxt_router_listener_conf_t;
24115Sigor@sysoev.ru 
25115Sigor@sysoev.ru 
26423Smax.romanov@nginx.com typedef struct nxt_msg_info_s {
27423Smax.romanov@nginx.com     nxt_buf_t                 *buf;
28423Smax.romanov@nginx.com     nxt_port_mmap_tracking_t  tracking;
29423Smax.romanov@nginx.com     nxt_work_handler_t        completion_handler;
30423Smax.romanov@nginx.com } nxt_msg_info_t;
31423Smax.romanov@nginx.com 
32423Smax.romanov@nginx.com 
33167Smax.romanov@nginx.com typedef struct nxt_req_app_link_s nxt_req_app_link_t;
34141Smax.romanov@nginx.com 
35141Smax.romanov@nginx.com 
36318Smax.romanov@nginx.com typedef struct {
37318Smax.romanov@nginx.com     uint32_t             stream;
38318Smax.romanov@nginx.com     nxt_conn_t           *conn;
39343Smax.romanov@nginx.com     nxt_app_t            *app;
40318Smax.romanov@nginx.com     nxt_port_t           *app_port;
41346Smax.romanov@nginx.com     nxt_app_parse_ctx_t  *ap;
42423Smax.romanov@nginx.com     nxt_msg_info_t       msg_info;
43318Smax.romanov@nginx.com     nxt_req_app_link_t   *ra;
44318Smax.romanov@nginx.com 
45318Smax.romanov@nginx.com     nxt_queue_link_t     link;     /* for nxt_conn_t.requests */
46318Smax.romanov@nginx.com } nxt_req_conn_link_t;
47318Smax.romanov@nginx.com 
48318Smax.romanov@nginx.com 
49167Smax.romanov@nginx.com struct nxt_req_app_link_s {
50318Smax.romanov@nginx.com     uint32_t             stream;
51425Smax.romanov@nginx.com     nxt_atomic_t         use_count;
52167Smax.romanov@nginx.com     nxt_port_t           *app_port;
53167Smax.romanov@nginx.com     nxt_port_t           *reply_port;
54167Smax.romanov@nginx.com     nxt_app_parse_ctx_t  *ap;
55423Smax.romanov@nginx.com     nxt_msg_info_t       msg_info;
56167Smax.romanov@nginx.com     nxt_req_conn_link_t  *rc;
57167Smax.romanov@nginx.com 
58425Smax.romanov@nginx.com     nxt_queue_link_t     link_app_requests; /* for nxt_app_t.requests */
59425Smax.romanov@nginx.com     nxt_queue_link_t     link_port_pending; /* for nxt_port_t.pending_requests */
60167Smax.romanov@nginx.com 
61167Smax.romanov@nginx.com     nxt_mp_t             *mem_pool;
62167Smax.romanov@nginx.com     nxt_work_t           work;
63345Smax.romanov@nginx.com 
64345Smax.romanov@nginx.com     int                  err_code;
65345Smax.romanov@nginx.com     const char           *err_str;
66167Smax.romanov@nginx.com };
67167Smax.romanov@nginx.com 
68167Smax.romanov@nginx.com 
69198Sigor@sysoev.ru typedef struct {
70198Sigor@sysoev.ru     nxt_socket_conf_t       *socket_conf;
71198Sigor@sysoev.ru     nxt_router_temp_conf_t  *temp_conf;
72198Sigor@sysoev.ru } nxt_socket_rpc_t;
73198Sigor@sysoev.ru 
74198Sigor@sysoev.ru 
75343Smax.romanov@nginx.com static nxt_int_t nxt_router_start_worker(nxt_task_t *task, nxt_app_t *app);
76343Smax.romanov@nginx.com 
77425Smax.romanov@nginx.com nxt_inline void
78425Smax.romanov@nginx.com nxt_router_ra_inc_use(nxt_req_app_link_t *ra)
79425Smax.romanov@nginx.com {
80425Smax.romanov@nginx.com     nxt_atomic_fetch_add(&ra->use_count, 1);
81425Smax.romanov@nginx.com }
82425Smax.romanov@nginx.com 
83425Smax.romanov@nginx.com nxt_inline void
84425Smax.romanov@nginx.com nxt_router_ra_dec_use(nxt_req_app_link_t *ra)
85425Smax.romanov@nginx.com {
86425Smax.romanov@nginx.com     int  c;
87425Smax.romanov@nginx.com 
88425Smax.romanov@nginx.com     c = nxt_atomic_fetch_add(&ra->use_count, -1);
89425Smax.romanov@nginx.com 
90425Smax.romanov@nginx.com     nxt_assert(c > 1);
91425Smax.romanov@nginx.com }
92425Smax.romanov@nginx.com 
93425Smax.romanov@nginx.com static void nxt_router_ra_use(nxt_task_t *task, nxt_req_app_link_t *ra, int i);
94425Smax.romanov@nginx.com 
95139Sigor@sysoev.ru static nxt_router_temp_conf_t *nxt_router_temp_conf(nxt_task_t *task);
96198Sigor@sysoev.ru static void nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data);
97198Sigor@sysoev.ru static void nxt_router_conf_ready(nxt_task_t *task,
98139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
99139Sigor@sysoev.ru static void nxt_router_conf_error(nxt_task_t *task,
100139Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
101139Sigor@sysoev.ru static void nxt_router_conf_send(nxt_task_t *task,
102193Smax.romanov@nginx.com     nxt_router_temp_conf_t *tmcf, nxt_port_msg_type_t type);
10353Sigor@sysoev.ru 
104115Sigor@sysoev.ru static nxt_int_t nxt_router_conf_create(nxt_task_t *task,
105115Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, u_char *start, u_char *end);
106133Sigor@sysoev.ru static nxt_app_t *nxt_router_app_find(nxt_queue_t *queue, nxt_str_t *name);
107133Sigor@sysoev.ru static nxt_app_t *nxt_router_listener_application(nxt_router_temp_conf_t *tmcf,
108133Sigor@sysoev.ru     nxt_str_t *name);
109198Sigor@sysoev.ru static void nxt_router_listen_socket_rpc_create(nxt_task_t *task,
110198Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_socket_conf_t *skcf);
111198Sigor@sysoev.ru static void nxt_router_listen_socket_ready(nxt_task_t *task,
112198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
113198Sigor@sysoev.ru static void nxt_router_listen_socket_error(nxt_task_t *task,
114198Sigor@sysoev.ru     nxt_port_recv_msg_t *msg, void *data);
115359Sigor@sysoev.ru static nxt_socket_conf_t *nxt_router_socket_conf(nxt_task_t *task,
116359Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf, nxt_str_t *name);
117359Sigor@sysoev.ru static nxt_int_t nxt_router_listen_socket_find(nxt_router_temp_conf_t *tmcf,
118359Sigor@sysoev.ru     nxt_socket_conf_t *nskcf, nxt_sockaddr_t *sa);
11953Sigor@sysoev.ru 
12053Sigor@sysoev.ru static nxt_int_t nxt_router_engines_create(nxt_task_t *task,
12153Sigor@sysoev.ru     nxt_router_t *router, nxt_router_temp_conf_t *tmcf,
12253Sigor@sysoev.ru     const nxt_event_interface_t *interface);
123115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_create(nxt_router_temp_conf_t *tmcf,
124115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
125115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_update(nxt_router_temp_conf_t *tmcf,
126115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
127115Sigor@sysoev.ru static nxt_int_t nxt_router_engine_conf_delete(nxt_router_temp_conf_t *tmcf,
128115Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
129154Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_create(nxt_router_temp_conf_t *tmcf,
130154Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets,
131154Sigor@sysoev.ru     nxt_work_handler_t handler);
132313Sigor@sysoev.ru static nxt_int_t nxt_router_engine_quit(nxt_router_temp_conf_t *tmcf,
133313Sigor@sysoev.ru     nxt_router_engine_conf_t *recf);
134139Sigor@sysoev.ru static nxt_int_t nxt_router_engine_joints_delete(nxt_router_temp_conf_t *tmcf,
135139Sigor@sysoev.ru     nxt_router_engine_conf_t *recf, nxt_queue_t *sockets);
13653Sigor@sysoev.ru 
13753Sigor@sysoev.ru static nxt_int_t nxt_router_threads_create(nxt_task_t *task, nxt_runtime_t *rt,
13853Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
13953Sigor@sysoev.ru static nxt_int_t nxt_router_thread_create(nxt_task_t *task, nxt_runtime_t *rt,
14053Sigor@sysoev.ru     nxt_event_engine_t *engine);
141343Smax.romanov@nginx.com static void nxt_router_apps_sort(nxt_task_t *task, nxt_router_t *router,
142133Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
14353Sigor@sysoev.ru 
144315Sigor@sysoev.ru static void nxt_router_engines_post(nxt_router_t *router,
145315Sigor@sysoev.ru     nxt_router_temp_conf_t *tmcf);
146315Sigor@sysoev.ru static void nxt_router_engine_post(nxt_event_engine_t *engine,
147315Sigor@sysoev.ru     nxt_work_t *jobs);
14853Sigor@sysoev.ru 
14953Sigor@sysoev.ru static void nxt_router_thread_start(void *data);
15053Sigor@sysoev.ru static void nxt_router_listen_socket_create(nxt_task_t *task, void *obj,
15153Sigor@sysoev.ru     void *data);
15253Sigor@sysoev.ru static void nxt_router_listen_socket_update(nxt_task_t *task, void *obj,
15353Sigor@sysoev.ru     void *data);
15453Sigor@sysoev.ru static void nxt_router_listen_socket_delete(nxt_task_t *task, void *obj,
15553Sigor@sysoev.ru     void *data);
156313Sigor@sysoev.ru static void nxt_router_worker_thread_quit(nxt_task_t *task, void *obj,
157313Sigor@sysoev.ru     void *data);
15853Sigor@sysoev.ru static void nxt_router_listen_socket_close(nxt_task_t *task, void *obj,
15953Sigor@sysoev.ru     void *data);
16053Sigor@sysoev.ru static void nxt_router_thread_exit_handler(nxt_task_t *task, void *obj,
16153Sigor@sysoev.ru     void *data);
162359Sigor@sysoev.ru static void nxt_router_listen_socket_release(nxt_task_t *task,
163359Sigor@sysoev.ru     nxt_socket_conf_t *skcf);
16453Sigor@sysoev.ru static void nxt_router_conf_release(nxt_task_t *task,
16553Sigor@sysoev.ru     nxt_socket_conf_joint_t *joint);
16653Sigor@sysoev.ru 
167343Smax.romanov@nginx.com static void nxt_router_app_port_ready(nxt_task_t *task,
168343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
169343Smax.romanov@nginx.com static void nxt_router_app_port_error(nxt_task_t *task,
170343Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg, void *data);
171343Smax.romanov@nginx.com 
172343Smax.romanov@nginx.com static nxt_port_t * nxt_router_app_get_idle_port(nxt_app_t *app);
173343Smax.romanov@nginx.com static void nxt_router_app_port_release(nxt_task_t *task, nxt_port_t *port,
174343Smax.romanov@nginx.com     uint32_t request_failed, uint32_t got_response);
175425Smax.romanov@nginx.com static nxt_int_t nxt_router_app_port(nxt_task_t *task, nxt_req_app_link_t *ra);
176141Smax.romanov@nginx.com 
17753Sigor@sysoev.ru static void nxt_router_conn_init(nxt_task_t *task, void *obj, void *data);
17853Sigor@sysoev.ru static void nxt_router_conn_http_header_parse(nxt_task_t *task, void *obj,
17953Sigor@sysoev.ru     void *data);
180359Sigor@sysoev.ru static nxt_sockaddr_t *nxt_router_local_addr(nxt_task_t *task, nxt_conn_t *c);
181206Smax.romanov@nginx.com static void nxt_router_conn_http_body_read(nxt_task_t *task, void *obj,
182206Smax.romanov@nginx.com     void *data);
18388Smax.romanov@nginx.com static void nxt_router_process_http_request(nxt_task_t *task,
18488Smax.romanov@nginx.com     nxt_conn_t *c, nxt_app_parse_ctx_t *ap);
185425Smax.romanov@nginx.com static void nxt_router_app_prepare_request(nxt_task_t *task,
186343Smax.romanov@nginx.com     nxt_req_app_link_t *ra);
187216Sigor@sysoev.ru static nxt_int_t nxt_python_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
188216Sigor@sysoev.ru     nxt_app_wmsg_t *wmsg);
189216Sigor@sysoev.ru static nxt_int_t nxt_php_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
190216Sigor@sysoev.ru     nxt_app_wmsg_t *wmsg);
191216Sigor@sysoev.ru static nxt_int_t nxt_go_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
192216Sigor@sysoev.ru     nxt_app_wmsg_t *wmsg);
19388Smax.romanov@nginx.com static void nxt_router_conn_ready(nxt_task_t *task, void *obj, void *data);
19453Sigor@sysoev.ru static void nxt_router_conn_close(nxt_task_t *task, void *obj, void *data);
19553Sigor@sysoev.ru static void nxt_router_conn_free(nxt_task_t *task, void *obj, void *data);
19653Sigor@sysoev.ru static void nxt_router_conn_error(nxt_task_t *task, void *obj, void *data);
19753Sigor@sysoev.ru static void nxt_router_conn_timeout(nxt_task_t *task, void *obj, void *data);
198318Smax.romanov@nginx.com static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
19962Sigor@sysoev.ru static nxt_msec_t nxt_router_conn_timeout_value(nxt_conn_t *c, uintptr_t data);
20020Sigor@sysoev.ru 
201141Smax.romanov@nginx.com static void nxt_router_gen_error(nxt_task_t *task, nxt_conn_t *c, int code,
202345Smax.romanov@nginx.com     const char* str);
203141Smax.romanov@nginx.com 
204119Smax.romanov@nginx.com static nxt_router_t  *nxt_router;
20520Sigor@sysoev.ru 
206216Sigor@sysoev.ru 
207216Sigor@sysoev.ru static nxt_app_prepare_msg_t  nxt_app_prepare_msg[] = {
208216Sigor@sysoev.ru     nxt_python_prepare_msg,
209216Sigor@sysoev.ru     nxt_php_prepare_msg,
210216Sigor@sysoev.ru     nxt_go_prepare_msg,
211216Sigor@sysoev.ru };
212216Sigor@sysoev.ru 
213216Sigor@sysoev.ru 
21420Sigor@sysoev.ru nxt_int_t
215141Smax.romanov@nginx.com nxt_router_start(nxt_task_t *task, void *data)
21620Sigor@sysoev.ru {
217141Smax.romanov@nginx.com     nxt_int_t      ret;
218141Smax.romanov@nginx.com     nxt_router_t   *router;
219141Smax.romanov@nginx.com     nxt_runtime_t  *rt;
220141Smax.romanov@nginx.com 
221141Smax.romanov@nginx.com     rt = task->thread->runtime;
22253Sigor@sysoev.ru 
22388Smax.romanov@nginx.com     ret = nxt_app_http_init(task, rt);
22488Smax.romanov@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
22588Smax.romanov@nginx.com         return ret;
22688Smax.romanov@nginx.com     }
22788Smax.romanov@nginx.com 
22853Sigor@sysoev.ru     router = nxt_zalloc(sizeof(nxt_router_t));
22953Sigor@sysoev.ru     if (nxt_slow_path(router == NULL)) {
23053Sigor@sysoev.ru         return NXT_ERROR;
23153Sigor@sysoev.ru     }
23253Sigor@sysoev.ru 
23353Sigor@sysoev.ru     nxt_queue_init(&router->engines);
23453Sigor@sysoev.ru     nxt_queue_init(&router->sockets);
235133Sigor@sysoev.ru     nxt_queue_init(&router->apps);
23653Sigor@sysoev.ru 
237119Smax.romanov@nginx.com     nxt_router = router;
238119Smax.romanov@nginx.com 
239115Sigor@sysoev.ru     return NXT_OK;
240115Sigor@sysoev.ru }
241115Sigor@sysoev.ru 
242115Sigor@sysoev.ru 
243343Smax.romanov@nginx.com static void
244343Smax.romanov@nginx.com nxt_router_start_worker_handler(nxt_task_t *task, nxt_port_t *port, void *data)
245167Smax.romanov@nginx.com {
246343Smax.romanov@nginx.com     size_t         size;
247343Smax.romanov@nginx.com     uint32_t       stream;
248343Smax.romanov@nginx.com     nxt_app_t      *app;
249343Smax.romanov@nginx.com     nxt_buf_t      *b;
250343Smax.romanov@nginx.com     nxt_port_t     *main_port;
251343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
252343Smax.romanov@nginx.com 
253343Smax.romanov@nginx.com     app = data;
254167Smax.romanov@nginx.com 
255167Smax.romanov@nginx.com     rt = task->thread->runtime;
256240Sigor@sysoev.ru     main_port = rt->port_by_type[NXT_PROCESS_MAIN];
257167Smax.romanov@nginx.com 
258343Smax.romanov@nginx.com     nxt_debug(task, "app '%V' %p start worker", &app->name, app);
259343Smax.romanov@nginx.com 
260343Smax.romanov@nginx.com     size = app->name.length + 1 + app->conf.length;
261343Smax.romanov@nginx.com 
262343Smax.romanov@nginx.com     b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size);
263343Smax.romanov@nginx.com 
264343Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
265343Smax.romanov@nginx.com         goto failed;
266167Smax.romanov@nginx.com     }
267167Smax.romanov@nginx.com 
268343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->name);
269343Smax.romanov@nginx.com     *b->mem.free++ = '\0';
270343Smax.romanov@nginx.com     nxt_buf_cpystr(b, &app->conf);
271343Smax.romanov@nginx.com 
272343Smax.romanov@nginx.com     stream = nxt_port_rpc_register_handler(task, port,
273343Smax.romanov@nginx.com                                            nxt_router_app_port_ready,
274343Smax.romanov@nginx.com                                            nxt_router_app_port_error,
275343Smax.romanov@nginx.com                                            -1, app);
276343Smax.romanov@nginx.com 
277343Smax.romanov@nginx.com     if (nxt_slow_path(stream == 0)) {
278343Smax.romanov@nginx.com         nxt_mp_release(b->data, b);
279343Smax.romanov@nginx.com 
280343Smax.romanov@nginx.com         goto failed;
281343Smax.romanov@nginx.com     }
282343Smax.romanov@nginx.com 
283343Smax.romanov@nginx.com     nxt_port_socket_write(task, main_port, NXT_PORT_MSG_START_WORKER, -1,
284343Smax.romanov@nginx.com                           stream, port->id, b);
285343Smax.romanov@nginx.com 
286343Smax.romanov@nginx.com     return;
287343Smax.romanov@nginx.com 
288343Smax.romanov@nginx.com failed:
289343Smax.romanov@nginx.com 
290343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
291343Smax.romanov@nginx.com 
292343Smax.romanov@nginx.com     app->pending_workers--;
293343Smax.romanov@nginx.com 
294343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
295343Smax.romanov@nginx.com 
296343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
297167Smax.romanov@nginx.com }
298167Smax.romanov@nginx.com 
299167Smax.romanov@nginx.com 
300343Smax.romanov@nginx.com static nxt_int_t
301343Smax.romanov@nginx.com nxt_router_start_worker(nxt_task_t *task, nxt_app_t *app)
302141Smax.romanov@nginx.com {
303343Smax.romanov@nginx.com     nxt_int_t      res;
304343Smax.romanov@nginx.com     nxt_port_t     *router_port;
305343Smax.romanov@nginx.com     nxt_runtime_t  *rt;
306343Smax.romanov@nginx.com 
307343Smax.romanov@nginx.com     rt = task->thread->runtime;
308343Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
309343Smax.romanov@nginx.com 
310343Smax.romanov@nginx.com     nxt_router_app_use(task, app, 1);
311343Smax.romanov@nginx.com 
312343Smax.romanov@nginx.com     res = nxt_port_post(task, router_port, nxt_router_start_worker_handler,
313343Smax.romanov@nginx.com                         app);
314343Smax.romanov@nginx.com 
315343Smax.romanov@nginx.com     if (res == NXT_OK) {
316343Smax.romanov@nginx.com         return res;
317318Smax.romanov@nginx.com     }
318318Smax.romanov@nginx.com 
319343Smax.romanov@nginx.com     nxt_thread_mutex_lock(&app->mutex);
320343Smax.romanov@nginx.com 
321343Smax.romanov@nginx.com     app->pending_workers--;
322343Smax.romanov@nginx.com 
323343Smax.romanov@nginx.com     nxt_thread_mutex_unlock(&app->mutex);
324343Smax.romanov@nginx.com 
325343Smax.romanov@nginx.com     nxt_router_app_use(task, app, -1);
326343Smax.romanov@nginx.com 
327343Smax.romanov@nginx.com     return NXT_ERROR;
328318Smax.romanov@nginx.com }
329318Smax.romanov@nginx.com 
330318Smax.romanov@nginx.com 
331351Smax.romanov@nginx.com nxt_inline void
332351Smax.romanov@nginx.com nxt_router_ra_init(nxt_task_t *task, nxt_req_app_link_t *ra,
333351Smax.romanov@nginx.com     nxt_req_conn_link_t *rc)
334167Smax.romanov@nginx.com {
335318Smax.romanov@nginx.com     nxt_event_engine_t  *engine;
336351Smax.romanov@nginx.com 
337318Smax.romanov@nginx.com     engine = task->thread->engine;
338167Smax.romanov@nginx.com 
339167Smax.romanov@nginx.com     nxt_memzero(ra, sizeof(nxt_req_app_link_t));
340167Smax.romanov@nginx.com 
341318Smax.romanov@nginx.com     ra->stream = rc->stream;
342425Smax.romanov@nginx.com     ra->use_count = 1;
343167Smax.romanov@nginx.com     ra->rc = rc;
344318Smax.romanov@nginx.com     rc->ra = ra;
345318Smax.romanov@nginx.com     ra->reply_port = engine->port;
346351Smax.romanov@nginx.com     ra->ap = rc->ap;
347167Smax.romanov@nginx.com 
348167Smax.romanov@nginx.com     ra->work.handler = NULL;
349318Smax.romanov@nginx.com     ra->work.task = &engine->task;
350167Smax.romanov@nginx.com     ra->work.obj = ra;
351318Smax.romanov@nginx.com     ra->work.data = engine;
352351Smax.romanov@nginx.com }
353351Smax.romanov@nginx.com 
354351Smax.romanov@nginx.com 
355351Smax.romanov@nginx.com nxt_inline nxt_req_app_link_t *
356351Smax.romanov@nginx.com nxt_router_ra_create(nxt_task_t *task, nxt_req_app_link_t *ra_src)
357351Smax.romanov@nginx.com {
358351Smax.romanov@nginx.com     nxt_mp_t            *mp;
359351Smax.romanov@nginx.com     nxt_req_app_link_t  *ra;
360351Smax.romanov@nginx.com 
361425Smax.romanov@nginx.com     if (ra_src->mem_pool != NULL) {
362425Smax.romanov@nginx.com         return ra_src;
363425Smax.romanov@nginx.com     }
364425Smax.romanov@nginx.com 
365351Smax.romanov@nginx.com     mp = ra_src->ap->mem_pool;
366351Smax.romanov@nginx.com 
367351Smax.romanov@nginx.com     ra = nxt_mp_retain(mp, sizeof(nxt_req_app_link_t));
368351Smax.romanov@nginx.com 
369351Smax.romanov@nginx.com     if (nxt_slow_path(ra == NULL)) {
370351Smax.romanov@nginx.com 
371351Smax.romanov@nginx.com         ra_src->rc->ra = NULL;
372351Smax.romanov@nginx.com         ra_src->rc = NULL;
373351Smax.romanov@nginx.com 
374351Smax.romanov@nginx.com         return NULL;
375351Smax.romanov@nginx.com     }
376351Smax.romanov@nginx.com 
377351Smax.romanov@nginx.com     nxt_router_ra_init(task, ra, ra_src->rc);
378351Smax.romanov@nginx.com 
379351Smax.romanov@nginx.com     ra->mem_pool = mp;
380167Smax.romanov@nginx.com 
381167Smax.romanov@nginx.com     return ra;
382167Smax.romanov@nginx.com }
383167Smax.romanov@nginx.com 
384167Smax.romanov@nginx.com 
385423Smax.romanov@nginx.com nxt_inline nxt_bool_t
386423Smax.romanov@nginx.com nxt_router_msg_cancel(nxt_task_t *task, nxt_msg_info_t *msg_info,
387423Smax.romanov@nginx.com     uint32_t stream)
388423Smax.romanov@nginx.com {
389423Smax.romanov@nginx.com     nxt_buf_t   *b, *next;
390423Smax.romanov@nginx.com     nxt_bool_t  cancelled;
391423Smax.romanov@nginx.com 
392423Smax.romanov@nginx.com     if (msg_info->buf == NULL) {
393423Smax.romanov@nginx.com         return 0;
394423Smax.romanov@nginx.com     }
395423Smax.romanov@nginx.com 
396423Smax.romanov@nginx.com     cancelled = nxt_port_mmap_tracking_cancel(task, &msg_info->tracking,
397423Smax.romanov@nginx.com                                               stream);
398423Smax.romanov@nginx.com 
399423Smax.romanov@nginx.com     if (cancelled) {
400423Smax.romanov@nginx.com         nxt_debug(task, "stream #%uD: cancelled by router", stream);
401423Smax.romanov@nginx.com     }
402423Smax.romanov@nginx.com 
403423Smax.romanov@nginx.com     for (b = msg_info->buf; b != NULL; b = next) {
404423Smax.romanov@nginx.com         next = b->next;
405423Smax.romanov@nginx.com 
406423Smax.romanov@nginx.com         b->completion_handler = msg_info->completion_handler;
407423Smax.romanov@nginx.com 
408423Smax.romanov@nginx.com         if (b->is_port_mmap_sent) {
409423Smax.romanov@nginx.com             b->is_port_mmap_sent = cancelled == 0;
410423Smax.romanov@nginx.com             b->completion_handler(task, b, b->parent);
411423Smax.romanov@nginx.com         }
412423Smax.romanov@nginx.com     }
413423Smax.romanov@nginx.com 
414423Smax.romanov@nginx.com     msg_info->buf = NULL;
415423Smax.romanov@nginx.com 
416423Smax.romanov@nginx.com     return cancelled;
417423Smax.romanov@nginx.com }
418423Smax.romanov@nginx.com 
419423Smax.romanov@nginx.com 
420167Smax.romanov@nginx.com static void
421425Smax.romanov@nginx.com nxt_router_ra_update_peer(nxt_task_t *task, nxt_req_app_link_t *ra);
422425Smax.romanov@nginx.com 
423425Smax.romanov@nginx.com 
424425Smax.romanov@nginx.com static void
425425Smax.romanov@nginx.com nxt_router_ra_update_peer_handler(nxt_task_t *task, void *obj, void *data)
426167Smax.romanov@nginx.com {
427425Smax.romanov@nginx.com     nxt_req_app_link_t  *ra;
428425Smax.romanov@nginx.com 
429425Smax.romanov@nginx.com     ra = obj;
430425Smax.romanov@nginx.com 
431425Smax.romanov@nginx.com     nxt_router_ra_update_peer(task, ra);
432425Smax.romanov@nginx.com 
433425Smax.romanov@nginx.com     nxt_router_ra_use(task, ra, -1);
434425Smax.romanov@nginx.com }
435425Smax.romanov@nginx.com 
436425Smax.romanov@nginx.com 
437425Smax.romanov@nginx.com static void
438425Smax.romanov@nginx.com nxt_router_ra_update_peer(nxt_task_t *task, nxt_req_app_link_t *ra)
439425Smax.romanov@nginx.com {
440343Smax.romanov@nginx.com     nxt_event_engine_t   *engine;
441343Smax.romanov@nginx.com     nxt_req_conn_link_t  *rc;
442318Smax.romanov@nginx.com 
443425Smax.romanov@nginx.com     engine = ra->work.data;
444318Smax.romanov@nginx.com 
445343Smax.romanov@nginx.com     if (task->thread->engine != engine) {
446425Smax.romanov@nginx.com         nxt_router_ra_inc_use(ra);
447425Smax.romanov@nginx.com 
448425Smax.romanov@nginx.com         ra->work.handler = nxt_router_ra_update_peer_handler;
449318Smax.romanov@nginx.com         ra->work.task = &engine->task;
450318Smax.romanov@nginx.com         ra->work.next = NULL;
451318Smax.romanov@nginx.com 
452425Smax.romanov@nginx.com         nxt_debug(task, "ra stream #%uD post update peer to %p",
453318Smax.romanov@nginx.com                   ra->stream, engine);
454318Smax.romanov@nginx.com 
455318Smax.romanov@nginx.com         nxt_event_engine_post(engine, &ra->work);
456318Smax.romanov@nginx.com 
457318Smax.romanov@nginx.com         return;
458318Smax.romanov@nginx.com     }
459318Smax.romanov@nginx.com 
460425Smax.romanov@nginx.com     nxt_debug(task, "ra stream #%uD update peer", ra->stream);
461425Smax.romanov@nginx.com 
462425Smax.romanov@nginx.com     rc = ra->rc;
463425Smax.romanov@nginx.com 
464425Smax.romanov@nginx.com     if (rc != NULL && ra->app_port != NULL) {
465425Smax.romanov@nginx.com         nxt_port_rpc_ex_set_peer(task, engine->port, rc, ra->app_port->pid);
466425Smax.romanov@nginx.com     }
467425Smax.romanov@nginx.com 
468425Smax.romanov@nginx.com     nxt_router_ra_use(task, ra, -1);
469425Smax.romanov@nginx.com }
470425Smax.romanov@nginx.com 
471425Smax.romanov@nginx.com 
472425Smax.romanov@nginx.com static void
473425Smax.romanov@nginx.com nxt_router_ra_release(nxt_task_t *task, nxt_req_app_link_t *ra)
474425Smax.romanov@nginx.com {
475425Smax.romanov@nginx.com     nxt_conn_t           *c;
476425Smax.romanov@nginx.com     nxt_req_conn_link_t  *rc;
477425Smax.romanov@nginx.com 
478425Smax.romanov@nginx.com     nxt_assert(task->thread->engine == ra->work.data);
479425Smax.romanov@nginx.com     nxt_assert(ra->use_count == 0);
480425Smax.romanov@nginx.com 
481343Smax.romanov@nginx.com     nxt_debug(task, "ra stream #%uD release", ra->stream);
482343Smax.romanov@nginx.com 
483343Smax.romanov@nginx.com     rc = ra->rc;
484343Smax.romanov@nginx.com 
485343Smax.romanov@nginx.com     if (rc != NULL) {
486425Smax.romanov@nginx.com         c = rc->conn;
487343Smax.romanov@nginx.com 
488423Smax.romanov@nginx.com         if (nxt_slow_path(ra->err_code != 0)) {
489425Smax.romanov@nginx.com             nxt_router_gen_error(task, c, ra->err_code, ra->err_str);
490423Smax.romanov@nginx.com 
491423Smax.romanov@nginx.com         } else {
492423Smax.romanov@nginx.com             rc->app_port = ra->app_port;
493423Smax.romanov@nginx.com             rc->msg_info = ra->msg_info;
494423Smax.romanov@nginx.com 
495425Smax.romanov@nginx.com             if (rc->app->timeout != 0) {
496425Smax.romanov@nginx.com                 c->read_timer.handler = nxt_router_app_timeout;
497425Smax.romanov@nginx.com                 nxt_timer_add(task->thread->engine, &c->read_timer,
498425Smax.romanov@nginx.com                               rc->app->timeout);
499425Smax.romanov@nginx.com             }
500425Smax.romanov@nginx.com 
501423Smax.romanov@nginx.com             ra->app_port = NULL;
502423Smax.romanov@nginx.com             ra->msg_info.buf = NULL;
503423Smax.romanov@nginx.com         }
504343Smax.romanov@nginx.com 
505343Smax.romanov@nginx.com         rc->ra = NULL;
506343Smax.romanov@nginx.com         ra->rc = NULL;
507343Smax.romanov@nginx.com     }
508343Smax.romanov@nginx.com 
509343Smax.romanov@nginx.com     if (ra->app_port != NULL) {
510343Smax.romanov@nginx.com         nxt_router_app_port_release(task, ra->app_port, 0, 1);
511343Smax.romanov@nginx.com 
512343Smax.romanov@nginx.com         ra->app_port = NULL;
513167Smax.romanov@nginx.com     }
514167Smax.romanov@nginx.com 
515423Smax.romanov@nginx.com     nxt_router_msg_cancel(task, &ra->msg_info, ra->stream);
516423Smax.romanov@nginx.com 
517351Smax.romanov@nginx.com     if (ra->mem_pool != NULL) {
518351Smax.romanov@nginx.com         nxt_mp_release(ra->mem_pool, ra);
519351Smax.romanov@nginx.com     }
520167Smax.romanov@nginx.com }
521167Smax.romanov@nginx.com 
522167Smax.romanov@nginx.com 
523425Smax.romanov@nginx.com static void
524425Smax.romanov@nginx.com nxt_router_ra_release_handler(nxt_task_t *task, void *obj, void *data)
525425Smax.romanov@nginx.com {
526425Smax.romanov@nginx.com     nxt_req_app_link_t  *ra;
527425Smax.romanov@nginx.com 
528425Smax.romanov@nginx.com     ra = obj;
529425Smax.romanov@nginx.com 
530425Smax.romanov@nginx.com     nxt_assert(ra->work.data == data);
531425Smax.romanov@nginx.com 
532425Smax.romanov@nginx.com     nxt_atomic_fetch_add(&ra->use_count, -1);
533425Smax.romanov@nginx.com 
534425Smax.romanov@nginx.com     nxt_router_ra_release(task, ra);
535425Smax.romanov@nginx.com }
536425Smax.romanov@nginx.com 
537425Smax.romanov@nginx.com 
538425Smax.romanov@nginx.com static void
539425Smax.romanov@nginx.com nxt_router_ra_use(nxt_task_t *task, nxt_req_app_link_t *ra, int i)
540425Smax.romanov@nginx.com {
541425Smax.romanov@nginx.com     int                 c;
542425Smax.romanov@nginx.com     nxt_event_engine_t  *engine;
543425Smax.romanov@nginx.com 
544425Smax.romanov@nginx.com     c = nxt_atomic_fetch_add(&ra->use_count, i);
545425Smax.romanov@nginx.com 
546425Smax.romanov@nginx.com     if (i < 0 && c == -i) {
547425Smax.romanov@nginx.com         engine = ra->work.data;
548425Smax.romanov@nginx.com 
549425Smax.romanov@nginx.com         if (task->thread->engine == engine) {
550425Smax.romanov@nginx.com             nxt_router_ra_release(task, ra);
551425Smax.romanov@nginx.com 
552425Smax.romanov@nginx.com             return;
553425Smax.romanov@nginx.com         }
554425Smax.romanov@nginx.com 
555425Smax.romanov@nginx.com         nxt_router_ra_inc_use(ra);
556425Smax.romanov@nginx.com 
557425Smax.romanov@nginx.com         ra->work.handler = nxt_router_ra_release_handler;
558425Smax.romanov@nginx.com         ra->work.task = &engine->task;
559425Smax.romanov@nginx.com         ra->work.next = NULL;
560425Smax.romanov@nginx.com 
561425Smax.romanov@nginx.com         nxt_debug(task, "ra stream #%uD post release to %p",
562425Smax.romanov@nginx.com                   ra->stream, engine);
563425Smax.romanov@nginx.com 
564425Smax.romanov@nginx.com         nxt_event_engine_post(engine, &ra->work);
565425Smax.romanov@nginx.com     }
566425Smax.romanov@nginx.com }
567425Smax.romanov@nginx.com 
568425Smax.romanov@nginx.com 
569423Smax.romanov@nginx.com nxt_inline void
570423Smax.romanov@nginx.com nxt_router_ra_error(nxt_req_app_link_t *ra, int code, const char* str)
571345Smax.romanov@nginx.com {
572423Smax.romanov@nginx.com     ra->app_port = NULL;
573423Smax.romanov@nginx.com     ra->err_code = code;
574423Smax.romanov@nginx.com     ra->err_str = str;
575345Smax.romanov@nginx.com }
576345Smax.romanov@nginx.com 
577345Smax.romanov@nginx.com 
578425Smax.romanov@nginx.com nxt_inline nxt_bool_t
579425Smax.romanov@nginx.com nxt_queue_chk_remove(nxt_queue_link_t *lnk)
580425Smax.romanov@nginx.com {
581425Smax.romanov@nginx.com     if (lnk->next != NULL) {
582425Smax.romanov@nginx.com         nxt_queue_remove(lnk);
583425Smax.romanov@nginx.com 
584425Smax.romanov@nginx.com         lnk->next = NULL;
585425Smax.romanov@nginx.com 
586425Smax.romanov@nginx.com         return 1;
587425Smax.romanov@nginx.com     }
588425Smax.romanov@nginx.com 
589425Smax.romanov@nginx.com     return 0;
590425Smax.romanov@nginx.com }
591425Smax.romanov@nginx.com 
592425Smax.romanov@nginx.com 
593343Smax.romanov@nginx.com nxt_inline void
594343Smax.romanov@nginx.com nxt_router_rc_unlink(nxt_task_t *task, nxt_req_conn_link_t *rc)
595343Smax.romanov@nginx.com {
596425Smax.romanov@nginx.com     int                 ra_use_delta;
597343Smax.romanov@nginx.com     nxt_req_app_link_t  *ra;
598343Smax.romanov@nginx.com 
599343Smax.romanov@nginx.com     if (rc->app_port != NULL) {
600343Smax.romanov@nginx.com         nxt_router_app_port_release(task, rc->app_port, 0, 1);
601343Smax.romanov@nginx.com 
602343Smax.romanov@nginx.com         rc->app_port = NULL;
603343Smax.romanov@nginx.com     }
604343Smax.romanov@nginx.com 
605423Smax.romanov@nginx.com     nxt_router_msg_cancel(task, &rc->msg_info, rc->stream);
606423Smax.romanov@nginx.com 
607343Smax.romanov@nginx.com     ra = rc->ra;
608343Smax.romanov@nginx.com 
609343Smax.romanov@nginx.com     if (ra != NULL) {
610343Smax.romanov@nginx.com         rc->ra = NULL;
611343Smax.romanov@nginx.com         ra->rc = NULL;
612343Smax.romanov@nginx.com 
613425Smax.romanov@nginx.com         ra_use_delta = 0;
614425Smax.romanov@nginx.com 
615343Smax.romanov@nginx.com         nxt_thread_mutex_lock(&rc->app->mutex);
616343Smax.romanov@nginx.com 
617425Smax.romanov@nginx.com         if (ra->link_app_requests.next == NULL
618425Smax.romanov@nginx.com             && ra->link_port_pending.next == NULL)
619425Smax.romanov@nginx.com         {
620425Smax.romanov@nginx.com             ra = NULL;
621343Smax.romanov@nginx.com 
622343Smax.romanov@nginx.com         } else {
623425Smax.romanov@nginx.com             ra_use_delta -= nxt_queue_chk_remove(&ra->link_app_requests);
624425Smax.romanov@nginx.com             ra_use_delta -= nxt_queue_chk_remove(&ra->link_port_pending);
625343Smax.romanov@nginx.com         }
626343Smax.romanov@nginx.com 
627343Smax.romanov@nginx.com         nxt_thread_mutex_unlock(&rc->app->mutex);
628425Smax.romanov@nginx.com 
629425Smax.romanov@nginx.com         if (ra != NULL) {
630425Smax.romanov@nginx.com             nxt_router_ra_use(task, ra, ra_use_delta);
631425Smax.romanov@nginx.com         }
632343Smax.romanov@nginx.com     }
633343Smax.romanov@nginx.com 
634343Smax.romanov@nginx.com     if (rc->app != NULL) {
635343Smax.romanov@nginx.com         nxt_router_app_use(task, rc->app, -1);
636343Smax.romanov@nginx.com 
637343Smax.romanov@nginx.com         rc->app = NULL;
638343Smax.romanov@nginx.com     }
639343Smax.romanov@nginx.com 
640346Smax.romanov@nginx.com     if (rc->ap != NULL) {
641346Smax.romanov@nginx.com         nxt_app_http_req_done(task, rc->ap);
642346Smax.romanov@nginx.com 
643346Smax.romanov@nginx.com         rc->ap = NULL;
644346Smax.romanov@nginx.com     }
645346Smax.romanov@nginx.com 
646343Smax.romanov@nginx.com     nxt_queue_remove(&rc->link);
647343Smax.romanov@nginx.com 
648343Smax.romanov@nginx.com     rc->conn = NULL;
649343Smax.romanov@nginx.com }
650343Smax.romanov@nginx.com 
651343Smax.romanov@nginx.com 
652141Smax.romanov@nginx.com void
653141Smax.romanov@nginx.com nxt_router_new_port_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
654141Smax.romanov@nginx.com {
655141Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
656141Smax.romanov@nginx.com 
657192Smax.romanov@nginx.com     if (msg->port_msg.stream == 0) {
658141Smax.romanov@nginx.com         return;
659141Smax.romanov@nginx.com     }
660141Smax.romanov@nginx.com 
661*426Smax.romanov@nginx.com     if (msg->u.new_port == NULL
662*426Smax.romanov@nginx.com         || msg->u.new_port->type != NXT_PROCESS_WORKER)
663347Smax.romanov@nginx.com     {
664192Smax.romanov@nginx.com         msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
665141Smax.romanov@nginx.com     }
666192Smax.romanov@nginx.com 
667192Smax.romanov@nginx.com     nxt_port_rpc_handler(task, msg);
668141Smax.romanov@nginx.com }
669141Smax.romanov@nginx.com 
670141Smax.romanov@nginx.com 
671139Sigor@sysoev.ru void
672139Sigor@sysoev.ru nxt_router_conf_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
673115Sigor@sysoev.ru {
674198Sigor@sysoev.ru     nxt_int_t               ret;
675139Sigor@sysoev.ru     nxt_buf_t               *b;
676139Sigor@sysoev.ru     nxt_router_temp_conf_t  *tmcf;
677139Sigor@sysoev.ru 
678139Sigor@sysoev.ru     tmcf = nxt_router_temp_conf(task);
679139Sigor@sysoev.ru     if (nxt_slow_path(tmcf == NULL)) {
680139Sigor@sysoev.ru         return;
68153Sigor@sysoev.ru     }
68253Sigor@sysoev.ru 
683423Smax.romanov@nginx.com     nxt_debug(task, "nxt_router_conf_data_handler(%d): %*s",
684423Smax.romanov@nginx.com               nxt_buf_used_size(msg->buf),
685423Smax.romanov@nginx.com               nxt_buf_used_size(msg->buf), msg->buf->mem.pos);
686423Smax.romanov@nginx.com 
687352Smax.romanov@nginx.com     b = nxt_buf_chk_make_plain(tmcf->conf->mem_pool, msg->buf, msg->size);
688352Smax.romanov@nginx.com 
689352Smax.romanov@nginx.com     nxt_assert(b != NULL);
690352Smax.romanov@nginx.com 
691139Sigor@sysoev.ru     tmcf->conf->router = nxt_router;
692139Sigor@sysoev.ru     tmcf->stream = msg->port_msg.stream;
693139Sigor@sysoev.ru     tmcf->port = nxt_runtime_port_find(task->thread->runtime,
694198Sigor@sysoev.ru                                        msg->port_msg.pid,
695198Sigor@sysoev.ru                                        msg->port_msg.reply_port);
696198Sigor@sysoev.ru 
697198Sigor@sysoev.ru     ret = nxt_router_conf_create(task, tmcf, b->mem.pos, b->mem.free);
698198Sigor@sysoev.ru 
699198Sigor@sysoev.ru     if (nxt_fast_path(ret == NXT_OK)) {
700198Sigor@sysoev.ru         nxt_router_conf_apply(task, tmcf, NULL);
701198Sigor@sysoev.ru 
702198Sigor@sysoev.ru     } else {
703198Sigor@sysoev.ru         nxt_router_conf_error(task, tmcf);
704139Sigor@sysoev.ru     }
70553Sigor@sysoev.ru }
70653Sigor@sysoev.ru 
70753Sigor@sysoev.ru 
708347Smax.romanov@nginx.com static void
709347Smax.romanov@nginx.com nxt_router_worker_remove_pid(nxt_task_t *task, nxt_port_t *port, void *data)
710347Smax.romanov@nginx.com {
711347Smax.romanov@nginx.com     union {
712347Smax.romanov@nginx.com         nxt_pid_t  removed_pid;
713347Smax.romanov@nginx.com         void       *data;
714347Smax.romanov@nginx.com     } u;
715347Smax.romanov@nginx.com 
716347Smax.romanov@nginx.com     u.data = data;
717347Smax.romanov@nginx.com 
718347Smax.romanov@nginx.com     nxt_port_rpc_remove_peer(task, port, u.removed_pid);
719347Smax.romanov@nginx.com }
720347Smax.romanov@nginx.com 
721347Smax.romanov@nginx.com 
722192Smax.romanov@nginx.com void
723192Smax.romanov@nginx.com nxt_router_remove_pid_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
724192Smax.romanov@nginx.com {
725347Smax.romanov@nginx.com     nxt_event_engine_t  *engine;
726318Smax.romanov@nginx.com 
727192Smax.romanov@nginx.com     nxt_port_remove_pid_handler(task, msg);
728192Smax.romanov@nginx.com 
729192Smax.romanov@nginx.com     if (msg->port_msg.stream == 0) {
730192Smax.romanov@nginx.com         return;
731192Smax.romanov@nginx.com     }
732192Smax.romanov@nginx.com 
733318Smax.romanov@nginx.com     nxt_queue_each(engine, &nxt_router->engines, nxt_event_engine_t, link0)
734318Smax.romanov@nginx.com     {
735347Smax.romanov@nginx.com         nxt_port_post(task, engine->port, nxt_router_worker_remove_pid,
736347Smax.romanov@nginx.com                       msg->u.data);
737318Smax.romanov@nginx.com     }
738318Smax.romanov@nginx.com     nxt_queue_loop;
739318Smax.romanov@nginx.com 
740192Smax.romanov@nginx.com     msg->port_msg.type = _NXT_PORT_MSG_RPC_ERROR;
741192Smax.romanov@nginx.com 
742192Smax.romanov@nginx.com     nxt_port_rpc_handler(task, msg);
743192Smax.romanov@nginx.com }
744192Smax.romanov@nginx.com 
745192Smax.romanov@nginx.com 
74653Sigor@sysoev.ru static nxt_router_temp_conf_t *
747139Sigor@sysoev.ru nxt_router_temp_conf(nxt_task_t *task)
74853Sigor@sysoev.ru {
74965Sigor@sysoev.ru     nxt_mp_t                *mp, *tmp;
75053Sigor@sysoev.ru     nxt_router_conf_t       *rtcf;
75153Sigor@sysoev.ru     nxt_router_temp_conf_t  *tmcf;
75253Sigor@sysoev.ru 
75365Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
75453Sigor@sysoev.ru     if (nxt_slow_path(mp == NULL)) {
75553Sigor@sysoev.ru         return NULL;
75653Sigor@sysoev.ru     }
75753Sigor@sysoev.ru 
75865Sigor@sysoev.ru     rtcf = nxt_mp_zget(mp, sizeof(nxt_router_conf_t));
75953Sigor@sysoev.ru     if (nxt_slow_path(rtcf == NULL)) {
76053Sigor@sysoev.ru         goto fail;
76153Sigor@sysoev.ru     }
76253Sigor@sysoev.ru 
76353Sigor@sysoev.ru     rtcf->mem_pool = mp;
76453Sigor@sysoev.ru 
76565Sigor@sysoev.ru     tmp = nxt_mp_create(1024, 128, 256, 32);
76653Sigor@sysoev.ru     if (nxt_slow_path(tmp == NULL)) {
76753Sigor@sysoev.ru         goto fail;
76853Sigor@sysoev.ru     }
76953Sigor@sysoev.ru 
77065Sigor@sysoev.ru     tmcf = nxt_mp_zget(tmp, sizeof(nxt_router_temp_conf_t));
77153Sigor@sysoev.ru     if (nxt_slow_path(tmcf == NULL)) {
77253Sigor@sysoev.ru         goto temp_fail;
77353Sigor@sysoev.ru     }
77453Sigor@sysoev.ru 
77553Sigor@sysoev.ru     tmcf->mem_pool = tmp;
77653Sigor@sysoev.ru     tmcf->conf = rtcf;
777139Sigor@sysoev.ru     tmcf->count = 1;
778139Sigor@sysoev.ru     tmcf->engine = task->thread->engine;
77953Sigor@sysoev.ru 
78053Sigor@sysoev.ru     tmcf->engines = nxt_array_create(tmcf->mem_pool, 4,
78153Sigor@sysoev.ru                                      sizeof(nxt_router_engine_conf_t));
78253Sigor@sysoev.ru     if (nxt_slow_path(tmcf->engines == NULL)) {
78353Sigor@sysoev.ru         goto temp_fail;
78453Sigor@sysoev.ru     }
78553Sigor@sysoev.ru 
78653Sigor@sysoev.ru     nxt_queue_init(&tmcf->deleting);
78753Sigor@sysoev.ru     nxt_queue_init(&tmcf->keeping);
78853Sigor@sysoev.ru     nxt_queue_init(&tmcf->updating);
78953Sigor@sysoev.ru     nxt_queue_init(&tmcf->pending);
79053Sigor@sysoev.ru     nxt_queue_init(&tmcf->creating);
791416Smax.romanov@nginx.com 
792133Sigor@sysoev.ru     nxt_queue_init(&tmcf->apps);
793133Sigor@sysoev.ru     nxt_queue_init(&tmcf->previous);
79453Sigor@sysoev.ru 
79553Sigor@sysoev.ru     return tmcf;
79653Sigor@sysoev.ru 
79753Sigor@sysoev.ru temp_fail:
79853Sigor@sysoev.ru 
79965Sigor@sysoev.ru     nxt_mp_destroy(tmp);
80053Sigor@sysoev.ru 
80153Sigor@sysoev.ru fail:
80253Sigor@sysoev.ru 
80365Sigor@sysoev.ru     nxt_mp_destroy(mp);
80453Sigor@sysoev.ru 
80553Sigor@sysoev.ru     return NULL;
80653Sigor@sysoev.ru }
80753Sigor@sysoev.ru 
80853Sigor@sysoev.ru 
809198Sigor@sysoev.ru static void
810198Sigor@sysoev.ru nxt_router_conf_apply(nxt_task_t *task, void *obj, void *data)
811139Sigor@sysoev.ru {
812139Sigor@sysoev.ru     nxt_int_t                    ret;
813139Sigor@sysoev.ru     nxt_router_t                 *router;
814139Sigor@sysoev.ru     nxt_runtime_t                *rt;
815198Sigor@sysoev.ru     nxt_queue_link_t             *qlk;
816198Sigor@sysoev.ru     nxt_socket_conf_t            *skcf;
817198Sigor@sysoev.ru     nxt_router_temp_conf_t       *tmcf;
818139Sigor@sysoev.ru     const nxt_event_interface_t  *interface;
819139Sigor@sysoev.ru 
820198Sigor@sysoev.ru     tmcf = obj;
821198Sigor@sysoev.ru 
822198Sigor@sysoev.ru     qlk = nxt_queue_first(&tmcf->pending);
823198Sigor@sysoev.ru 
824198Sigor@sysoev.ru     if (qlk != nxt_queue_tail(&tmcf->pending)) {
825198Sigor@sysoev.ru         nxt_queue_remove(qlk);
826198Sigor@sysoev.ru         nxt_queue_insert_tail(&tmcf->creating, qlk);
827198Sigor@sysoev.ru 
828198Sigor@sysoev.ru         skcf = nxt_queue_link_data(qlk, nxt_socket_conf_t, link);
829198Sigor@sysoev.ru 
830198Sigor@sysoev.ru         nxt_router_listen_socket_rpc_create(task, tmcf, skcf);
831198Sigor@sysoev.ru 
832198Sigor@sysoev.ru         return;
833139Sigor@sysoev.ru     }
834139Sigor@sysoev.ru 
835139Sigor@sysoev.ru     rt = task->thread->runtime;
836139Sigor@sysoev.ru 
837