-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx_send_minimum_rate.patch
316 lines (289 loc) · 10 KB
/
nginx_send_minimum_rate.patch
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
diff --git src/core/ngx_connection.c src/core/ngx_connection.c
index 160b3e8..895ba3d 100644
--- src/core/ngx_connection.c
+++ src/core/ngx_connection.c
@@ -1338,3 +1338,60 @@ ngx_connection_error(ngx_connection_t *c, ngx_err_t err, char *text)
return NGX_ERROR;
}
+
+
+/*
+ * When a socket is idle, determines if the kernel is draining its buffer
+ * faster than minimum_rate. Avoids false negatives by allowing sufficient
+ * time to pass (currently 1 second) before recording the current send rate.
+ * Used to avoid closing an active connection whose buffers are so large
+ * relative to throughput that we stop sending data for long periods of time.
+ */
+ngx_flag_t
+ngx_connection_sending_above_rate(
+ ngx_connection_t *c, size_t minimum_rate)
+{
+ ssize_t new_unacknowledged_bytes, bytes_sent;
+ ngx_msec_t t1, time_elapsed;
+ ngx_time_t *tp;
+
+ if (minimum_rate == 0) {
+ return 0;
+ }
+
+ tp = ngx_timeofday();
+ t1 = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
+ time_elapsed = t1 - c->last_send_rate_time;
+
+ if (time_elapsed < 1000) {
+ /*
+ * Need more time to get an accurate rate.
+ * Defends against spurious wakeups when used in the timeout handler
+ */
+ return 1;
+ }
+
+ new_unacknowledged_bytes = ngx_tcp_unacknowledged_bytes(c->fd);
+ bytes_sent = c->socket_unacknowledged_bytes - new_unacknowledged_bytes;
+
+ c->socket_unacknowledged_bytes = new_unacknowledged_bytes;
+ c->last_send_rate_time = t1;
+
+ if (bytes_sent < 0) {
+ /* buffer grew */
+ return 1;
+ }
+
+ if ((size_t)bytes_sent >= (time_elapsed * minimum_rate) / 1000) {
+ return 1;
+ }
+
+ return 0;
+}
+
+
+void
+ngx_connection_reset_send_rate(ngx_connection_t *c)
+{
+ c->socket_unacknowledged_bytes = 0;
+}
diff --git src/core/ngx_connection.h src/core/ngx_connection.h
index 2930cbc..771abd9 100644
--- src/core/ngx_connection.h
+++ src/core/ngx_connection.h
@@ -199,6 +199,9 @@ struct ngx_connection_s {
#if (NGX_THREADS)
ngx_thread_task_t *sendfile_task;
#endif
+
+ ssize_t socket_unacknowledged_bytes;
+ ngx_msec_t last_send_rate_time;
};
@@ -230,4 +233,8 @@ void ngx_free_connection(ngx_connection_t *c);
void ngx_reusable_connection(ngx_connection_t *c, ngx_uint_t reusable);
+ngx_flag_t ngx_connection_sending_above_rate(ngx_connection_t *c,
+ size_t minimum_rate);
+void ngx_connection_reset_send_rate(ngx_connection_t *c);
+
#endif /* _NGX_CONNECTION_H_INCLUDED_ */
diff --git src/http/ngx_http_core_module.c src/http/ngx_http_core_module.c
index ea3da5b..f2cbd69 100644
--- src/http/ngx_http_core_module.c
+++ src/http/ngx_http_core_module.c
@@ -753,6 +753,13 @@ static ngx_command_t ngx_http_core_commands[] = {
0,
NULL },
+ { ngx_string("send_minimum_rate"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_size_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_core_loc_conf_t, send_minimum_rate),
+ NULL },
+
ngx_null_command
};
@@ -3688,6 +3695,7 @@ ngx_http_core_create_loc_conf(ngx_conf_t *cf)
clcf->tcp_nopush = NGX_CONF_UNSET;
clcf->tcp_nodelay = NGX_CONF_UNSET;
clcf->send_timeout = NGX_CONF_UNSET_MSEC;
+ clcf->send_minimum_rate = NGX_CONF_UNSET_SIZE;
clcf->send_lowat = NGX_CONF_UNSET_SIZE;
clcf->postpone_output = NGX_CONF_UNSET_SIZE;
clcf->limit_rate = NGX_CONF_UNSET_SIZE;
@@ -3921,6 +3929,8 @@ ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1);
ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
+ ngx_conf_merge_size_value(conf->send_minimum_rate, prev->send_minimum_rate,
+ 0);
ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
1460);
diff --git src/http/ngx_http_core_module.h src/http/ngx_http_core_module.h
index 3cb669f..59cf58d 100644
--- src/http/ngx_http_core_module.h
+++ src/http/ngx_http_core_module.h
@@ -387,6 +387,7 @@ struct ngx_http_core_loc_conf_s {
size_t limit_rate_after; /* limit_rate_after */
size_t sendfile_max_chunk; /* sendfile_max_chunk */
size_t read_ahead; /* read_ahead */
+ size_t send_minimum_rate; /* send_minimum_rate */
ngx_msec_t client_body_timeout; /* client_body_timeout */
ngx_msec_t send_timeout; /* send_timeout */
diff --git src/http/ngx_http_request.c src/http/ngx_http_request.c
index c07b0e6..9532423 100644
--- src/http/ngx_http_request.c
+++ src/http/ngx_http_request.c
@@ -2652,6 +2652,12 @@ ngx_http_writer(ngx_http_request_t *r)
if (wev->timedout) {
if (!wev->delayed) {
+ if (ngx_connection_sending_above_rate(c, clcf->send_minimum_rate)) {
+ wev->timedout = 0;
+ ngx_add_timer(wev, clcf->send_timeout);
+ return;
+ }
+
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
"client timed out");
c->timedout = 1;
@@ -2675,6 +2681,8 @@ ngx_http_writer(ngx_http_request_t *r)
}
+ ngx_connection_reset_send_rate(c);
+
if (wev->delayed || r->aio) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
"http writer delayed");
diff --git src/http/ngx_http_upstream.c src/http/ngx_http_upstream.c
index 64f1162..fcb8062 100644
--- src/http/ngx_http_upstream.c
+++ src/http/ngx_http_upstream.c
@@ -3530,13 +3530,22 @@ ngx_http_upstream_process_upgraded(ngx_http_request_t *r,
downstream = c;
upstream = u->peer.connection;
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
if (downstream->write->timedout) {
- c->timedout = 1;
- ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
- ngx_http_upstream_finalize_request(r, u, NGX_HTTP_REQUEST_TIME_OUT);
- return;
+ if (ngx_connection_sending_above_rate(c, clcf->send_minimum_rate)) {
+ downstream->write->timedout = 0;
+
+ } else {
+ c->timedout = 1;
+ ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
+ ngx_http_upstream_finalize_request(r, u, NGX_HTTP_REQUEST_TIME_OUT);
+ return;
+ }
}
+ ngx_connection_reset_send_rate(c);
+
if (upstream->read->timedout || upstream->write->timedout) {
ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
ngx_http_upstream_finalize_request(r, u, NGX_HTTP_GATEWAY_TIME_OUT);
@@ -3635,8 +3644,6 @@ ngx_http_upstream_process_upgraded(ngx_http_request_t *r,
return;
}
- clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
-
if (ngx_handle_write_event(upstream->write, u->conf->send_lowat)
!= NGX_OK)
{
@@ -3698,6 +3705,7 @@ ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
ngx_event_t *wev;
ngx_connection_t *c;
ngx_http_upstream_t *u;
+ ngx_http_core_loc_conf_t *clcf;
c = r->connection;
u = r->upstream;
@@ -3709,12 +3717,21 @@ ngx_http_upstream_process_non_buffered_downstream(ngx_http_request_t *r)
c->log->action = "sending to client";
if (wev->timedout) {
- c->timedout = 1;
- ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
- ngx_http_upstream_finalize_request(r, u, NGX_HTTP_REQUEST_TIME_OUT);
- return;
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ if (ngx_connection_sending_above_rate(c, clcf->send_minimum_rate)) {
+ wev->timedout = 0;
+
+ } else {
+ c->timedout = 1;
+ ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
+ ngx_http_upstream_finalize_request(r, u, NGX_HTTP_REQUEST_TIME_OUT);
+ return;
+ }
}
+ ngx_connection_reset_send_rate(c);
+
ngx_http_upstream_process_non_buffered_request(r, 1);
}
@@ -3926,6 +3943,7 @@ ngx_http_upstream_process_downstream(ngx_http_request_t *r)
ngx_connection_t *c;
ngx_event_pipe_t *p;
ngx_http_upstream_t *u;
+ ngx_http_core_loc_conf_t *clcf;
c = r->connection;
u = r->upstream;
@@ -3960,6 +3978,18 @@ ngx_http_upstream_process_downstream(ngx_http_request_t *r)
}
} else {
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ if (ngx_connection_sending_above_rate(c, clcf->send_minimum_rate)) {
+ wev->timedout = 0;
+ ngx_add_timer(wev, p->send_timeout);
+
+ if (ngx_handle_write_event(wev, p->send_lowat) != NGX_OK) {
+ ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
+ }
+
+ return;
+ }
p->downstream_error = 1;
c->timedout = 1;
ngx_connection_error(c, NGX_ETIMEDOUT, "client timed out");
@@ -3985,6 +4015,8 @@ ngx_http_upstream_process_downstream(ngx_http_request_t *r)
}
}
+ ngx_connection_reset_send_rate(c);
+
ngx_http_upstream_process_request(r, u);
}
diff --git src/os/unix/ngx_socket.c src/os/unix/ngx_socket.c
index 3978f65..4bec805 100644
--- src/os/unix/ngx_socket.c
+++ src/os/unix/ngx_socket.c
@@ -114,3 +114,28 @@ ngx_tcp_push(ngx_socket_t s)
}
#endif
+
+
+#if (NGX_LINUX)
+
+int
+ngx_tcp_unacknowledged_bytes(ngx_socket_t s)
+{
+ int bs;
+
+ if (ioctl(s, TIOCOUTQ, &bs) == -1) {
+ return 0;
+ }
+
+ return bs;
+}
+
+#else
+
+int
+ngx_tcp_unacknowledged_bytes(ngx_socket_t s)
+{
+ return 0;
+}
+
+#endif
diff --git src/os/unix/ngx_socket.h src/os/unix/ngx_socket.h
index fcc5153..3b2cad2 100644
--- src/os/unix/ngx_socket.h
+++ src/os/unix/ngx_socket.h
@@ -40,6 +40,7 @@ int ngx_blocking(ngx_socket_t s);
int ngx_tcp_nopush(ngx_socket_t s);
int ngx_tcp_push(ngx_socket_t s);
+int ngx_tcp_unacknowledged_bytes(ngx_socket_t s);
#if (NGX_LINUX)