Skip to content

Commit 49d489f

Browse files
committed
Correctly clear resource from pool and context on log
1 parent a1f26e7 commit 49d489f

File tree

4 files changed

+24
-38
lines changed

4 files changed

+24
-38
lines changed

src/ngx_http_redirectionio_module.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static ngx_int_t ngx_http_redirectionio_redirect_handler(ngx_http_request_t *r)
208208

209209
if (ctx->connection_error) {
210210
if (ctx->resource != NULL) {
211-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 1);
211+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 1);
212212
}
213213

214214
ctx->wait_for_connection = 0;
@@ -247,10 +247,7 @@ static ngx_int_t ngx_http_redirectionio_redirect_handler(ngx_http_request_t *r)
247247
return NGX_AGAIN;
248248
}
249249

250-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 0);
251-
252-
ctx->wait_for_connection = 0;
253-
ctx->resource = NULL;
250+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 0);
254251

255252
if (ctx->status == 0 || ctx->match_on_response_status != 0) {
256253
return NGX_DECLINED;

src/ngx_http_redirectionio_module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ngx_int_t ngx_http_redirectionio_pool_construct(void **resource, void *params);
7878
ngx_int_t ngx_http_redirectionio_pool_destruct(void *resource, void *params);
7979
ngx_int_t ngx_http_redirectionio_pool_available(ngx_reslist_t *reslist, void *resource, void *data, ngx_int_t deferred);
8080
ngx_int_t ngx_http_redirectionio_pool_available_log_handler(ngx_reslist_t *reslist, void *resource, void *data, ngx_int_t deferred);
81-
void ngx_http_redirectionio_release_resource(ngx_reslist_t *reslist, ngx_http_redirectionio_resource_t *resource, ngx_uint_t in_error);
81+
void ngx_http_redirectionio_release_resource(ngx_reslist_t *reslist, ngx_http_redirectionio_ctx_t *ctx, ngx_uint_t in_error);
8282
void ngx_http_redirectionio_read_handler(ngx_event_t *rev);
8383
void ngx_http_redirectionio_read_binary_handler(ngx_event_t *rev);
8484

src/ngx_http_redirectionio_module_filter.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,7 @@ ngx_int_t ngx_http_redirectionio_headers_filter(ngx_http_request_t *r) {
103103

104104
// Check connection
105105
if (ctx->connection_error) {
106-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 1);
107-
108-
ctx->wait_for_connection = 0;
109-
ctx->resource = NULL;
110-
ctx->connection_error = 0;
106+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 1);
111107

112108
return ngx_http_next_header_filter(r);
113109
}
@@ -182,11 +178,7 @@ ngx_int_t ngx_http_redirectionio_body_filter(ngx_http_request_t *r, ngx_chain_t
182178

183179
// Check connection
184180
if (ctx->connection_error) {
185-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 1);
186-
187-
ctx->wait_for_connection = 0;
188-
ctx->resource = NULL;
189-
ctx->connection_error = 0;
181+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 1);
190182

191183
return ngx_http_next_body_filter(r, ctx->body_buffer);
192184
}
@@ -270,7 +262,7 @@ static void ngx_http_redirectionio_read_filter_headers_handler(ngx_event_t *rev,
270262
ctx->wait_for_header_filtering = 0;
271263

272264
if (json == NULL) {
273-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 1);
265+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 1);
274266
ngx_http_redirectionio_finalize_request(r, ctx);
275267

276268
return;
@@ -279,7 +271,7 @@ static void ngx_http_redirectionio_read_filter_headers_handler(ngx_event_t *rev,
279271
headers = cJSON_GetObjectItem(json, "headers");
280272

281273
if (headers == NULL || headers->type != cJSON_Array) {
282-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 1);
274+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 1);
283275
ngx_http_redirectionio_finalize_request(r, ctx);
284276

