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

@ -29,15 +29,20 @@ do_search(PoolName, Version, IpBin) ->
case ets:lookup(CacheTable, IpBin) of case ets:lookup(CacheTable, IpBin) of
[{_, Region}] -> Region; [{_, Region}] -> Region;
_ -> _ ->
Worker = poolboy:checkout(PoolName, true, infinity), case whereis(PoolName) of
try undefined ->
case ip2region_worker:search(Worker, IpBin) of {error, pool_not_configured};
{error, _} = Err -> Err; _ ->
Region -> Worker = poolboy:checkout(PoolName, true, infinity),
ets:insert(CacheTable, {IpBin, Region}), try
Region case ip2region_worker:search(Worker, IpBin) of
end {error, _} = Err -> Err;
after Region ->
poolboy:checkin(PoolName, Worker) ets:insert(CacheTable, {IpBin, Region}),
Region
end
after
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,