Add AACI caching

This commit is contained in:
2025-12-18 09:03:21 +09:00
parent c713053efd
commit aeb78eab38
2 changed files with 63 additions and 4 deletions
+36 -1
View File
@@ -20,6 +20,9 @@
chain_nodes/0, chain_nodes/1,
timeout/0, timeout/1]).
%% Contract caching
-export([cache_aaci/2, lookup_aaci/1]).
%% The whole point of this module:
-export([request_sticky/1, request_sticky/2, request/1, request/2]).
@@ -44,7 +47,8 @@
chain_nodes = {[], []} :: {[hz:chain_node()], [hz:chain_node()]},
sticky = none :: none | hz:chain_node(),
fetchers = [] :: [#fetcher{}],
timeout = 5000 :: pos_integer()}).
timeout = 5000 :: pos_integer(),
cache = #{} :: #{Label :: term() := AACI :: hz:aaci()}}).
-type state() :: #s{}.
@@ -94,6 +98,22 @@ timeout(Value) when 0 < Value, Value =< 120000 ->
gen_server:cast(?MODULE, {timeout, Value}).
-spec cache_aaci(Label, AACI) -> ok
when Label :: term(),
AACI :: hz:aaci().
cache_aaci(Label, AACI) ->
gen_server:call(?MODULE, {cache, Label, AACI}).
-spec lookup_aaci(Label) -> Result
when Label :: term(),
Result :: {ok, hz:aaci()} | error.
lookup_aaci(Label) ->
gen_server:call(?MODULE, {lookup, Label}).
-spec request_sticky(Path) -> {ok, Value} | {error, Reason}
when Path :: unicode:charlist(),
Value :: map(),
@@ -167,6 +187,12 @@ handle_call({request, Request}, From, State) ->
handle_call({request_sticky, Request}, From, State) ->
NewState = do_request_sticky(Request, From, State),
{noreply, NewState};
handle_call({lookup, Label}, _, State) ->
Result = do_lookup(Label, State),
{reply, Result, State};
handle_call({cache, Label, AACI}, _, State) ->
NewState = do_cache_aaci(Label, AACI, State),
{reply, ok, NewState};
handle_call(tls, _, State = #s{tls = TLS}) ->
{reply, TLS, State};
handle_call(chain_nodes, _, State) ->
@@ -265,6 +291,15 @@ do_tls(_, State) ->
State.
do_cache_aaci(Label, AACI, State = #s{cache = Cache}) ->
NewCache = maps:put(Label, AACI, Cache),
State#s{cache = NewCache}.
do_lookup(Label, #s{cache = Cache}) ->
maps:find(Label, Cache).
do_request_sticky(_, From, State = #s{sticky = none}) ->
ok = gen_server:reply(From, {error, no_nodes}),
State;