The event module of Nginx source code - Nginx connection

Nginx event processing model is the core of nginx event processing, which fully embodies the principle of the simplest business and the concept of segmented processing architecture.
As a Web server, every user request of Nignx needs at least one read event and one write event for at least one TCP connection (the embodiment of segmentation principle, even simple and clear requirements will be processed in segments). Here we must understand the definition of Nginx event and connection. The basic data structure of event definition is NGX ﹣ event ﹣ T, and the basic data structure of connection definition is NGX ﹣ connect ﹣ t.
1. Event definition
There are two basic events and AIO events in Nginx event definition. The core of each event is handler callback method, which will be implemented by the event consumer, and it determines the final handling mode of the event. There are many types of Nginx event driven models, so many fields in the definition of NGX event are also targeted. They need to be in a specific event driven mode to make sense. We only need to master the most basic about TCP processing, while others need to master the specific event driven mode slowly.

/*Basic event definition*/
struct ngx_event_s {
    void            *data;/*Pointer to data*/

    unsigned         write:1;/*Events can be written, that is, TCP connection (event processing can be understood as TCP request processing) status can send network messages*/

    unsigned         accept:1;/*Whether new connections can be accepted is directly controlled by the worker process in the core framework*/

    /* Whether overdue events need to be handled in the event driver module is understood in combination with the previous NGX ﹣ posted ﹣ accept ﹣ events*/
    unsigned         instance:1;

    /*
     * the event was passed or would be passed to a kernel;
     * in aio mode - operation was posted.
     */
    unsigned         active:1;
    /*Event disable is valid in kqueue or rtsig, not in epoll events*/
    unsigned         disabled:1;

    /* AIO Processing mode, events are in a processable state*/
    unsigned         ready:1;

    unsigned         oneshot:1;/*Marked with request event handling in AIO mode*/

    /* aio operation is complete */
    unsigned         complete:1;/*Event processing completed in AIO mode*/

    unsigned         eof:1;
    unsigned         error:1;

    unsigned         timedout:1;
    unsigned         timer_set:1;

    unsigned         delayed:1;

    unsigned         deferred_accept:1;

    /* the pending eof reported by kqueue, epoll or in aio chain operation */
    unsigned         pending_eof:1;

    unsigned         posted:1;

    unsigned         closed:1;

    /* worker to test on worker exit */
    unsigned         channel:1;
    unsigned         resolver:1;

    unsigned         cancelable:1;

#if (NGX_HAVE_KQUEUE)
    unsigned         kq_vnode:1;

    /* the pending errno reported by kqueue */
    int              kq_errno;
#endif

    /*
     * kqueue only:
     *   accept:     number of sockets that wait to be accepted
     *   read:       bytes to read when event is ready
     *               or lowat when event is set with NGX_LOWAT_EVENT flag
     *   write:      available space in buffer when event is ready
     *               or lowat when event is set with NGX_LOWAT_EVENT flag
     *
     * iocp: TODO
     *
     * otherwise:
     *   accept:     1 if accept many, 0 otherwise
     *   read:       bytes to read when event is ready, -1 if not known
     */

    int              available;

    ngx_event_handler_pt  handler;


#if (NGX_HAVE_IOCP)
    ngx_event_ovlp_t ovlp;
#endif

    ngx_uint_t       index;

    ngx_log_t       *log;

    ngx_rbtree_node_t   timer; /*Timeout processing mechanism, using red black tree management*/

    /* the posted queue */
    ngx_queue_t      queue;/* event Event processing queue */

#if 0

    /* the threads support */

    /*
     * the event thread context, we store it here
     * if $(CC) does not understand __thread declaration
     * and pthread_getspecific() is too costly
     */

    void            *thr_ctx;

#if (NGX_EVENT_T_PADDING)

    /* event should not cross cache line in SMP */

    uint32_t         padding[NGX_EVENT_T_PADDING];
#endif
#endif
};

2. Connection definition
The connection of Nginx is divided into active connection and passive connection. Active connection refers to the connection established by the Nginx server to the client or other servers. Passive connection refers to the connection sent by the client to the Nginx server. Corresponding to NGX ﹐ connection ﹐ and NGX ﹐ peer ﹐ connection ﹐ respectively, NGX ﹐ connection ﹐ was introduced in the previous chapters. Here, it mainly introduces NGX ﹐ peer ﹐ connection ﹐ s own definition, which mainly defines the remote server information and server management information. See the note for details.

struct ngx_peer_connection_s {
    ngx_connection_t                *connection;/*TCP Basic information of connection*/

    struct sockaddr                 *sockaddr;/*Remote server address*/
    socklen_t                        socklen;/*Remote server address length*/
    ngx_str_t                       *name;/*Remote server name*/

    ngx_uint_t                       tries;/*Number of retries after connection failure*/
    ngx_msec_t                       start_time;

    ngx_event_get_peer_pt            get;/**/
    ngx_event_free_peer_pt           free;
    ngx_event_notify_peer_pt         notify;
    void                            *data;

#if (NGX_SSL || NGX_COMPAT)
    ngx_event_set_peer_session_pt    set_session;
    ngx_event_save_peer_session_pt   save_session;
#endif

    ngx_addr_t                      *local;

    int                              type;
    int                              rcvbuf;

    ngx_log_t                       *log;

    unsigned                         cached:1;
    unsigned                         transparent:1;
    unsigned                         so_keepalive:1;
    unsigned                         down:1;

                                     /* ngx_connection_log_error_e */
    unsigned                         log_error:2;

    NGX_COMPAT_BEGIN(2)
    NGX_COMPAT_END
};

 

Published 18 original articles, won praise 1, visited 1951
Private letter follow

Tags: Nginx Web Server Nignx network

Posted on Thu, 16 Jan 2020 03:11:53 -0800 by doogles