Skip to content

Bring in the support of unix sockets #24

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -25,11 +25,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
Expand Down
10 changes: 9 additions & 1 deletion src/ngx_postgres_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,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)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_postgres_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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;
Expand All @@ -122,6 +123,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;
Expand Down
20 changes: 14 additions & 6 deletions src/ngx_postgres_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -329,6 +330,7 @@ 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
Expand All @@ -346,12 +348,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);
Expand Down