Skip to content

Build as Dynamic module support; sockets; fixes #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -43,11 +43,11 @@ Configuration directives
========================
postgres_server
---------------
* **syntax**: `postgres_server ip[:port] dbname=dbname user=user password=pass`
* **syntax**: `postgres_server {ip[:portnum]|unix:/socket/dir} [port=portnum] [dbname=dbname] [user=user] [password=pass]`
* **default**: `none`
* **context**: `upstream`

Set details about the database server.
Set details about the database server. Additional port parameter is offered to connect to unix socket with alternative port numbers.


postgres_keepalive
124 changes: 26 additions & 98 deletions config
Original file line number Diff line number Diff line change
@@ -102,27 +102,16 @@ END
exit 1
fi

ngx_version=`grep nginx_version src/core/nginx.h | sed -e 's/^.* \(.*\)$/\1/'`
lib_version=90600
ngx_feature="libpq library version 9.6"

if [ -z "$ngx_version" ]; then
cat << END
$0: error: ngx_postgres addon was unable to detect version of nginx.
END
exit 1
fi

# work-around for versions of nginx older than nginx-0.9.0
if [ $ngx_version -ge 9000 ]; then
ngx_feature_name="NGX_POSTGRES_LIBRARY_VERSION"
ngx_feature_run=value
else
ngx_feature_name="NGX_POSTGRES_LIBRARY_VERSION_DETECTED"
ngx_feature_run=no
if [ $ngx_found = no ]; then
lib_version=90500
ngx_feature="libpq library version 9.5"
ngx_feature_test="printf(\"%d\", PQlibVersion())"
. auto/feature
fi

lib_version=90500
ngx_feature="libpq library version 9.5"