285277
return;
@@ -332,9 +324,7 @@ static void ngx_http_redirectionio_read_filter_headers_handler(ngx_event_t *rev,
332324
h->value.len = strlen(value->valuestring);
333325
}
334326

335-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, 0);
336-
ctx->wait_for_connection = 0;
337-
ctx->resource = NULL;
327+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, 0);
338328

339329
ngx_http_redirectionio_finalize_request(r, ctx);
340330
}
@@ -359,8 +349,7 @@ static void ngx_http_redirectionio_read_filter_body_handler(ngx_event_t *rev, u_
359349
// If buffer is last or errored -> send a last empty buffer, finalize request and release resource
360350
if (buffer_size < 0) {
361351
if (ctx->resource != NULL) {
362-
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx->resource, (buffer_size == -1) ? 0 : 1);
363-
ctx->resource = NULL;
352+
ngx_http_redirectionio_release_resource(conf->connection_pool, ctx, (buffer_size == -1) ? 0 : 1);
364353
}
365354

366355
new_chain = ngx_alloc_chain_link(r->pool);

src/ngx_http_redirectionio_module_pool.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,38 +107,38 @@ ngx_int_t ngx_http_redirectionio_pool_available(ngx_reslist_t *reslist, void *re
107107
}
108108

109109
ngx_int_t ngx_http_redirectionio_pool_available_log_handler(ngx_reslist_t *reslist, void *resource, void *data, ngx_int_t deferred) {
110-
ngx_http_redirectionio_log_t *log = (ngx_http_redirectionio_log_t *)data;
111-
ngx_peer_connection_t *peer = (ngx_peer_connection_t *)resource;
110+
ngx_http_redirectionio_log_t *log = (ngx_http_redirectionio_log_t *)data;
111+
ngx_http_redirectionio_resource_t *rr = (ngx_http_redirectionio_resource_t *)resource;
112112

113-
if (peer == NULL) {
113+
if (rr == NULL) {
114114
ngx_http_redirectionio_protocol_free_log(log);
115115

116116
return NGX_ERROR;
117117
}
118118

119-
ngx_http_redirectionio_protocol_send_log(peer->connection, log);
119+
ngx_http_redirectionio_protocol_send_log(rr->peer.connection, log);
120120
ngx_http_redirectionio_protocol_free_log(log);
121-
ngx_http_redirectionio_release_resource(reslist, resource, 0);
121+
ngx_reslist_release(reslist, rr);
122122

123123
return NGX_OK;
124124
}
125125

126-
void ngx_http_redirectionio_release_resource(ngx_reslist_t *reslist, ngx_http_redirectionio_resource_t *resource, ngx_uint_t in_error) {
127-
if (in_error) {
128-
ngx_reslist_invalidate(reslist, resource);
129-
126+
void ngx_http_redirectionio_release_resource(ngx_reslist_t *reslist, ngx_http_redirectionio_ctx_t *ctx, ngx_uint_t in_error) {
127+
if (ctx->resource == NULL) {
130128
return;
131129
}
132130

133-
resource->usage++;
134-
135-
if (resource->usage > NGX_HTTP_REDIRECTIONIO_RESOURCE_MAX_USAGE) {
136-
ngx_reslist_invalidate(reslist, resource);
131+
ctx->resource->usage++;
137132

138-
return;
133+
if (!in_error && ctx->resource->usage < NGX_HTTP_REDIRECTIONIO_RESOURCE_MAX_USAGE) {
134+
ngx_reslist_release(reslist, ctx->resource);
135+
} else {
136+
ngx_reslist_invalidate(reslist, ctx->resource);
139137
}
140138

141-
ngx_reslist_release(reslist, resource);
139+
ctx->resource = NULL;
140+
ctx->wait_for_connection = 0;
141+
ctx->connection_error = 0;
142142
}
143143

144144
static ngx_int_t ngx_http_redirectionio_get_connection(ngx_peer_connection_t *pc, void *data) {

0 commit comments

Comments
 (0)