diff --git a/README.md b/README.md index f21043d6..c7b34eba 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ releases: - 0.9.x (tested with 0.9.0 to 0.9.7), - 1.0.x (tested with 1.0.0 to 1.0.11), - 1.1.x (tested with 1.1.0 to 1.1.12). +- 1.2.x (tested with 1.2.3 to 1.2.3). +- 1.3.x (tested with 1.3.4 to 1.3.4). Configuration directives diff --git a/src/ngx_postgres_ddebug.h b/src/ngx_postgres_ddebug.h index 7cc05ecd..38253fca 100644 --- a/src/ngx_postgres_ddebug.h +++ b/src/ngx_postgres_ddebug.h @@ -29,6 +29,7 @@ #ifndef _NGX_POSTGRES_DDEBUG_H_ #define _NGX_POSTGRES_DDEBUG_H_ +#include #include #if defined(DDEBUG) && (DDEBUG) diff --git a/src/ngx_postgres_escape.c b/src/ngx_postgres_escape.c index 3b589741..dd781947 100644 --- a/src/ngx_postgres_escape.c +++ b/src/ngx_postgres_escape.c @@ -90,6 +90,7 @@ ngx_postgres_escape_string(ngx_http_script_engine_t *e) dd("returning"); done: + v->valid = 1; v->no_cacheable = 0; v->not_found = 0; diff --git a/src/ngx_postgres_keepalive.c b/src/ngx_postgres_keepalive.c index f5a8a022..6575338c 100644 --- a/src/ngx_postgres_keepalive.c +++ b/src/ngx_postgres_keepalive.c @@ -85,6 +85,9 @@ ngx_postgres_keepalive_get_peer_single(ngx_peer_connection_t *pc, c->idle = 0; c->log = pc->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = pc->log; +#endif c->read->log = pc->log; c->write->log = pc->log; @@ -140,6 +143,9 @@ ngx_postgres_keepalive_get_peer_multi(ngx_peer_connection_t *pc, c->idle = 0; c->log = pc->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = pc->log; +#endif c->read->log = pc->log; c->write->log = pc->log; @@ -235,6 +241,9 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc, c->data = item; c->idle = 1; c->log = ngx_cycle->log; +#if defined(nginx_version) && (nginx_version >= 1001004) + c->pool->log = ngx_cycle->log; +#endif c->read->log = ngx_cycle->log; c->write->log = ngx_cycle->log; @@ -269,6 +278,10 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev) c = ev->data; item = c->data; + if (c->close) { + goto close; + } + if (PQconsumeInput(item->pgconn) && !PQisBusy(item->pgconn)) { res = PQgetResult(item->pgconn); if (res == NULL) { @@ -283,13 +296,13 @@ ngx_postgres_keepalive_close_handler(ngx_event_t *ev) "postgres: received result on idle keepalive connection"); } - pgscf = item->srv_conf; +close: - ngx_queue_remove(&item->queue); + pgscf = item->srv_conf; - ngx_postgres_upstream_free_connection(ev->log, item->connection, - item->pgconn, pgscf); + ngx_postgres_upstream_free_connection(ev->log, c, item->pgconn, pgscf); + ngx_queue_remove(&item->queue); ngx_queue_insert_head(&pgscf->free, &item->queue); dd("returning"); diff --git a/src/ngx_postgres_module.c b/src/ngx_postgres_module.c index 176d828e..365de007 100644 --- a/src/ngx_postgres_module.c +++ b/src/ngx_postgres_module.c @@ -872,6 +872,7 @@ ngx_postgres_conf_rewrite(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) pgrcf->handler = e[i].handler; found: + if (cf->args->nelts == 3) { /* default rewrite */ dd("default rewrite"); diff --git a/src/ngx_postgres_output.c b/src/ngx_postgres_output.c index 71e4d8dd..9fa5481c 100644 --- a/src/ngx_postgres_output.c +++ b/src/ngx_postgres_output.c @@ -260,6 +260,7 @@ ngx_postgres_output_rds(ngx_http_request_t *r, PGresult *res) } done: + last->next = NULL; /* set output response */ @@ -548,7 +549,7 @@ ngx_postgres_output_chain(ngx_http_request_t *r, ngx_chain_t *cl) pglcf = ngx_http_get_module_loc_conf(r, ngx_postgres_module); pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module); - r->headers_out.status = pgctx->status ? abs(pgctx->status) + r->headers_out.status = pgctx->status ? ngx_abs(pgctx->status) : NGX_HTTP_OK; if (pglcf->output_handler == &ngx_postgres_output_rds) { diff --git a/src/ngx_postgres_processor.c b/src/ngx_postgres_processor.c index 1b4dce1d..d25c0541 100644 --- a/src/ngx_postgres_processor.c +++ b/src/ngx_postgres_processor.c @@ -103,6 +103,7 @@ ngx_postgres_process_events(ngx_http_request_t *r) return; failed: + ngx_postgres_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); dd("returning"); @@ -200,6 +201,7 @@ ngx_postgres_upstream_connect(ngx_http_request_t *r, ngx_connection_t *pgxc, } done: + /* remove connection timeout from new connection */ if (pgxc->write->timer_set) { ngx_del_timer(pgxc->write); diff --git a/src/ngx_postgres_upstream.c b/src/ngx_postgres_upstream.c index fd293d5b..919029b0 100644 --- a/src/ngx_postgres_upstream.c +++ b/src/ngx_postgres_upstream.c @@ -30,6 +30,7 @@ #define DDEBUG 0 #endif +#include #include "ngx_postgres_ddebug.h" #include "ngx_postgres_module.h" #include "ngx_postgres_keepalive.h" @@ -102,6 +103,9 @@ ngx_postgres_upstream_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *uscf) } peers->peer[n].host.len = ngx_sock_ntop(peers->peer[n].sockaddr, +#if defined(nginx_version) && (nginx_version >= 1005003) + peers->peer[n].socklen, +#endif peers->peer[n].host.data, NGX_SOCKADDR_STRLEN, 0); if (peers->peer[n].host.len == 0) { @@ -218,6 +222,7 @@ ngx_postgres_upstream_init_peer(ngx_http_request_t *r, return NGX_OK; failed: + #if defined(nginx_version) && (nginx_version >= 8017) dd("returning NGX_ERROR"); return NGX_ERROR; @@ -456,10 +461,12 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) return NGX_AGAIN; bad_add: + ngx_log_error(NGX_LOG_ERR, pc->log, 0, "postgres: failed to add nginx connection"); invalid: + ngx_postgres_upstream_free_connection(pc->log, pc->connection, pgdt->pgconn, pgscf); @@ -469,6 +476,7 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data) #else failed: + /* a bit hack-ish way to return error response (setup part) */ pc->connection = ngx_get_connection(0, pc->log); @@ -553,18 +561,34 @@ ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c, } } +#if defined(nginx_version) && nginx_version >= 1007005 + if (rev->posted) { +#else if (rev->prev) { +#endif ngx_delete_posted_event(rev); } +#if defined(nginx_version) && nginx_version >= 1007005 + if (wev->posted) { +#else if (wev->prev) { +#endif ngx_delete_posted_event(wev); } rev->closed = 1; wev->closed = 1; +#if defined(nginx_version) && (nginx_version >= 1001004) + if (c->pool) { + ngx_destroy_pool(c->pool); + } +#endif + ngx_free_connection(c); + + c->fd = (ngx_socket_t) -1; } /* free spot in keepalive connection pool */ diff --git a/src/ngx_postgres_util.c b/src/ngx_postgres_util.c index cc254cdc..ae7de38c 100644 --- a/src/ngx_postgres_util.c +++ b/src/ngx_postgres_util.c @@ -45,7 +45,9 @@ void ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, ngx_http_upstream_t *u, ngx_int_t rc) { +#if defined(nginx_version) && (nginx_version < 1009001) ngx_time_t *tp; +#endif dd("entering"); @@ -61,10 +63,15 @@ ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, u->resolved->ctx = NULL; } +#if defined(nginx_version) && (nginx_version >= 1009001) + if (u->state && u->state->response_time) { + u->state->response_time = ngx_current_msec - u->state->response_time; +#else if (u->state && u->state->response_sec) { tp = ngx_timeofday(); u->state->response_sec = tp->sec - u->state->response_sec; u->state->response_msec = tp->msec - u->state->response_msec; +#endif if (u->pipe) { u->state->response_length = u->pipe->read_length; @@ -120,34 +127,6 @@ ngx_postgres_upstream_finalize_request(ngx_http_request_t *r, u->pipe->temp_file->file.fd); } -#if (NGX_HTTP_CACHE) - - if (u->cacheable && r->cache) { - time_t valid; - - ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, - "http upstream cache fd: %d", - r->cache->file.fd); - - if (rc == NGX_HTTP_BAD_GATEWAY || rc == NGX_HTTP_GATEWAY_TIME_OUT) { - - valid = ngx_http_file_cache_valid(u->conf->cache_valid, rc); - - if (valid) { - r->cache->valid_sec = ngx_time() + valid; - r->cache->error = rc; - } - } - -# if defined(nginx_version) && (nginx_version >= 8047) - ngx_http_file_cache_free(r->cache, u->pipe->temp_file); -# else - ngx_http_file_cache_free(r, u->pipe->temp_file); -# endif - } - -#endif - if (u->header_sent && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE)) { diff --git a/t/000_init.t b/t/000_init.t index ef50138b..832244e4 100644 --- a/t/000_init.t +++ b/t/000_init.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(1); -plan tests => repeat_each() * blocks(); +plan tests => repeat_each() * 2 * blocks(); $ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1'; $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; @@ -36,6 +36,8 @@ __DATA__ GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -50,6 +52,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -64,6 +68,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -78,6 +84,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -95,6 +103,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -109,6 +119,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -126,6 +138,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -140,6 +154,8 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] @@ -154,3 +170,5 @@ GET /init GET /init --- error_code: 200 --- timeout: 10 +--- no_error_log +[error] diff --git a/t/output.t b/t/output.t index a564bf86..b4ba503a 100644 --- a/t/output.t +++ b/t/output.t @@ -82,7 +82,7 @@ test -=== TEST 5: value - NULL value +=== TEST 4: value - NULL value --- http_config eval: $::http_config --- config default_type text/plain; @@ -99,7 +99,7 @@ GET /postgres -=== TEST 6: value - empty value +=== TEST 5: value - empty value --- http_config eval: $::http_config --- config default_type text/plain; @@ -116,7 +116,7 @@ GET /postgres -=== TEST 7: text - sanity +=== TEST 6: text - sanity --- http_config eval: $::http_config --- config default_type text/plain; @@ -143,7 +143,7 @@ Content-Type: text/plain -=== TEST 8: rds - sanity (configured) +=== TEST 7: rds - sanity (configured) --- http_config eval: $::http_config --- config location /postgres { @@ -179,7 +179,7 @@ Content-Type: application/x-resty-dbd-stream -=== TEST 9: rds - sanity (default) +=== TEST 8: rds - sanity (default) --- http_config eval: $::http_config --- config location /postgres { @@ -214,7 +214,7 @@ Content-Type: application/x-resty-dbd-stream -=== TEST 10: inheritance +=== TEST 9: inheritance --- http_config eval: $::http_config --- config default_type text/plain; @@ -235,7 +235,7 @@ test -=== TEST 11: inheritance (mixed, don't inherit) +=== TEST 10: inheritance (mixed, don't inherit) --- http_config eval: $::http_config --- config postgres_output text; @@ -256,7 +256,7 @@ GET /postgres -=== TEST 14: value - sanity (request with known extension) +=== TEST 11: value - sanity (request with known extension) --- http_config eval: $::http_config --- config default_type text/plain; @@ -277,7 +277,7 @@ test -=== TEST 15: value - bytea returned in text format +=== TEST 12: value - bytea returned in text format --- http_config eval: $::http_config --- config default_type text/plain; @@ -298,7 +298,7 @@ Content-Type: text/plain -=== TEST 16: binary value - bytea returned in binary format +=== TEST 13: binary value - bytea returned in binary format --- http_config eval: $::http_config --- config default_type text/plain; @@ -319,7 +319,7 @@ Content-Type: text/plain -=== TEST 17: binary value - int2 returned in binary format +=== TEST 14: binary value - int2 returned in binary format --- http_config eval: $::http_config --- config default_type text/plain; @@ -340,7 +340,7 @@ Content-Type: text/plain -=== TEST 18: value - "if" pseudo-location +=== TEST 15: value - "if" pseudo-location --- http_config eval: $::http_config --- config default_type text/plain; @@ -366,7 +366,7 @@ Content-Type: text/plain -=== TEST 19: text - NULL value +=== TEST 16: text - NULL value --- http_config eval: $::http_config --- config default_type text/plain; @@ -393,7 +393,7 @@ Content-Type: text/plain -=== TEST 20: text - empty result +=== TEST 17: text - empty result --- http_config eval: $::http_config --- config default_type text/plain; @@ -414,7 +414,7 @@ Content-Type: text/plain -=== TEST 21: value - empty result +=== TEST 18: value - empty result --- http_config eval: $::http_config --- config default_type text/plain; @@ -431,7 +431,7 @@ GET /postgres -=== TEST 22: value - too many values +=== TEST 19: value - too many values --- http_config eval: $::http_config --- config default_type text/plain; diff --git a/t/sanity.t b/t/sanity.t index 917d0edd..25c3d7ed 100644 --- a/t/sanity.t +++ b/t/sanity.t @@ -5,7 +5,7 @@ use Test::Nginx::Socket; repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); +plan tests => repeat_each() * (blocks() * 5); $ENV{TEST_NGINX_POSTGRESQL_HOST} ||= '127.0.0.1'; $ENV{TEST_NGINX_POSTGRESQL_PORT} ||= 5432; @@ -69,6 +69,9 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -115,6 +118,9 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -142,6 +148,9 @@ Content-Type: application/x-resty-dbd-stream "\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id "\x{00}\x{00}" # col count --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -178,6 +187,9 @@ Content-Type: application/x-resty-dbd-stream "name". # col name data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -206,6 +218,9 @@ Content-Type: application/x-resty-dbd-stream "\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}\x{00}". # insert id "\x{00}\x{00}" # col count --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -224,6 +239,9 @@ Content-Type: application/x-resty-dbd-stream --- response_body eval "" --- timeout: 10 +--- no_error_log +[alert] +[error] @@ -275,3 +293,6 @@ Content-Type: application/x-resty-dbd-stream "bob". # field data "\x{00}" # row list terminator --- timeout: 10 +--- no_error_log +[alert] +[error] diff --git a/valgrind.suppress b/valgrind.suppress index dc47c735..e0b8e89d 100644 --- a/valgrind.suppress +++ b/valgrind.suppress @@ -1,3 +1,100 @@ +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_event_accept + fun:ngx_event_process_posted + fun:ngx_process_events_and_timers +} +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_create_pool +fun:ngx_http_init_request +fun:ngx_event_process_posted +fun:ngx_process_events_and_timers +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_event_accept + fun:ngx_epoll_process_events + fun:ngx_process_events_and_timers +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_create_pool + fun:ngx_http_init_request + fun:ngx_epoll_process_events + fun:ngx_process_events_and_timers +} +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_malloc +fun:ngx_pnalloc +} +{ + +Memcheck:Leak +fun:malloc +fun:ngx_alloc +fun:ngx_malloc +fun:ngx_palloc +} +{ + +Memcheck:Addr4 +fun:ngx_init_cycle +fun:ngx_master_process_cycle +fun:main +} +{ + + Memcheck:Param + epoll_ctl(event) + fun:epoll_ctl +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_event_process_init +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:(below main) +} +{ + + Memcheck:Leak + fun:malloc + fun:ngx_alloc + fun:ngx_calloc + fun:ngx_event_process_init +} { Memcheck:Leak @@ -140,8 +237,6 @@ fun:malloc fun:ngx_alloc fun:ngx_event_process_init - fun:ngx_single_process_cycle - fun:main } { nginx-core-crc32-init @@ -162,8 +257,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { palloc_large_for_create_temp_buf @@ -176,8 +269,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { accept_create_pool @@ -189,8 +280,6 @@ fun:ngx_event_accept fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { create_pool_for_init_req @@ -202,8 +291,6 @@ fun:ngx_http_init_request fun:ngx_epoll_process_events fun:ngx_process_events_and_timers - fun:ngx_single_process_cycle - fun:main } { create_pool_posix_memalign @@ -231,4 +318,14 @@ fun:ngx_init_cycle fun:main } - +{ + + Memcheck:Cond + fun:index + fun:expand_dynamic_string_token + fun:_dl_map_object + fun:map_doit + fun:_dl_catch_error + fun:do_preload + fun:dl_main +}