feat(erlang): make worker xdb version-aware for IPv4/IPv6

This commit is contained in:
Alice39s 2026-06-28 00:01:09 +09:00
parent 2f0dc5f33f
commit 241831a6f0
No known key found for this signature in database
GPG Key ID: 0E0AA11C3939DDDD
3 changed files with 156 additions and 61 deletions

View File

@ -42,9 +42,23 @@ init([]) ->
%% %%
create_table() -> create_table() ->
Opts = [named_table, set, public, {read_concurrency, true}, {keypos, 1}], Opts = [named_table, set, public, {read_concurrency, true}, {keypos, 1}],
ets:new(?XDB_VECTOR_INDEX, Opts), %% Legacy tables (kept for backward compatibility)
ets:new(?XDB_SEGMENT_INDEX, Opts), ensure_table(?XDB_VECTOR_INDEX, Opts),
ets:new(?IP2REGION_CACHE, Opts). ensure_table(?XDB_SEGMENT_INDEX, Opts),
ensure_table(?IP2REGION_CACHE, Opts),
%% Version-specific tables for dual-stack support
ensure_table(?XDB_VECTOR_INDEX_V4, Opts),
ensure_table(?XDB_VECTOR_INDEX_V6, Opts),
ensure_table(?XDB_SEGMENT_INDEX_V4, Opts),
ensure_table(?XDB_SEGMENT_INDEX_V6, Opts),
ensure_table(?IP2REGION_CACHE_V4, Opts),
ensure_table(?IP2REGION_CACHE_V6, Opts).
ensure_table(Name, Opts) ->
case ets:whereis(Name) of
undefined -> ets:new(Name, Opts);
_ -> ok
end.
start_ip2region_pool(Sup) -> start_ip2region_pool(Sup) ->
{ok, PoolArgsCfg} = application:get_env(poolargs), {ok, PoolArgsCfg} = application:get_env(poolargs),

View File

