diff --git a/src/pgsql_connection.erl b/src/pgsql_connection.erl index e82026d..1a759ef 100644 --- a/src/pgsql_connection.erl +++ b/src/pgsql_connection.erl @@ -138,6 +138,7 @@ | {application_name, atom() | iodata()} % default: node() | {timezone, iodata() | undefined} % default: undefined (not set) | {async, pid()} % subscribe to notifications (default: no) + | {connection_timeout, integer()} % default: infinity | proplists:property(). % undocumented ! -type open_options() :: [open_option()]. -type query_option() :: @@ -450,7 +451,12 @@ unsubscribe(Pid, {pgsql_connection, ConnectionPid}) -> %% -spec start_link(open_options()) -> {ok, pid()} | {error, any()}. start_link(Options) -> - gen_server:start_link(?MODULE, Options, []). + case lists:keytake(connection_timeout, 1, Options) of + false -> + gen_server:start_link(?MODULE, Options, []); + {value, {connection_timeout, Timeout}, ConnectionOptions} -> + gen_server:start_link(?MODULE, ConnectionOptions, [{timeout, Timeout}]) + end. %% ========================================================================= %% %% gen_server API diff --git a/test/pgsql_connection_test.erl b/test/pgsql_connection_test.erl index 367a864..4fb4296 100644 --- a/test/pgsql_connection_test.erl +++ b/test/pgsql_connection_test.erl @@ -63,6 +63,16 @@ open_close_test_() -> catch throw:{pgsql_error, _Error} -> ok end + end)}, + {"Connection timeout", + ?_test(begin + try + R = pgsql_connection:open("0.0.0.0", "test", "test", "", [{connection_timeout, 0}]), + pgsql_connection:close(R), + ?assert(false) + catch throw:timeout -> + ok + end end)} ]}.