Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement an event module in nginx

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

< cycle->

Connection_n; [I]. Read = 1; / / the default read event of the initial state is the closed state Rev. instance = 1. / / initially initialize the ngx_event_t array with an instance of 1} / / apply for the specified number of ngx_event_t arrays with the same length as the connections array, / / so that you can correspond the connections array to the write_events array cycle- > write_events = ngx_alloc (sizeof (ngx_event_t) * cycle- > connection_n, cycle- > log); if (cycle- > write_events = = NULL) {return NGX_ERROR;} wev = cycle- > write_events For (I = 0; I)

< cycle->

Connection_n; iTunes +) {wev [I]. Write = 1; / / initial write events also default to the closed state} I = cycle- > connection_n; next = NULL; do {Imuri; / / assign the elements of the read_events and write_events arrays to the read and write attributes of the connections array elements in turn, / / and assemble the connections array into a single linked list c [I]. Data = next C [I] .read = & cycle- > read_ events [I]; c [I] .write = & cycle- > write_ events [I]; c [I] .fd = (ngx_socket_t)-1; next = & c [I];} while (I); / / in the initial state, all connections are not used, so they need to be stored in the free_connections linked list cycle- > free_connections = next; cycle- > free_connection_n = cycle- > connection_n / * for each listening socket * / ls = cycle- > listening.elts; for (I = 0; I

< cycle->

Listening.nelts; iTunes +) {# if (NGX_HAVE_REUSEPORT) if (LS [I] .reuseport & & LS [I] .worker! = ngx_worker) {continue;} # endif / / here binds a ngx_connection_t structure c = ngx_get_connection (ls [I] .fd, cycle- > log) for each port currently being monitored; if (c = = NULL) {return NGX_ERROR } c-> type = ls [I] .type; c-> log = & ls [I] .log; c-> listening = & ls [I] .connection = c; rev = c-> read; rev- > log = c-> log; / / tag accept is 1, indicating that the connection event rev- > accept = 1 of the client can be received # if (NGX_HAVE_DEFERRED_ACCEPT) rev- > deferred_accept = ls[ I] .duration _ accept;#endif if (! (ngx_event_flags & NGX_USE_IOCP_EVENT)) {if (ls[ I] .deletion) {/ * delete the old accept events that were bound to * the old cycle read events array * / / Delete the old event old = ls[ I] .duration-> connection If (ngx_del_event (old- > read, NGX_READ_EVENT, NGX_CLOSE_EVENT) = = NGX_ERROR) {return NGX_ERROR;} old- > fd = (ngx_socket_t)-1;} # if (NGX_WIN32) if (ngx_event_flags & NGX_USE_IOCP_EVENT) {ngx_iocp_conf_t * iocpcf; rev- > handler = ngx_event_acceptex If (ngx_use_accept_mutex) {continue;} if (ngx_add_event (rev, 0, NGX_IOCP_ACCEPT) = = NGX_ERROR) {return NGX_ERROR;} ls [I] .log.handler = ngx_acceptex_log_error; iocpcf = ngx_event_get_conf (cycle- > conf_ctx, ngx_iocp_module) If (ngx_event_post_acceptex (& LS [I], iocpcf- > post_acceptex) = = NGX_ERROR) {return NGX_ERROR;}} else {rev- > handler = ngx_event_accept; if (ngx_use_accept_mutex) {continue } if (ngx_add_event (rev, NGX_READ_EVENT, 0) = = NGX_ERROR) {return NGX_ERROR;}} # else / / SOCK_STREAM means TCP, usually TCP, that is, after receiving the accept event from the client, / / the ngx_event_accept () method is called to handle the event rev- > handler = (c-> type = = SOCK_STREAM)? Ngx_event_accept: ngx_event_recvmsg;#if (NGX_HAVE_REUSEPORT) / / add the current event to the event listening queue if (LS [I] .reuseport) {if (ngx_add_event (rev, NGX_READ_EVENT, 0) = = NGX_ERROR) {return NGX_ERROR;} continue } # endif if (ngx_use_accept_mutex) {continue;} # if (NGX_HAVE_EPOLLEXCLUSIVE) if ((ngx_event_flags & NGX_USE_EPOLL_EVENT) & & ccf- > worker_processes > 1) {if (ngx_add_event (rev, NGX_READ_EVENT, NGX_EXCLUSIVE_EVENT) = = NGX_ERROR) {return NGX_ERROR } continue;} # endif / / add the current event to the event listening queue if (ngx_add_event (rev, NGX_READ_EVENT, 0) = = NGX_ERROR) {return NGX_ERROR;} # endif} return NGX_OK;}

Here, the ngx_event_process_init () method mainly completes the following tasks:

Configure whether to use the field ngx_use_accept_mutex of the shared lock based on the process mode used and the number of worker processes

Initialize ngx_posted_accept_events queues and ngx_posted_events queues for handling events

Call the init () method of the currently used event model module, for example, the epoll model will build an epoll handle in the init () method so that events to be listened to can be added later.

Determine whether the ngx_timer_resolution attribute is configured, that is, the execution frequency field of the scheduled task to update the nginx cache time obtained in the previous section, and if so, create a scheduled task to set the value of the ngx_event_timer_alarm attribute on a regular basis

Create connections, read_events, and write_events arrays of the same length, and point the read attribute of each ngx_connection_t structure in the connections array to the read event structure at the corresponding position in the read_events array, point the write attribute to the write event structure at the corresponding position in the write_events array, and organize all connections into a single linked list and store them in the free_connections attribute of the ngx_cycle_t

Configure a ngx_connection_t structure for each port that the current nginx listens on, and add corresponding event listeners to it to wait for the client connection to arrive.

After reading the above, have you mastered how to implement an event module in nginx? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report