summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_conn_close.c
blob: 7c13030908cc94a658454eb7aaaeb1876135525f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


static void nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_close_timer_handler(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_close_error_ignore(nxt_task_t *task, void *obj,
    void *data);


void
nxt_conn_close(nxt_event_engine_t *engine, nxt_conn_t *c)
{
    int                 ret;
    nxt_work_queue_t    *wq;
    nxt_work_handler_t  handler;

    static const struct linger  linger_off = {
        .l_onoff = 1,
        .l_linger = 0,
    };

    nxt_debug(c->socket.task, "conn close fd:%d, to:%d",
              c->socket.fd, c->socket.timedout);

    /*
     * Disable all pending write operations because on success they
     * will incorrectly call a ready handler set for nxt_conn_close().
     */
    c->write = NULL;

    if (c->socket.timedout) {
        /*
         * Resetting of timed out connection on close
         * releases kernel memory associated with socket.
         * This also causes sending TCP/IP RST to a peer.
         */
        ret = setsockopt(c->socket.fd, SOL_SOCKET, SO_LINGER, &linger_off,
                         sizeof(struct linger));

        if (nxt_slow_path(ret != 0)) {
            nxt_alert(c->socket.task, "setsockopt(%d, SO_LINGER) failed %E",
                      c->socket.fd, nxt_socket_errno);
        }
    }

    /*
     * Event errors should be ignored here to avoid repeated nxt_conn_close()
     * calls.  nxt_conn_close_handler() or nxt_conn_close_timer_handler()
     * will eventually close socket.
     */
    c->socket.error_handler = nxt_conn_close_error_ignore;

    if (c->socket.error == 0 && !c->socket.closed && !c->socket.shutdown) {
        wq = &engine->shutdown_work_queue;
        handler = nxt_conn_shutdown_handler;

    } else {
        wq = &engine->close_work_queue;
        handler = nxt_conn_close_handler;
    }

    nxt_work_queue_add(wq, handler, c->socket.task, c, engine);
}


static void
nxt_conn_shutdown_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t          *c;
    nxt_event_engine_t  *engine;

    c = obj;
    engine = data;

    nxt_debug(task, "conn shutdown handler fd:%d", c->socket.fd);

    c->socket.shutdown = 1;

    nxt_socket_shutdown(task, c->socket.fd, SHUT_RDWR);

    nxt_work_queue_add(&engine->close_work_queue, nxt_conn_close_handler,
                       task, c, engine);
}


static void
nxt_conn_close_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_uint_t          events_pending, timers_pending;
    nxt_conn_t          *c;
    nxt_event_engine_t  *engine;

    c = obj;
    engine = data;

    nxt_debug(task, "conn close handler fd:%d", c->socket.fd);

    /*
     * Socket should be closed only after all pending socket event operations
     * will be processed by the kernel.  This could be achieved with zero-timer
     * handler.  Pending timer operations associated with the socket are
     * processed before going to the kernel.
     */

    timers_pending = nxt_timer_delete(engine, &c->read_timer);
    timers_pending += nxt_timer_delete(engine, &c->write_timer);

    events_pending = nxt_fd_event_close(engine, &c->socket);

    if (events_pending == 0) {
        nxt_socket_close(task, c->socket.fd);
        c->socket.fd = -1;

        if (timers_pending == 0) {
            nxt_work_queue_add(&engine->fast_work_queue,
                               c->write_state->ready_handler,
                               task, c, c->socket.data);
            return;
        }
    }

    c->write_timer.handler = nxt_conn_close_timer_handler;
    c->write_timer.work_queue = &engine->fast_work_queue;

    nxt_timer_add(engine, &c->write_timer, 0);
}


static void
nxt_conn_close_timer_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t   *c;
    nxt_timer_t  *timer;

    timer = obj;

    c = nxt_write_timer_conn(timer);

    nxt_debug(task, "conn close timer handler fd:%d", c->socket.fd);

    if (c->socket.fd != -1) {
        nxt_socket_close(task, c->socket.fd);
        c->socket.fd = -1;
    }

    nxt_work_queue_add(&task->thread->engine->fast_work_queue,
                       c->write_state->ready_handler,
                       task, c, c->socket.data);
}


static void
nxt_conn_close_error_ignore(nxt_task_t *task, void *obj, void *data)
{
    nxt_debug(task, "conn close error ignore");
}