xref: /unit/src/nxt_conn_accept.c (revision 312:c156aea91063)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 
9 
10 /*
11  * A listen socket handler calls an event facility specific io_accept()
12  * method.  The method accept()s a new connection and then calls
13  * nxt_event_conn_accept() to handle the new connection and to prepare
14  * for a next connection to avoid just dropping next accept()ed socket
15  * if no more connections allowed.  If there are no available connections
16  * an idle connection would be closed.  If there are no idle connections
17  * then new connections will not be accept()ed for 1 second.
18  */
19 
20 
21 static nxt_conn_t *nxt_conn_accept_alloc(nxt_task_t *task,
22     nxt_listen_event_t *lev);
23 static void nxt_conn_listen_handler(nxt_task_t *task, void *obj,
24     void *data);
25 static nxt_conn_t *nxt_conn_accept_next(nxt_task_t *task,
26     nxt_listen_event_t *lev);
27 static nxt_int_t nxt_conn_accept_close_idle(nxt_task_t *task,
28     nxt_listen_event_t *lev);
29 static void nxt_conn_listen_event_error(nxt_task_t *task, void *obj,
30     void *data);
31 static void nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj,
32     void *data);
33 
34 
35 nxt_listen_event_t *
36 nxt_listen_event(nxt_task_t *task, nxt_listen_socket_t *ls)
37 {
38     nxt_listen_event_t  *lev;
39     nxt_event_engine_t  *engine;
40 
41     lev = nxt_zalloc(sizeof(nxt_listen_event_t));
42 
43     if (nxt_fast_path(lev != NULL)) {
44         lev->socket.fd = ls->socket;
45 
46         engine = task->thread->engine;
47         lev->batch = engine->batch;
48 
49         lev->socket.read_work_queue = &engine->accept_work_queue;
50         lev->socket.read_handler = nxt_conn_listen_handler;
51         lev->socket.error_handler = nxt_conn_listen_event_error;
52         lev->socket.log = &nxt_main_log;
53 
54         lev->accept = engine->event.io->accept;
55 
56         lev->listen = ls;
57         lev->work_queue = &engine->read_work_queue;
58 
59         lev->timer.work_queue = &engine->fast_work_queue;
60         lev->timer.handler = nxt_conn_listen_timer_handler;
61         lev->timer.log = &nxt_main_log;
62 
63         lev->task.thread = task->thread;
64         lev->task.log = &nxt_main_log;
65         lev->task.ident = nxt_task_next_ident();
66         lev->socket.task = &lev->task;
67         lev->timer.task = &lev->task;
68 
69         if (nxt_conn_accept_alloc(task, lev) != NULL) {
70             nxt_fd_event_enable_accept(engine, &lev->socket);
71 
72             nxt_queue_insert_tail(&engine->listen_connections, &lev->link);
73         }
74 
75         return lev;
76     }
77 
78     return NULL;
79 }
80 
81 
82 static nxt_conn_t *
83 nxt_conn_accept_alloc(nxt_task_t *task, nxt_listen_event_t *lev)
84 {
85     nxt_mp_t             *mp;
86     nxt_conn_t           *c;
87     nxt_sockaddr_t       *sa, *remote;
88     nxt_event_engine_t   *engine;
89     nxt_listen_socket_t  *ls;
90 
91     engine = task->thread->engine;
92 
93     if (engine->connections < engine->max_connections) {
94 
95         mp = nxt_mp_create(1024, 128, 256, 32);
96 
97         if (nxt_fast_path(mp != NULL)) {
98             c = nxt_conn_create(mp, lev->socket.task);
99             if (nxt_slow_path(c == NULL)) {
100                 goto fail;
101             }
102 
103             lev->next = c;
104             c->socket.read_work_queue = lev->socket.read_work_queue;
105             c->socket.write_ready = 1;
106             c->listen = lev;
107 
108             ls = lev->listen;
109 
110             remote = nxt_sockaddr_alloc(mp, ls->socklen, ls->address_length);
111             if (nxt_slow_path(remote == NULL)) {
112                 goto fail;
113             }
114 
115             c->remote = remote;
116 
117             sa = ls->sockaddr;
118             remote->type = sa->type;
119             /*
120              * Set address family for unspecified Unix domain socket,
121              * because these sockaddr's are not updated by old BSD systems,
122              * see comment in nxt_conn_io_accept().
123              */
124             remote->u.sockaddr.sa_family = sa->u.sockaddr.sa_family;
125 
126             return c;
127         }
128 
129     fail:
130 
131         nxt_mp_destroy(mp);
132     }
133 
134     return NULL;
135 }
136 
137 
138 static void
139 nxt_conn_listen_handler(nxt_task_t *task, void *obj, void *data)
140 {
141     nxt_listen_event_t  *lev;
142 
143     lev = obj;
144     lev->ready = lev->batch;
145 
146     lev->accept(task, lev, data);
147 }
148 
149 
150 void
151 nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data)
152 {
153     socklen_t           socklen;
154     nxt_conn_t          *c;
155     nxt_socket_t        s;
156     struct sockaddr     *sa;
157     nxt_listen_event_t  *lev;
158 
159     lev = obj;
160     c = lev->next;
161 
162     lev->ready--;
163     lev->socket.read_ready = (lev->ready != 0);
164 
165     sa = &c->remote->u.sockaddr;
166     socklen = c->remote->socklen;
167     /*
168      * The returned socklen is ignored here, because sockaddr_in and
169      * sockaddr_in6 socklens are not changed.  As to unspecified sockaddr_un
170      * it is 3 byte length and already prepared, because old BSDs return zero
171      * socklen and do not update the sockaddr_un at all; Linux returns 2 byte
172      * socklen and updates only the sa_family part; other systems copy 3 bytes
173      * and truncate surplus zero part.  Only bound sockaddr_un will be really
174      * truncated here.
175      */
176     s = accept(lev->socket.fd, sa, &socklen);
177 
178     if (s == -1) {
179         nxt_conn_accept_error(task, lev, "accept", nxt_socket_errno);
180         return;
181     }
182 
183     c->socket.fd = s;
184 
185 #if (NXT_LINUX)
186     /*
187      * Linux does not inherit non-blocking mode
188      * from listen socket for accept()ed socket.
189      */
190     if (nxt_slow_path(nxt_socket_nonblocking(task, s) != NXT_OK)) {
191         nxt_socket_close(task, s);
192     }
193 
194 #endif
195 
196     nxt_debug(task, "accept(%d): %d", lev->socket.fd, s);
197 
198     nxt_conn_accept(task, lev, c);
199 }
200 
201 
202 void
203 nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c)
204 {
205     nxt_conn_t  *next;
206 
207     nxt_sockaddr_text(c->remote);
208 
209     nxt_debug(task, "client: %*s",
210               c->remote->address_length, nxt_sockaddr_address(c->remote));
211 
212     nxt_queue_insert_head(&task->thread->engine->idle_connections, &c->link);
213 
214     c->read_work_queue = lev->work_queue;
215     c->write_work_queue = lev->work_queue;
216 
217     if (lev->listen->read_after_accept) {
218 
219         //c->socket.read_ready = 1;
220 //        lev->listen->handler(task, c, lev->socket.data);
221         nxt_work_queue_add(c->read_work_queue, lev->listen->handler,
222                            &c->task, c, lev->socket.data);
223 
224     } else {
225         nxt_work_queue_add(c->write_work_queue, lev->listen->handler,
226                            &c->task, c, lev->socket.data);
227     }
228 
229     next = nxt_conn_accept_next(task, lev);
230 
231     if (next != NULL && lev->socket.read_ready) {
232         nxt_work_queue_add(lev->socket.read_work_queue,
233                            lev->accept, task, lev, next);
234     }
235 }
236 
237 
238 static nxt_conn_t *
239 nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev)
240 {
241     nxt_conn_t  *c;
242 
243     lev->next = NULL;
244 
245     do {
246         c = nxt_conn_accept_alloc(task, lev);
247 
248         if (nxt_fast_path(c != NULL)) {
249             return c;
250         }
251 
252     } while (nxt_conn_accept_close_idle(task, lev) == NXT_OK);
253 
254     nxt_log(task, NXT_LOG_CRIT, "no available connections, "
255                   "new connections are not accepted within 1s");
256 
257     return NULL;
258 }
259 
260 
261 static nxt_int_t
262 nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev)
263 {
264     nxt_conn_t          *c;
265     nxt_queue_t         *idle;
266     nxt_queue_link_t    *link;
267     nxt_event_engine_t  *engine;
268 
269     static nxt_log_moderation_t  nxt_idle_close_log_moderation = {
270         NXT_LOG_INFO, 2, "idle connections closed", NXT_LOG_MODERATION
271     };
272 
273     engine = task->thread->engine;
274 
275     idle = &engine->idle_connections;
276 
277     for (link = nxt_queue_last(idle);
278          link != nxt_queue_head(idle);
279          link = nxt_queue_next(link))
280     {
281         c = nxt_queue_link_data(link, nxt_conn_t, link);
282 
283         if (!c->socket.read_ready) {
284             nxt_log_moderate(&nxt_idle_close_log_moderation, NXT_LOG_INFO,
285                              task->log, "no available connections, "
286                              "close idle connection");
287             nxt_queue_remove(link);
288             nxt_conn_close(engine, c);
289 
290             return NXT_OK;
291         }
292     }
293 
294     nxt_timer_add(engine, &lev->timer, 1000);
295 
296     nxt_fd_event_disable_read(engine, &lev->socket);
297 
298     return NXT_DECLINED;
299 }
300 
301 
302 void
303 nxt_conn_accept_error(nxt_task_t *task, nxt_listen_event_t *lev,
304     const char *accept_syscall, nxt_err_t err)
305 {
306     static nxt_log_moderation_t  nxt_accept_log_moderation = {
307         NXT_LOG_INFO, 2, "accept() failed", NXT_LOG_MODERATION
308     };
309 
310     lev->socket.read_ready = 0;
311 
312     switch (err) {
313 
314     case NXT_EAGAIN:
315         nxt_debug(task, "%s(%d) %E", accept_syscall, lev->socket.fd, err);
316         return;
317 
318     case ECONNABORTED:
319         nxt_log_moderate(&nxt_accept_log_moderation, NXT_LOG_WARN,
320                          task->log, "%s(%d) failed %E",
321                          accept_syscall, lev->socket.fd, err);
322         return;
323 
324     case EMFILE:
325     case ENFILE:
326     case ENOBUFS:
327     case ENOMEM:
328         if (nxt_conn_accept_close_idle(task, lev) != NXT_OK) {
329             nxt_log(task, NXT_LOG_CRIT, "%s(%d) failed %E, "
330                     "new connections are not accepted within 1s",
331                     accept_syscall, lev->socket.fd, err);
332         }
333 
334         return;
335 
336     default:
337         nxt_log(task, NXT_LOG_CRIT, "%s(%d) failed %E",
338                 accept_syscall, lev->socket.fd, err);
339         return;
340     }
341 }
342 
343 
344 static void
345 nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data)
346 {
347     nxt_conn_t          *c;
348     nxt_timer_t         *timer;
349     nxt_listen_event_t  *lev;
350 
351     timer = obj;
352 
353     lev = nxt_timer_data(timer, nxt_listen_event_t, timer);
354     c = lev->next;
355 
356     if (c == NULL) {
357         c = nxt_conn_accept_next(task, lev);
358 
359         if (c == NULL) {
360             return;
361         }
362     }
363 
364     nxt_fd_event_enable_accept(task->thread->engine, &lev->socket);
365 
366     lev->accept(task, lev, c);
367 }
368 
369 
370 static void
371 nxt_conn_listen_event_error(nxt_task_t *task, void *obj, void *data)
372 {
373     nxt_fd_event_t  *ev;
374 
375     ev = obj;
376 
377     nxt_log(task, NXT_LOG_CRIT, "accept(%d) event error", ev->fd);
378 }
379