@ -2,9 +2,9 @@
%% Copyright 2022 The Ip2Region Authors. All rights reserved. %% Copyright 2022 The Ip2Region Authors. All rights reserved.
%% Use of this source code is governed by a Apache2.0-style %% Use of this source code is governed by a Apache2.0-style
%% license that can be found in the LICENSE file. %% license that can be found in the LICENSE file.
%% %%
%% @doc %% @doc
%% ip2region xdb client worker %% ip2region xdb client worker, now version-aware (IPv4/IPv6).
%% @end %% @end
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(ip2region_worker). -module(ip2region_worker).
@ -15,7 +15,12 @@
-export([start/1, stop/1, start_link/1]). -export([start/1, stop/1, start_link/1]).
-export([search/2]). -export([search/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, {xdb_fd}).
-record(state, {
xdb_fd,
version :: ipv4 | ipv6,
segment_index_size :: pos_integer()
}).
%%========================================== %%==========================================
%% API %% API
@ -28,7 +33,6 @@ start_link(Args) ->
Opts = [{spawn_opt, [{min_heap_size, 6000}]}], Opts = [{spawn_opt, [{min_heap_size, 6000}]}],
gen_server:start_link(?MODULE, Args, Opts). gen_server:start_link(?MODULE, Args, Opts).
stop(Pid) -> stop(Pid) ->
gen_server:call(Pid, stop). gen_server:call(Pid, stop).
@ -38,26 +42,42 @@ search(Pid, Ip) ->
%%========================================== %%==========================================
%% gen_server callbacks %% gen_server callbacks
%% ========================================= %% =========================================
init(_Args) -> init(Args) ->
process_flag(trap_exit, true), process_flag(trap_exit, true),
AppName = AppName =
case application:get_application() of case application:get_application() of
{ok, AName} -> AName; {ok, AName} -> AName;
_ -> ?APP_NAME _ -> ?APP_NAME
end, end,
PrivDir = code:priv_dir(AppName), PrivDir = code:priv_dir(AppName),
XdbFileName = filename:join([PrivDir, "ip2region.xdb"]), XdbFileName =
case proplists:get_value(xdb_file, Args) of
undefined -> filename:join([PrivDir, "ip2region.xdb"]);
Path ->
case filename:pathtype(Path) of
absolute -> Path;
_ -> filename:join([PrivDir, Path])
end
end,
error_logger:info_report(io_lib:format("XdbFile:~s~n", [XdbFileName])), error_logger:info_report(io_lib:format("XdbFile:~s~n", [XdbFileName])),
{ok, IoDevice} = file:open(XdbFileName, [read, binary]), {ok, IoDevice} = file:open(XdbFileName, [read, binary]),
load_vector_index(IoDevice), {ok, HeaderBin} = file:read(IoDevice, ?XDB_HEADER_SIZE),
{ok, #state{xdb_fd = IoDevice}}. {ok, Header} = ip2region_xdb:parse_header(HeaderBin),
Version = resolve_version(Header),
SegmentIndexSize = ip2region_xdb:segment_index_size(Version),
load_vector_index(IoDevice, Version),
{ok, #state{
xdb_fd = IoDevice,
version = Version,
segment_index_size = SegmentIndexSize
}}.
handle_call(Request, From, State) -> handle_call(Request, From, State) ->
try try
do_call(Request, From, State) do_call(Request, From, State)
catch catch
Class:Error:Stacktrace -> Class:Error:Stacktrace ->
error_logger:error_report(io_lib:format("~p handle call error, Req:~p ~p, stacktrace:~p~n", error_logger:error_report(io_lib:format("~p handle call error, Req:~p ~p, stacktrace:~p~n",
[?MODULE, Request, {Class, Error}, Stacktrace])), [?MODULE, Request, {Class, Error}, Stacktrace])),
{reply, {error, {Class, Error}}, State} {reply, {error, {Class, Error}}, State}
end. end.
@ -67,7 +87,7 @@ handle_cast(Msg, State) ->
do_cast(Msg, State) do_cast(Msg, State)
catch catch
Class:Error:Stacktrace -> Class:Error:Stacktrace ->
error_logger:error_report(io_lib:format("~p handle cast error, Msg:~p, ~p, stacktrace:~w~n", error_logger:error_report(io_lib:format("~p handle cast error, Msg:~p, ~p, stacktrace:~w~n",
[?MODULE, Msg, {Class, Error}, Stacktrace])), [?MODULE, Msg, {Class, Error}, Stacktrace])),
{noreply, State} {noreply, State}
end. end.
@ -77,7 +97,7 @@ handle_info(Info, State) ->
do_info(Info, State) do_info(Info, State)
catch catch
Class:Error:Stacktrace -> Class:Error:Stacktrace ->
error_logger:error_report(io_lib:format("~p handle info error, Info:~p, ~p, stacktrace:~p~n", error_logger:error_report(io_lib:format("~p handle info error, Info:~p, ~p, stacktrace:~p~n",
[?MODULE, Info, {Class, Error}, Stacktrace])), [?MODULE, Info, {Class, Error}, Stacktrace])),
{noreply, State} {noreply, State}
end. end.
@ -85,22 +105,19 @@ handle_info(Info, State) ->
terminate(_Reason, State) -> terminate(_Reason, State) ->
#state{xdb_fd = XdbFd} = State, #state{xdb_fd = XdbFd} = State,
case is_pid(XdbFd) of case is_pid(XdbFd) of
true -> true -> file:close(XdbFd);
file:close(XdbFd); _ -> skip
_ ->
skip
end, end,
ok. ok.
code_change(_OldVsn, State, _Extra) -> code_change(_OldVsn, State, _Extra) ->
{ok, State}. {ok, State}.
%%========================================== %%==========================================
%% Internal function %% Internal function
%% ========================================= %% =========================================
do_call({search, Ip}, _From, #state{xdb_fd = IoDevice} = State) -> do_call({search, Ip}, _From, #state{xdb_fd = IoDevice} = State) ->
Reply = search_ip(IoDevice, Ip), Reply = search_ip(IoDevice, Ip, State),
{reply, Reply, State}; {reply, Reply, State};
do_call(stop, _From, State) -> do_call(stop, _From, State) ->
@ -117,67 +134,118 @@ do_cast(Msg, State) ->
do_info(Info, State) -> do_info(Info, State) ->
error_logger:error_report(io:format("unknown info: ~p", [Info])), error_logger:error_report(io:format("unknown info: ~p", [Info])),
{noreply, State}. {noreply, State}.
load_vector_index(IoDevice) -> resolve_version(Header) ->
Key = ip2region_header_loaded, case ip2region_xdb:header_version(Header) of
case persistent_term:get(Key, false) of 2 -> ipv4;
true -> ok; 3 ->
_ -> case ip2region_xdb:header_ip_version(Header) of
{ok, <<_Header:?XDB_HEADER_SIZE/binary, VectorIndexBin/binary>> } = ?IP_VERSION_4 -> ipv4;
file:read(IoDevice, ?XDB_HEADER_SIZE + ?XDB_VECTOR_INDEX_COUNT*8), ?IP_VERSION_6 -> ipv6;
load_vector_index_aux(VectorIndexBin, 0), _ -> ipv4
persistent_term:put(Key, true) end;
_ -> ipv4
end. end.
load_vector_index_aux(<<>>, _Index) -> ok; load_vector_index(IoDevice, Version) ->
load_vector_index_aux(<<SPtr:32/little, EPtr:32/little, VectorIndexBin/binary>>, Index) -> Table = vector_index_table(Version),
Term = {Index, SPtr, EPtr}, case ets:info(Table, size) of
ets:insert(?XDB_VECTOR_INDEX, Term), undefined ->
load_vector_index_aux(VectorIndexBin, Index + 1). Opts = [named_table, set, public, {read_concurrency, true}, {keypos, 1}],
ets:new(Table, Opts),
load_vector_index_data(IoDevice, Table);
0 ->
load_vector_index_data(IoDevice, Table);
_ ->
ok
end.
load_vector_index_data(IoDevice, Table) ->
{ok, VectorIndexBin} =
file:read(IoDevice, ?XDB_VECTOR_INDEX_COUNT * 8),
load_vector_index_aux(VectorIndexBin, 0, Table).
search_ip(IoDevice, Ip) -> load_vector_index_aux(<<>>, _Index, _Table) -> ok;
IntIp = ip2region_util:ipv4_to_n(Ip), load_vector_index_aux(<<SPtr:32/little, EPtr:32/little, VectorIndexBin/binary>>, Index, Table) ->
case ets:lookup(?IP2REGION_CACHE, IntIp) of ets:insert(Table, {Index, SPtr, EPtr}),
[{_IntIp, RegionInfo}] -> load_vector_index_aux(VectorIndexBin, Index + 1, Table).
search_ip(IoDevice, IpInt, State) when is_integer(IpInt) ->
search_ip(IoDevice, <<IpInt:32>>, State);
search_ip(IoDevice, Ip, #state{version = Version, segment_index_size = SegSize}) ->
CacheTable = cache_table(Version),
VectorTable = vector_index_table(Version),
SegmentTable = segment_index_table(Version),
case ets:lookup(CacheTable, Ip) of
[{_, RegionInfo}] ->
RegionInfo; RegionInfo;
_ -> _ ->
<<A:8, B:8, _Rest/binary>> = <<IntIp:32>>, <<A:8, B:8, _/binary>> = Ip,
VectorIdx = A * ?XDB_VECTOR_COLS + B, VectorIdx = A * ?XDB_VECTOR_COLS + B,
[{_, SPtr, EPtr}] = ets:lookup(?XDB_VECTOR_INDEX, VectorIdx), [{_, SPtr, EPtr}] = ets:lookup(VectorTable, VectorIdx),
RegionInfo = search_ip(IoDevice, IntIp, SPtr, EPtr, 0, (EPtr - SPtr) div ?XDB_SEGMENT_INDEX_SIZE), RegionInfo = search_ip(IoDevice, Ip, SPtr, EPtr, 0,
ets:insert_new(?IP2REGION_CACHE, {IntIp, RegionInfo}), (EPtr - SPtr) div SegSize, SegSize, Version, SegmentTable),
ets:insert_new(CacheTable, {Ip, RegionInfo}),
RegionInfo RegionInfo
end. end.
search_ip(IoDevice, IntIp, SPtr, EPtr, Low, High) when Low =< High -> search_ip(IoDevice, Ip, SPtr, EPtr, Low, High, SegSize, Version, SegmentTable) when Low =< High ->
Middle = (Low + High) bsr 1, Middle = (Low + High) bsr 1,
SPtr2 = SPtr + Middle * ?XDB_SEGMENT_INDEX_SIZE, SPtr2 = SPtr + Middle * SegSize,
{SIp, EIp, DataLen, DataPtr} = read_segement_index(IoDevice, SPtr2), {SIp, EIp, DataLen, DataPtr} = read_segment_index(IoDevice, SPtr2, SegSize, SegmentTable),
if case ip_in_range(Ip, SIp, EIp, Version) of
IntIp < SIp -> below ->
search_ip(IoDevice, IntIp, SPtr, EPtr, Low, Middle - 1); search_ip(IoDevice, Ip, SPtr, EPtr, Low, Middle - 1, SegSize, Version, SegmentTable);
IntIp > EIp -> above ->
search_ip(IoDevice, IntIp, SPtr, EPtr, Middle + 1, High); search_ip(IoDevice, Ip, SPtr, EPtr, Middle + 1, High, SegSize, Version, SegmentTable);
true -> inside ->
{ok, DataBin} = read_file(IoDevice, DataPtr, DataLen), {ok, DataBin} = read_file(IoDevice, DataPtr, DataLen),
unicode:characters_to_nfc_list(DataBin) unicode:characters_to_nfc_list(DataBin)
end; end;
search_ip(_IoDevice, _IntIp, _SPtr, _EPtr, _Low, _High) -> search_ip(_IoDevice, _Ip, _SPtr, _EPtr, _Low, _High, _SegSize, _Version, _SegmentTable) ->
{error, unknown}. {error, unknown}.
ip_in_range(Ip, SIp, EIp, ipv4) ->
<<InputInt:32>> = Ip,
if
InputInt < SIp -> below;
InputInt > EIp -> above;
true -> inside
end;
ip_in_range(Ip, SIp, EIp, ipv6) ->
if
Ip < SIp -> below;
Ip > EIp -> above;
true -> inside
end.
read_file(IoDevice, Position, DataLength) -> read_file(IoDevice, Position, DataLength) ->
file:position(IoDevice, {bof, Position}), file:position(IoDevice, {bof, Position}),
file:read(IoDevice, DataLength). file:read(IoDevice, DataLength).
read_segement_index(IoDevice, SPtr) -> read_segment_index(IoDevice, SPtr, SegSize, SegmentTable) ->
case ets:lookup(?XDB_SEGMENT_INDEX, SPtr) of case ets:lookup(SegmentTable, SPtr) of
[{_SPtr, SIp, EIp, DataLen, DataPtr}] -> [{_SPtr, SIp, EIp, DataLen, DataPtr}] ->
{SIp, EIp, DataLen, DataPtr}; {SIp, EIp, DataLen, DataPtr};
_ -> _ ->
{ok, <<SIp:32/little, EIp:32/little, DataLen:16/little, DataPtr:32/little>>} = {ok, Bin} = read_file(IoDevice, SPtr, SegSize),
read_file(IoDevice, SPtr, ?XDB_SEGMENT_INDEX_SIZE), {SIp, EIp, DataLen, DataPtr} = decode_segment_index(Bin, SegSize),
ets:insert_new(?XDB_SEGMENT_INDEX, {SPtr, SIp, EIp, DataLen, DataPtr}), ets:insert_new(SegmentTable, {SPtr, SIp, EIp, DataLen, DataPtr}),
{SIp, EIp, DataLen, DataPtr} {SIp, EIp, DataLen, DataPtr}
end. end.
decode_segment_index(Bin, ?XDB_SEGMENT_INDEX_SIZE_V4) ->
<<SIp:32/little, EIp:32/little, DataLen:16/little, DataPtr:32/little>> = Bin,
{SIp, EIp, DataLen, DataPtr};
decode_segment_index(Bin, ?XDB_SEGMENT_INDEX_SIZE_V6) ->
<<SIp:16/binary, EIp:16/binary, DataLen:16/little, DataPtr:32/little>> = Bin,
{SIp, EIp, DataLen, DataPtr}.
vector_index_table(ipv4) -> ?XDB_VECTOR_INDEX_V4;
vector_index_table(ipv6) -> ?XDB_VECTOR_INDEX_V6.
segment_index_table(ipv4) -> ?XDB_SEGMENT_INDEX_V4;
segment_index_table(ipv6) -> ?XDB_SEGMENT_INDEX_V6.
cache_table(ipv4) -> ?IP2REGION_CACHE_V4;
cache_table(ipv6) -> ?IP2REGION_CACHE_V6.

View File

@ -0,0 +1,13 @@
-module(ip2region_worker_test).
-include_lib("eunit/include/eunit.hrl").
-include("ip2region.hrl").
worker_search_v4_binary_test() ->
ip2region_sup:create_table(),
{ok, Pid} = ip2region_worker:start_link([{xdb_file, "ip2region.xdb"}]),
try
Region = ip2region_worker:search(Pid, <<1,0,8,0>>),
?assert(is_list(Region))
after
ip2region_worker:stop(Pid)
end.