fix(erlang): guard xdb:search against unconfigured pool

This commit is contained in:
Alice39s 2026-06-28 05:10:04 +09:00
parent 44013251b6
commit d1dda93abd
No known key found for this signature in database
GPG Key ID: 0E0AA11C3939DDDD
2 changed files with 23 additions and 10 deletions

View File

@ -28,6 +28,10 @@ do_search(PoolName, Version, IpBin) ->
CacheTable = ip2region_xdb:cache_table(Version), CacheTable = ip2region_xdb:cache_table(Version),
case ets:lookup(CacheTable, IpBin) of case ets:lookup(CacheTable, IpBin) of
[{_, Region}] -> Region; [{_, Region}] -> Region;
_ ->
case whereis(PoolName) of
undefined ->
{error, pool_not_configured};
_ -> _ ->
Worker = poolboy:checkout(PoolName, true, infinity), Worker = poolboy:checkout(PoolName, true, infinity),
try try
@ -40,4 +44,5 @@ do_search(PoolName, Version, IpBin) ->
after after
poolboy:checkin(PoolName, Worker) poolboy:checkin(PoolName, Worker)
end end
end
end. end.

View File

@ -34,6 +34,14 @@ invalid_search_test_() ->
?_assertEqual({error, bad_ip_format}, xdb:search({1,2,3})) ?_assertEqual({error, bad_ip_format}, xdb:search({1,2,3}))
]. ].
ipv6_pool_not_configured_test() ->
try application:stop(ip2region) catch _:_ -> ok end,
try application:unload(ip2region) catch _:_ -> ok end,
ok = application:load(ip2region),
ok = application:set_env(ip2region, db, [{ipv4, "ip2region.xdb"}]),
{ok, _} = application:ensure_all_started(ip2region),
?assertEqual({error, pool_not_configured}, xdb:search("2001:4860:4860::8888")).
setup_ipv6() -> setup_ipv6() ->
try application:stop(ip2region) catch _:_ -> ok end, try application:stop(ip2region) catch _:_ -> ok end,
try application:unload(ip2region) catch _:_ -> ok end, try application:unload(ip2region) catch _:_ -> ok end,