Skip to content

Commit 73703b5

Browse files
committed
better handling of connection failure in nginx
1 parent 7c03e04 commit 73703b5

5 files changed

+21
-13
lines changed

nginx-test.conf.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ events {
99
worker_connections 1024;
1010
}
1111

12-
error_log /proc/self/fd/2 debug;
12+
error_log /proc/self/fd/2;
1313

1414
http {
1515
default_type application/octet-stream;

src/ngx_http_redirectionio_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void ngx_http_redirectionio_release_resource(ngx_reslist_t *reslist, ngx_http_re
9292
void ngx_http_redirectionio_read_handler(ngx_event_t *rev);
9393

9494
void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_request_t *r, ngx_http_redirectionio_ctx_t *ctx, ngx_str_t *project_key);
95-
void ngx_http_redirectionio_protocol_send_log(ngx_connection_t *c, ngx_http_redirectionio_log_t *log);
95+
ngx_int_t ngx_http_redirectionio_protocol_send_log(ngx_connection_t *c, ngx_http_redirectionio_log_t *log);
9696
ngx_http_redirectionio_log_t* ngx_http_redirectionio_protocol_create_log(ngx_http_request_t *r, ngx_http_redirectionio_ctx_t *ctx, ngx_str_t *project_key);
9797
void ngx_http_redirectionio_protocol_free_log(ngx_http_redirectionio_log_t *log);
9898
void ngx_http_redirectionio_protocol_send_filter_header(ngx_connection_t *c, ngx_http_request_t *r, ngx_str_t *project_key, ngx_str_t *rule_id);

src/ngx_http_redirectionio_module_filter.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ngx_int_t ngx_http_redirectionio_match_on_response_status_header_filter(ngx_http
3737
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "http redirectionio status code update %d (on response status code)", redirect_status_code);
3838

3939
r->headers_out.status = redirect_status_code;
40-
r->headers_out.status_line.len = 0;
40+
r->headers_out.status_line = (ngx_str_t)ngx_null_string;
4141

4242
return ngx_http_redirectionio_headers_filter(r);
4343
}

src/ngx_http_redirectionio_module_pool.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,22 @@ ngx_int_t ngx_http_redirectionio_pool_available(ngx_reslist_t *reslist, void *re
111111
ngx_int_t ngx_http_redirectionio_pool_available_log_handler(ngx_reslist_t *reslist, void *resource, void *data, ngx_int_t deferred) {
112112
ngx_http_redirectionio_log_t *log = (ngx_http_redirectionio_log_t *)data;
113113
ngx_http_redirectionio_resource_t *rr = (ngx_http_redirectionio_resource_t *)resource;
114+
ngx_int_t rv;
114115

115116
if (rr == NULL) {
116117
ngx_http_redirectionio_protocol_free_log(log);
117118

118119
return NGX_ERROR;
119120
}
120121

121-
ngx_http_redirectionio_protocol_send_log(rr->peer.connection, log);
122+
rv = ngx_http_redirectionio_protocol_send_log(rr->peer.connection, log);
122123
ngx_http_redirectionio_protocol_free_log(log);
123-
ngx_reslist_release(reslist, rr);
124+
125+
if (rv != NGX_OK) {
126+
ngx_reslist_invalidate(reslist, rr);
127+
} else {
128+
ngx_reslist_release(reslist, rr);
129+
}
124130

125131
return NGX_OK;
126132
}

src/ngx_http_redirectionio_protocol.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,17 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
9797
rv = ngx_http_redirectionio_send_protocol_header(c, project_key, REDIRECTIONIO_PROTOCOL_COMMAND_MATCH_ACTION);
9898

9999
if (rv != NGX_OK) {
100+
ctx->connection_error = 1;
101+
100102
return;
101103
}
102104

103105
// Send serialized request length
104106
rv = ngx_http_redirectionio_send_uint32(c, strlen(request_serialized));
105107

106108
if (rv != NGX_OK) {
109+
ctx->connection_error = 1;
110+
107111
return;
108112
}
109113

@@ -113,33 +117,31 @@ void ngx_http_redirectionio_protocol_send_match(ngx_connection_t *c, ngx_http_re
113117
free((void *)request_serialized);
114118

115119
if (rv != NGX_OK) {
120+
ctx->connection_error = 1;
121+
116122
return;
117123
}
118124
}
119125

120-
void ngx_http_redirectionio_protocol_send_log(ngx_connection_t *c, ngx_http_redirectionio_log_t *log) {
126+
ngx_int_t ngx_http_redirectionio_protocol_send_log(ngx_connection_t *c, ngx_http_redirectionio_log_t *log) {
121127
ssize_t wlen = strlen(log->log_serialized);
122128
ngx_int_t rv;
123129

124130
// Send protocol header
125131
rv = ngx_http_redirectionio_send_protocol_header(c, &log->project_key, REDIRECTIONIO_PROTOCOL_COMMAND_LOG);
126132

127133
if (rv != NGX_OK) {
128-
return;
134+
return rv;
129135
}
130136

131137
// Send log length
132138
rv = ngx_http_redirectionio_send_uint32(c, wlen);
133139

134140
if (rv != NGX_OK) {
135-
return;
141+
return rv;
136142
}
137143

138-
rv = ngx_http_redirectionio_send_string(c, log->log_serialized, wlen);
139-
140-
if (rv != NGX_OK) {
141-
return;
142-
}
144+
return ngx_http_redirectionio_send_string(c, log->log_serialized, wlen);
143145
}
144146

145147
ngx_http_redirectionio_log_t* ngx_http_redirectionio_protocol_create_log(ngx_http_request_t *r, ngx_http_redirectionio_ctx_t *ctx, ngx_str_t *project_key) {

0 commit comments

Comments
 (0)