epgsql_pool_settings.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. -module(epgsql_pool_settings).
  2. -behavior(gen_server).
  3. -export([start_link/0, get_connection_params/1, set_connection_params/2, get/1, set/2]).
  4. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
  5. -include("epgsql_pool.hrl").
  6. -include("otp_types.hrl").
  7. -import(epgsql_pool_utils, [pool_name_to_atom/1]).
  8. -record(state, {
  9. connection_params :: orddict:orddictr(),
  10. settings :: orddict:orddict()
  11. }).
  12. %%% module API
  13. -spec start_link() -> gs_start_link_reply().
  14. start_link() ->
  15. gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
  16. -spec get_connection_params(pool_name()) -> #epgsql_connection_params{}. % or throw({connection_params_not_found, PoolName})
  17. get_connection_params(PoolName) ->
  18. gen_server:call(?MODULE, {get_connection_params, pool_name_to_atom(PoolName)}).
  19. -spec set_connection_params(pool_name(), #epgsql_connection_params{}) -> ok.
  20. set_connection_params(PoolName, Params) ->
  21. gen_server:call(?MODULE, {set_connection_params, pool_name_to_atom(PoolName), Params}).
  22. -spec get(atom()) -> integer(). % or throw({settings_not_found, Key})
  23. get(Key) ->
  24. gen_server:call(?MODULE, {get, Key}).
  25. -spec set(atom(), integer()) -> ok. % or throw({settings_not_found, Key})
  26. set(Key, Value) ->
  27. gen_server:call(?MODULE, {set, Key, Value}).
  28. %%% gen_server API
  29. -spec init(gs_args()) -> gs_init_reply().
  30. init([]) ->
  31. {ok, #state{connection_params = orddict:new(),
  32. settings = orddict:from_list(
  33. [{query_timeout, 10000},
  34. {pooler_get_worker_timeout, 1000},
  35. {max_reconnect_timeout, 3000},
  36. {min_reconnect_timeout, 100}
  37. ])}}.
  38. -spec handle_call(gs_request(), gs_from(), gs_reply()) -> gs_call_reply().
  39. handle_call({get_connection_params, PoolName}, _From,
  40. #state{connection_params = ConnectionParams} = State) ->
  41. Reply = case orddict:find(PoolName, ConnectionParams) of
  42. {ok, Params} -> Params;
  43. error -> throw({connection_params_not_found, PoolName})
  44. end,
  45. {reply, Reply, State};
  46. handle_call({set_connection_params, PoolName, Params}, _From,
  47. #state{connection_params = ConnectionParams} = State) ->
  48. ConnectionParams2 = orddict:store(PoolName, Params, ConnectionParams),
  49. {reply, ok, State#state{connection_params = ConnectionParams2}};
  50. handle_call({get, Key}, _From,
  51. #state{settings = Settings} = State) ->
  52. Reply = case orddict:find(Key, Settings) of
  53. {ok, Value} -> Value;
  54. error -> throw({settings_not_found, Key})
  55. end,
  56. {reply, Reply, State};
  57. handle_call({set, Key, Value}, _From,
  58. #state{settings = Settings} = State) ->
  59. Settings2 = case orddict:find(Key, Settings) of
  60. {ok, _} -> orddict:store(Key, Value, Settings);
  61. error -> throw({settings_not_found, Key})
  62. end,
  63. {reply, ok, State#state{settings = Settings2}};
  64. handle_call(Any, _From, State) ->
  65. error_logger:error_msg("unknown call ~p in ~p ~n", [Any, ?MODULE]),
  66. {noreply, State}.
  67. -spec handle_cast(gs_request(), gs_state()) -> gs_cast_reply().
  68. handle_cast(Any, State) ->
  69. error_logger:error_msg("unknown cast ~p in ~p ~n", [Any, ?MODULE]),
  70. {noreply, State}.
  71. -spec handle_info(gs_request(), gs_state()) -> gs_info_reply().
  72. handle_info(Request, State) ->
  73. error_logger:error_msg("unknown info ~p in ~p ~n", [Request, ?MODULE]),
  74. {noreply, State}.
  75. -spec terminate(terminate_reason(), gs_state()) -> ok.
  76. terminate(_Reason, _State) ->
  77. ok.
  78. -spec code_change(term(), term(), term()) -> gs_code_change_reply().
  79. code_change(_OldVersion, State, _Extra) ->
  80. {ok, State}.