if [ $ngx_found = no ]; then
lib_version=90400
ngx_feature="libpq library version 9.4"
@@ -151,92 +140,31 @@ if [ $ngx_found = no ]; then
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=90000
ngx_feature="libpq library version 9.0"
ngx_feature_test="(void) PQescapeLiteral(NULL, NULL, 0);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80400
ngx_feature="libpq library version 8.4"
ngx_feature_test="PQinitOpenSSL(0, 0);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80300
ngx_feature="libpq library version 8.3"
ngx_feature_test="(void) PQconnectionNeedsPassword(NULL);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80200
ngx_feature="libpq library version 8.2"
ngx_feature_test="(void) PQsendDescribePortal(NULL, NULL);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80104
ngx_feature="libpq library version 8.1.4"
ngx_feature_test="(void) PQescapeByteaConn(NULL, NULL, 0, 0);
(void) PQregisterThreadLock(NULL);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80100
ngx_feature="libpq library version 8.1.0"
ngx_feature_test="(void) PQregisterThreadLock(NULL);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80008
ngx_feature="libpq library version 8.0.8"
ngx_feature_test="(void) PQescapeByteaConn(NULL, NULL, 0, 0);
(void) PQcancel(NULL, NULL, 0);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
lib_version=80000
ngx_feature="libpq library version 8.0.0"
ngx_feature_test="(void) PQcancel(NULL, NULL, 0);
printf(\"$lib_version\")"
. auto/feature
fi

if [ $ngx_found = no ]; then
cat << END
$0: error: ngx_postgres addon was unable to detect version of the libpq library.
END
exit 1
fi

# work-around for versions of nginx older than nginx-0.9.0
if [ $ngx_version -lt 9000 ]; then
have=NGX_POSTGRES_LIBRARY_VERSION value=$lib_version . auto/define
fi

ngx_addon_name=ngx_postgres

HTTP_MODULES="$HTTP_MODULES ngx_postgres_module"
ngx_addon_name=ngx_postgres_module
NGX_SRCS="$ngx_addon_dir/src/ngx_postgres_escape.c $ngx_addon_dir/src/ngx_postgres_handler.c $ngx_addon_dir/src/ngx_postgres_keepalive.c $ngx_addon_dir/src/ngx_postgres_module.c $ngx_addon_dir/src/ngx_postgres_output.c $ngx_addon_dir/src/ngx_postgres_processor.c $ngx_addon_dir/src/ngx_postgres_rewrite.c $ngx_addon_dir/src/ngx_postgres_upstream.c $ngx_addon_dir/src/ngx_postgres_util.c $ngx_addon_dir/src/ngx_postgres_variable.c"
NGX_DEPS="$ngx_addon_dir/src/ngx_postgres_escape.h $ngx_addon_dir/src/ngx_postgres_handler.h $ngx_addon_dir/src/ngx_postgres_keepalive.h $ngx_addon_dir/src/ngx_postgres_module.h $ngx_addon_dir/src/ngx_postgres_output.h $ngx_addon_dir/src/ngx_postgres_processor.h $ngx_addon_dir/src/ngx_postgres_rewrite.h $ngx_addon_dir/src/ngx_postgres_upstream.h $ngx_addon_dir/src/ngx_postgres_util.h $ngx_addon_dir/src/ngx_postgres_variable.h $ngx_addon_dir/src/ngx_postgres_ddebug.h $ngx_addon_dir/src/resty_dbd_stream.h"

CORE_INCS="$CORE_INCS $ngx_feature_path"
CORE_LIBS="$CORE_LIBS $ngx_feature_libs"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_postgres_escape.c $ngx_addon_dir/src/ngx_postgres_handler.c $ngx_addon_dir/src/ngx_postgres_keepalive.c $ngx_addon_dir/src/ngx_postgres_module.c $ngx_addon_dir/src/ngx_postgres_output.c $ngx_addon_dir/src/ngx_postgres_processor.c $ngx_addon_dir/src/ngx_postgres_rewrite.c $ngx_addon_dir/src/ngx_postgres_upstream.c $ngx_addon_dir/src/ngx_postgres_util.c $ngx_addon_dir/src/ngx_postgres_variable.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ngx_postgres_escape.h $ngx_addon_dir/src/ngx_postgres_handler.h $ngx_addon_dir/src/ngx_postgres_keepalive.h $ngx_addon_dir/src/ngx_postgres_module.h $ngx_addon_dir/src/ngx_postgres_output.h $ngx_addon_dir/src/ngx_postgres_processor.h $ngx_addon_dir/src/ngx_postgres_rewrite.h $ngx_addon_dir/src/ngx_postgres_upstream.h $ngx_addon_dir/src/ngx_postgres_util.h $ngx_addon_dir/src/ngx_postgres_variable.h $ngx_addon_dir/src/ngx_postgres_ddebug.h $ngx_addon_dir/src/resty_dbd_stream.h"
if test -n "$ngx_module_link"; then
ngx_module_type=HTTP
ngx_module_name=ngx_postgres_module
ngx_module_srcs="$NGX_SRCS"
ngx_module_deps="$NGX_DEPS"
ngx_module_libs="$ngx_feature_libs"
ngx_module_incs="$ngx_feature_path"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_postgres_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $NGX_SRCS"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $NGX_DEPS"
CORE_INCS="$CORE_INCS $ngx_feature_path"
CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
fi

have=NGX_POSTGRES_MODULE . auto/have
have=NGX_POSTGRES_MODULE . auto/have
21 changes: 19 additions & 2 deletions src/ngx_postgres_module.c
Original file line number Diff line number Diff line change
@@ -446,11 +446,19 @@ ngx_postgres_conf_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

pgs->addrs = u.addrs;
pgs->naddrs = u.naddrs;
pgs->port = u.port;
pgs->port = u.family == AF_UNIX ? u.default_port : u.port;
pgs->family = u.family;

/* parse various options */
for (i = 2; i < cf->args->nelts; i++) {

if (ngx_strncmp(value[i].data, "port=", sizeof("port=") - 1)
== 0)
{
pgs->port = (in_port_t) ngx_atoi(&value[i].data[sizeof("port=") - 1], value[i].len - (sizeof("port=") - 1));
continue;
}

if (ngx_strncmp(value[i].data, "dbname=", sizeof("dbname=") - 1)
== 0)
{
@@ -1314,13 +1322,22 @@ ngx_postgres_find_upstream(ngx_http_request_t *r, ngx_url_t *url)
dd("host doesn't match");
continue;
}


#if (nginx_version < 1011006)
if (uscfp[i]->port != url->port) {
dd("port doesn't match: %d != %d",
(int) uscfp[i]->port, (int) url->port);
continue;
}

if (uscfp[i]->default_port && url->default_port
&& (uscfp[i]->default_port != url->default_port))
{
dd("default_port doesn't match");
continue;
}

#endif
dd("returning");
return uscfp[i];
}
2 changes: 2 additions & 0 deletions src/ngx_postgres_module.h
Original file line number Diff line number Diff line change
@@ -112,6 +112,7 @@ typedef struct {
#endif
ngx_uint_t naddrs;
in_port_t port;
int family;
ngx_str_t dbname;
ngx_str_t user;
ngx_str_t password;
@@ -123,6 +124,7 @@ typedef struct {
ngx_str_t name;
ngx_str_t host;
in_port_t port;
int family;
ngx_str_t dbname;
ngx_str_t user;
ngx_str_t password;
22 changes: 16 additions & 6 deletions src/ngx_postgres_upstream.c
Original file line number Diff line number Diff line change
@@ -91,6 +91,7 @@ ngx_postgres_upstream_init(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *uscf)
peers->peer[n].socklen = server[i].addrs[j].socklen;
peers->peer[n].name = server[i].addrs[j].name;
peers->peer[n].port = server[i].port;
peers->peer[n].family = server[i].family;
peers->peer[n].dbname = server[i].dbname;
peers->peer[n].user = server[i].user;
peers->peer[n].password = server[i].password;
@@ -330,7 +331,10 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
}

/* sizeof("...") - 1 + 1 (for spaces and '\0' omitted */
/* we hope that unix sockets connection string will be always shorter than tcp/ip one (because 'host' is shorter than 'hostaddr') */
len = sizeof("hostaddr=") + peer->host.len
+ sizeof("port=") + sizeof("65535") - 1
+ sizeof("dbname=") + peer->dbname.len
+ sizeof("port=") + sizeof("65535") - 1
+ sizeof("dbname=") + peer->dbname.len
+ sizeof("user=") + peer->user.len
@@ -347,12 +351,18 @@ ngx_postgres_upstream_get_peer(ngx_peer_connection_t *pc, void *data)
#endif
}

/* TODO add unix sockets */
last = ngx_snprintf(connstring, len - 1,
"hostaddr=%V port=%d dbname=%V user=%V password=%V"
" sslmode=disable",
&peer->host, peer->port, &peer->dbname, &peer->user,
&peer->password);
if(peer->family != AF_UNIX)
last = ngx_snprintf(connstring, len - 1,
"hostaddr=%V port=%d dbname=%V user=%V password=%V"
" sslmode=disable",
&peer->host, peer->port, &peer->dbname, &peer->user,
&peer->password);
else
last = ngx_snprintf(connstring, len - 1,
"host=%s port=%d dbname=%V user=%V password=%V"
" sslmode=disable",
&peer->host.data[5], peer->port, &peer->dbname, &peer->user,
&peer->password);
*last = '\0';

dd("PostgreSQL connection string: %s", connstring);