Permalink
Browse files

Fix compatibility with poll, select and /dev/poll event models.

Reported by Yichun Zhang (agentzh).

Change-Id: I9411e72ccfdbb0d974a92b6cba569dc15254ed3d
Signed-off-by: Piotr Sikora <piotr.sikora@frickle.com>
  • Loading branch information...
1 parent c7d6a04 commit 51822ec5b6cd549d97430e49c197b8ddd78a5fed @PiotrSikora PiotrSikora committed Dec 23, 2011
Showing with 37 additions and 24 deletions.
  1. +16 −10 src/ngx_postgres_keepalive.c
  2. +21 −14 src/ngx_postgres_upstream.c
@@ -186,6 +186,22 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc,
{
c = pc->connection;
+ if (c->read->timer_set) {
+ ngx_del_timer(c->read);
+ }
+
+ if (c->write->timer_set) {
+ ngx_del_timer(c->write);
+ }
+
+ if (c->write->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
+ if (ngx_del_event(c->write, NGX_WRITE_EVENT, 0) != NGX_OK) {
+ return;
+ }
+ }
+
+ pc->connection = NULL;
+
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"postgres: free keepalive peer: saving connection %p",
c);
@@ -213,16 +229,6 @@ ngx_postgres_keepalive_free_peer(ngx_peer_connection_t *pc,
item->connection = c;
ngx_queue_insert_head(&pgscf->cache, q);
- pc->connection = NULL;
-
- if (c->read->timer_set) {
- ngx_del_timer(c->read);
- }
-
- if (c->write->timer_set) {
- ngx_del_timer(c->write);
- }
-
c->write->handler = ngx_postgres_keepalive_dummy_handler;
c->read->handler = ngx_postgres_keepalive_close_handler;
@@ -415,26 +415,29 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
dd("NGX_USE_RTSIG_EVENT");
- rc = ngx_add_conn(pgxc);
+ if (ngx_add_conn(pgxc) != NGX_OK) {
+ goto bad_add;
+ }
+
} else if (ngx_event_flags & NGX_USE_CLEAR_EVENT) {
dd("NGX_USE_CLEAR_EVENT");
- rc = ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT);
- if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) != NGX_OK) {
- ngx_log_error(NGX_LOG_ERR, pc->log, 0,
- "postgres: failed to add nginx connection");
+ if (ngx_add_event(rev, NGX_READ_EVENT, NGX_CLEAR_EVENT) != NGX_OK) {
+ goto bad_add;
+ }
- goto invalid;
+ if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_CLEAR_EVENT) != NGX_OK) {
+ goto bad_add;
}
- } else {
- dd("NGX_LEVEL_EVENT");
- rc = ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT);
- }
- if (rc != NGX_OK) {
- ngx_log_error(NGX_LOG_ERR, pc->log, 0,
- "postgres: failed to add nginx connection");
+ } else {
+ dd("NGX_USE_LEVEL_EVENT");
+ if (ngx_add_event(rev, NGX_READ_EVENT, NGX_LEVEL_EVENT) != NGX_OK) {
+ goto bad_add;
+ }
- goto invalid;
+ if (ngx_add_event(wev, NGX_WRITE_EVENT, NGX_LEVEL_EVENT) != NGX_OK) {
+ goto bad_add;
+ }
}
pgxc->log->action = "connecting to PostgreSQL database";
@@ -443,6 +446,10 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
dd("returning NGX_AGAIN");
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);

0 comments on commit 51822ec

Please sign in to comment.