xref: /unit/src/nxt_conn_accept.c (revision 358:40bbd4c2349d)
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_event_engine_t  *engine;
88 
89     engine = task->thread->engine;
90 
91     if (engine->connections < engine->max_connections) {
92 
93         mp = nxt_mp_create(1024, 128, 256, 32);
94 
95         if (nxt_fast_path(mp != NULL)) {
96             c = nxt_conn_create(mp, lev->socket.task);
97             if (nxt_slow_path(c == NULL)) {
98                 goto fail;
99             }
100 
101             lev->next = c;
102             c->socket.read_work_queue = lev->socket.read_work_queue;
103             c->socket.write_ready = 1;
104             c->listen = lev;
105 
106             c->remote = nxt_sockaddr_cache_alloc(engine, lev->listen);
107             if (nxt_fast_path(c->remote != NULL)) {
108                 return c;
109             }
110         }
111 
112     fail:
113 
114         nxt_mp_destroy(mp);
115     }
116 
117     return NULL;
118 }
119 
120 
121 static void
122 nxt_conn_listen_handler(nxt_task_t *task, void *obj, void *data)
123 {
124     nxt_listen_event_t  *lev;
125 
126     lev = obj;
127     lev->ready = lev->batch;
128 
129     lev->accept(task, lev, data);
130 }
131 
132 
133 void
134 nxt_conn_io_accept(nxt_task_t *task, void *obj, void *data)
135 {
136     socklen_t           socklen;
137     nxt_conn_t          *c;
138     nxt_socket_t        s;
139     struct sockaddr     *sa;
140     nxt_listen_event_t  *lev;
141 
142     lev = obj;
143     c = lev->next;
144 
145     lev->ready--;
146     lev->socket.read_ready = (lev->ready != 0);
147 
148     sa = &c->remote->u.sockaddr;
149     socklen = c->remote->socklen;
150     /*
151      * The returned socklen is ignored here, because sockaddr_in and
152      * sockaddr_in6 socklens are not changed.  As to unspecified sockaddr_un
153      * it is 3 byte length and already prepared, because old BSDs return zero
154      * socklen and do not update the sockaddr_un at all; Linux returns 2 byte
155      * socklen and updates only the sa_family part; other systems copy 3 bytes
156      * and truncate surplus zero part.  Only bound sockaddr_un will be really
157      * truncated here.
158      */
159     s = accept(lev->socket.fd, sa, &socklen);
160 
161     if (s == -1) {
162         nxt_conn_accept_error(task, lev, "accept", nxt_socket_errno);
163         return;
164     }
165 
166     c->socket.fd = s;
167 
168 #if (NXT_LINUX)
169     /*
170      * Linux does not inherit non-blocking mode
171      * from listen socket for accept()ed socket.
172      */
173     if (nxt_slow_path(nxt_socket_nonblocking(task, s) != NXT_OK)) {
174         nxt_socket_close(task, s);
175     }
176 
177 #endif
178 
179     nxt_debug(task, "accept(%d): %d", lev->socket.fd, s);
180 
181     nxt_conn_accept(task, lev, c);
182 }
183 
184 
185 void
186 nxt_conn_accept(nxt_task_t *task, nxt_listen_event_t *lev, nxt_conn_t *c)
187 {
188     nxt_conn_t  *next;
189 
190     nxt_sockaddr_text(c->remote);
191 
192     nxt_debug(task, "client: %*s",
193               c->remote->address_length, nxt_sockaddr_address(c->remote));
194 
195     nxt_queue_insert_head(&task->thread->engine->idle_connections, &c->link);
196 
197     c->read_work_queue = lev->work_queue;
198     c->write_work_queue = lev->work_queue;
199 
200     if (lev->listen->read_after_accept) {
201 
202         //c->socket.read_ready = 1;
203 //        lev->listen->handler(task, c, lev->socket.data);
204         nxt_work_queue_add(c->read_work_queue, lev->listen->handler,
205                            &c->task, c, lev->socket.data);
206 
207     } else {
208         nxt_work_queue_add(c->write_work_queue, lev->listen->handler,
209                            &c->task, c, lev->socket.data);
210     }
211 
212     next = nxt_conn_accept_next(task, lev);
213 
214     if (next != NULL && lev->socket.read_ready) {
215         nxt_work_queue_add(lev->socket.read_work_queue,
216                            lev->accept, task, lev, next);
217     }
218 }
219 
220 
221 static nxt_conn_t *
222 nxt_conn_accept_next(nxt_task_t *task, nxt_listen_event_t *lev)
223 {
224     nxt_conn_t  *c;
225 
226     lev->next = NULL;
227 
228     do {
229         c = nxt_conn_accept_alloc(task, lev);
230 
231         if (nxt_fast_path(c != NULL)) {
232             return c;
233         }
234 
235     } while (nxt_conn_accept_close_idle(task, lev) == NXT_OK);
236 
237     nxt_log(task, NXT_LOG_CRIT, "no available connections, "
238                   "new connections are not accepted within 1s");
239 
240     return NULL;
241 }
242 
243 
244 static nxt_int_t
245 nxt_conn_accept_close_idle(nxt_task_t *task, nxt_listen_event_t *lev)
246 {
247     nxt_conn_t          *c;
248     nxt_queue_t         *idle;
249     nxt_queue_link_t    *link;
250     nxt_event_engine_t  *engine;
251 
252     static nxt_log_moderation_t  nxt_idle_close_log_moderation = {
253         NXT_LOG_INFO, 2, "idle connections closed", NXT_LOG_MODERATION
254     };
255 
256     engine = task->thread->engine;
257 
258     idle = &engine->idle_connections;
259 
260     for (link = nxt_queue_last(idle);
261          link != nxt_queue_head(idle);
262          link = nxt_queue_next(link))
263     {
264         c = nxt_queue_link_data(link, nxt_conn_t, link);
265 
266         if (!c->socket.read_ready) {
267             nxt_log_moderate(&nxt_idle_close_log_moderation, NXT_LOG_INFO,
268                              task->log, "no available connections, "
269                              "close idle connection");
270             nxt_queue_remove(link);
271             nxt_conn_close(engine, c);
272 
273             return NXT_OK;
274         }
275     }
276 
277     nxt_timer_add(engine, &lev->timer, 1000);
278 
279     nxt_fd_event_disable_read(engine, &lev->socket);
280 
281     return NXT_DECLINED;
282 }
283 
284 
285 void
286 nxt_conn_accept_error(nxt_task_t *task, nxt_listen_event_t *lev,
287     const char *accept_syscall, nxt_err_t err)
288 {
289     static nxt_log_moderation_t  nxt_accept_log_moderation = {
290         NXT_LOG_INFO, 2, "accept() failed", NXT_LOG_MODERATION
291     };
292 
293     lev->socket.read_ready = 0;
294 
295     switch (err) {
296 
297     case NXT_EAGAIN:
298         nxt_debug(task, "%s(%d) %E", accept_syscall, lev->socket.fd, err);
299         return;
300 
301     case ECONNABORTED:
302         nxt_log_moderate(&nxt_accept_log_moderation, NXT_LOG_WARN,
303                          task->log, "%s(%d) failed %E",
304                          accept_syscall, lev->socket.fd, err);
305         return;
306 
307     case EMFILE:
308     case ENFILE:
309     case ENOBUFS:
310     case ENOMEM:
311         if (nxt_conn_accept_close_idle(task, lev) != NXT_OK) {
312             nxt_log(task, NXT_LOG_CRIT, "%s(%d) failed %E, "
313                     "new connections are not accepted within 1s",
314                     accept_syscall, lev->socket.fd, err);
315         }
316 
317         return;
318 
319     default:
320         nxt_log(task, NXT_LOG_CRIT, "%s(%d) failed %E",
321                 accept_syscall, lev->socket.fd, err);
322         return;
323     }
324 }
325 
326 
327 static void
328 nxt_conn_listen_timer_handler(nxt_task_t *task, void *obj, void *data)
329 {
330     nxt_conn_t          *c;
331     nxt_timer_t         *timer;
332     nxt_listen_event_t  *lev;
333 
334     timer = obj;
335 
336     lev = nxt_timer_data(timer, nxt_listen_event_t, timer);
337     c = lev->next;
338 
339     if (c == NULL) {
340         c = nxt_conn_accept_next(task, lev);
341 
342         if (c == NULL) {
343             return;
344         }
345     }
346 
347     nxt_fd_event_enable_accept(task->thread->engine, &lev->socket);
348 
349     lev->accept(task, lev, c);
350 }
351 
352 
353 static void
354 nxt_conn_listen_event_error(nxt_task_t *task, void *obj, void *data)
355 {
356     nxt_fd_event_t  *ev;
357 
358     ev = obj;
359 
360     nxt_log(task, NXT_LOG_CRIT, "accept(%d) event error", ev->fd);
361 }
362