|
28 | 28 | -export([init/1, handle_call/3, handle_cast/2, handle_info/2, |
29 | 29 | terminate/2, code_change/3]). |
30 | 30 | -export([sock_opts/0, new_connection/2]). |
31 | | --export([get_port/0, get_ip/0]). |
| 31 | +-export([get_listeners/0]). |
32 | 32 | -record(state, {portnum}). |
33 | 33 |
|
34 | 34 | %% @doc Starts the PB listener |
@@ -88,38 +88,80 @@ new_connection(Socket, State) -> |
88 | 88 | ok = riak_api_pb_server:set_socket(Pid, Socket), |
89 | 89 | {ok, State}. |
90 | 90 |
|
| 91 | +get_listeners() -> |
| 92 | + DefaultListener = case {get_ip(), get_port()} of |
| 93 | + {undefined, _} -> []; |
| 94 | + {_, undefined} -> []; |
| 95 | + {IP, Port} -> [{IP, Port}] |
| 96 | + end, |
| 97 | + Listeners = app_helper:get_env(riak_api, pb, []) ++ DefaultListener, |
| 98 | + [ {I, P} || {I, P} <- Listeners ]. |
| 99 | + |
91 | 100 | %% @private |
92 | 101 | get_port() -> |
93 | | - Envs = [{riak_api, pb_port}, |
94 | | - {riak_kv, pb_port}], |
95 | | - case app_helper:try_envs(Envs) of |
96 | | - {riak_api, pb_port, Port} -> |
97 | | - Port; |
98 | | - {riak_kv, pb_port, Port} -> |
99 | | - lager:warning("The config riak_kv/pb_port has been" |
100 | | - " deprecated and will be removed. Use" |
101 | | - " riak_api/pb_port in the future."), |
102 | | - Port; |
103 | | - _ -> |
104 | | - lager:warning("The config riak_api/pb_port is missing," |
105 | | - " PB connections will be disabled."), |
106 | | - undefined |
| 102 | + case app_helper:get_env(riak_api, pb_port) of |
| 103 | + undefined -> |
| 104 | + undefined; |
| 105 | + Port -> |
| 106 | + lager:warning("The config riak_api/pb_port has been" |
| 107 | + " deprecated and will be removed. Use" |
| 108 | + " riak_api/pb (IP/Port pairs) in the future."), |
| 109 | + Port |
107 | 110 | end. |
108 | 111 |
|
109 | 112 | %% @private |
110 | 113 | get_ip() -> |
111 | | - Envs = [{riak_api, pb_ip}, |
112 | | - {riak_kv, pb_ip}], |
113 | | - case app_helper:try_envs(Envs) of |
114 | | - {riak_api, pb_ip, IP} -> |
115 | | - IP; |
116 | | - {riak_kv, pb_ip, IP} -> |
117 | | - lager:warning("The config riak_kv/pb_ip has been" |
118 | | - " deprecated and will be removed. Use" |
119 | | - " riak_api/pb_ip in the future."), |
120 | | - IP; |
121 | | - _ -> |
122 | | - lager:warning("The config riak_api/pb_ip is missing," |
123 | | - " PB connections will be disabled."), |
124 | | - undefined |
| 114 | + case app_helper:get_env(riak_api, pb_ip) of |
| 115 | + undefined -> |
| 116 | + undefined; |
| 117 | + IP -> |
| 118 | + lager:warning("The config riak_api/pb_ip has been" |
| 119 | + " deprecated and will be removed. Use" |
| 120 | + " riak_api/pb (IP/Port pairs) in the future."), |
| 121 | + IP |
125 | 122 | end. |
| 123 | + |
| 124 | +-ifdef(TEST). |
| 125 | +-include_lib("eunit/include/eunit.hrl"). |
| 126 | +-compile(export_all). |
| 127 | + |
| 128 | +listeners_test_() -> |
| 129 | + {foreach, |
| 130 | + fun() -> |
| 131 | + application:load(riak_api), |
| 132 | + app_helper:get_env(riak_api, pb, [{"127.0.0.1", 8087}]) |
| 133 | + end, |
| 134 | + fun(OldListeners) -> |
| 135 | + application:set_env(riak_api, pb, OldListeners), |
| 136 | + application:unset_env(riak_api, pb_ip), |
| 137 | + application:unset_env(riak_api, pb_port) |
| 138 | + end, |
| 139 | + [ |
| 140 | + {"old config keys get upgraded", |
| 141 | + fun() -> |
| 142 | + application:unset_env(riak_api, pb), |
| 143 | + application:set_env(riak_api, pb_ip, "127.0.0.1"), |
| 144 | + application:set_env(riak_api, pb_port, 10887), |
| 145 | + ?assertEqual([{"127.0.0.1", 10887}], get_listeners()) |
| 146 | + end}, |
| 147 | + {"missing old IP config key disables listener", |
| 148 | + fun() -> |
| 149 | + application:unset_env(riak_api, pb), |
| 150 | + %% application:set_env(riak_api, pb_ip, "127.0.0.1"), |
| 151 | + application:set_env(riak_api, pb_port, 10887), |
| 152 | + ?assertEqual([], get_listeners()) |
| 153 | + end}, |
| 154 | + {"missing old Port config key disables listener", |
| 155 | + fun() -> |
| 156 | + application:unset_env(riak_api, pb), |
| 157 | + application:set_env(riak_api, pb_ip, "127.0.0.1"), |
| 158 | + %% application:set_env(riak_api, pb_port, 10887), |
| 159 | + ?assertEqual([], get_listeners()) |
| 160 | + end}, |
| 161 | + {"bad configs are ignored", |
| 162 | + fun() -> |
| 163 | + application:set_env(riak_api, pb, [{"0.0.0.0", 8087}, badjuju]), |
| 164 | + ?assertEqual([{"0.0.0.0", 8087}], get_listeners()) |
| 165 | + end}]}. |
| 166 | + |
| 167 | +-endif. |
0 commit comments