Network specific cache filename + more sanity checks

This commit is contained in:
Ulf Wiger
2025-10-13 13:57:34 +02:00
parent cb0d8f3689
commit 570f31ab3c
4 changed files with 59 additions and 18 deletions
+43 -16
View File
@@ -16,7 +16,8 @@ get_pool_address() ->
end.
cached_address() ->
CacheF = cache_filename(),
I0 = cache_info(),
CacheF = cache_filename(I0),
?LOG_DEBUG("Eureka cache filename: ~p", [CacheF]),
case file:read_file(CacheF) of
{ok, Bin} ->
@@ -24,9 +25,12 @@ cached_address() ->
OldestTS = NowTS - 24*60*60,
try binary_to_term(Bin) of
#{ ts := TS
, network := N
, pubkey := PK
, host := _
, port := _
, pool_id := _} = Map ->
, pool_id := _} = Map when N == map_get(network, I0),
PK == map_get(pubkey, I0) ->
if TS >= OldestTS ->
Result = maps:remove(ts, Map),
?LOG_DEBUG("Cached eureka info: ~p", [Result]),
@@ -44,20 +48,25 @@ cached_address() ->
Err
end.
cache_good_address(#{host := Addr,
port := Port,
pool_id := PoolId}) ->
CacheF = cache_filename(),
ToCache = #{ host => unicode:characters_to_binary(Addr)
, port => Port
, pool_id => unicode:characters_to_binary(PoolId)
, ts => erlang:system_time(seconds)},
case file:write_file(CacheF, term_to_binary(ToCache)) of
cache_good_address(#{host := _,
port := _,
pool_id := _} = I0) ->
CacheInfo = cache_info(I0),
CacheF = cache_filename(CacheInfo),
ToCache = CacheInfo#{ts => erlang:system_time(seconds)},
case filelib:ensure_dir(CacheF) of
ok ->
?LOG_DEBUG("Cached eureka info in: ~p", [CacheF]),
{ok, ToCache};
case file:write_file(CacheF, term_to_binary(ToCache)) of
ok ->
?LOG_DEBUG("Cached eureka info in: ~p", [CacheF]),
{ok, ToCache};
{error, _} = Err ->
?LOG_DEBUG("Couldn't cache eureka in ~p: ~p", [CacheF, Err]),
Err
end;
{error, _} = Err ->
?LOG_DEBUG("Couldn't cache eureka in ~p: ~p", [CacheF, Err]),
?LOG_ERROR("Cannot save cached info to ~s", [CacheF]),
Err
end.
@@ -72,9 +81,27 @@ invalidate_cache() ->
Err
end.
cache_info(#{ host := Addr
, port := Port
, pool_id := PoolId }) ->
I0 = cache_info(),
I0#{ host => unicode:characters_to_binary(Addr)
, port => Port
, pool_id => unicode:characters_to_binary(PoolId)}.
cache_info() ->
Pubkey = gmhc_config:get_config([<<"pubkey">>]),
Network = gmhc_config:get_config([<<"network">>]),
#{ pubkey => Pubkey
, network => Network }.
cache_filename() ->
<<"ak_", PKShort:8/binary, _/binary>> = gmhc_config:get_config([<<"pubkey">>]),
filename:join(setup:data_dir(), "gmhc_eureka." ++ binary_to_list(PKShort) ++ ".cache").
cache_filename(cache_info()).
cache_filename(#{network := Network, pubkey := Pubkey}) ->
Path = filename:join(setup:data_dir(), Network),
<<"ak_", PKShort:8/binary, _/binary>> = Pubkey,
filename:join(Path, "gmhc_eureka." ++ binary_to_list(PKShort) ++ ".cache").
get_pool_address_() ->
case gmconfig:find_config([<<"pool_admin">>, <<"url">>], [user_config]) of