Merge pull request #3569 from poanetwork/vb-cache-period-vars

Allow re-define cache period vars at runtime
pull/3603/head
Victor Baranov 4 years ago committed by GitHub
commit 46a4bb93a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 72
      apps/explorer/config/config.exs
  3. 11
      apps/explorer/lib/explorer/chain/cache/token_exchange_rate.ex
  4. 11
      apps/explorer/lib/explorer/counters/address_gas_usage_counter.ex
  5. 11
      apps/explorer/lib/explorer/counters/address_tokens_usd_sum.ex
  6. 11
      apps/explorer/lib/explorer/counters/address_transactions_counter.ex
  7. 15
      apps/explorer/lib/explorer/counters/average_block_time.ex
  8. 11
      apps/explorer/lib/explorer/counters/token_holders_counter.ex
  9. 11
      apps/explorer/lib/explorer/counters/token_transfers_counter.ex
  10. 11
      apps/explorer/lib/explorer/market/market_history_cache.ex

@ -13,6 +13,7 @@
### Chore ### Chore
- [#3585](https://github.com/poanetwork/blockscout/pull/3585) - Add autoswitching from eth_subscribe to eth_blockNumber in Staking DApp - [#3585](https://github.com/poanetwork/blockscout/pull/3585) - Add autoswitching from eth_subscribe to eth_blockNumber in Staking DApp
- [#3574](https://github.com/poanetwork/blockscout/pull/3574) - Correct UNI token price - [#3574](https://github.com/poanetwork/blockscout/pull/3574) - Correct UNI token price
- [#3569](https://github.com/poanetwork/blockscout/pull/3569) - Allow re-define cache period vars at runtime
- [#3567](https://github.com/poanetwork/blockscout/pull/3567) - Force to show filter at the page where filtered items list is empty - [#3567](https://github.com/poanetwork/blockscout/pull/3567) - Force to show filter at the page where filtered items list is empty
- [#3565](https://github.com/poanetwork/blockscout/pull/3565) - Staking dapp: unhealthy state alert message - [#3565](https://github.com/poanetwork/blockscout/pull/3565) - Staking dapp: unhealthy state alert message

@ -23,15 +23,7 @@ config :explorer,
else: Explorer.Chain.Events.DBSender else: Explorer.Chain.Events.DBSender
) )
average_block_period = config :explorer, Explorer.Counters.AverageBlockTime, enabled: true
case Integer.parse(System.get_env("AVERAGE_BLOCK_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.minutes(30)
end
config :explorer, Explorer.Counters.AverageBlockTime,
enabled: true,
period: average_block_period
config :explorer, Explorer.Chain.Events.Listener, config :explorer, Explorer.Chain.Events.Listener,
enabled: enabled:
@ -88,71 +80,29 @@ config :explorer, Explorer.Counters.AddressesCounter,
enable_consolidation: true, enable_consolidation: true,
update_interval_in_seconds: balances_update_interval || 30 * 60 update_interval_in_seconds: balances_update_interval || 30 * 60
address_transactions_counter_cache_period =
case Integer.parse(System.get_env("ADDRESS_TRANSACTIONS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
address_transactions_gas_usage_counter_cache_period =
case Integer.parse(System.get_env("ADDRESS_TRANSACTIONS_GAS_USAGE_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
config :explorer, Explorer.Counters.AddressTransactionsGasUsageCounter, config :explorer, Explorer.Counters.AddressTransactionsGasUsageCounter,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: address_transactions_gas_usage_counter_cache_period
address_tokens_usd_sum_cache_period =
case Integer.parse(System.get_env("ADDRESS_TOKENS_USD_SUM_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
config :explorer, Explorer.Counters.AddressTokenUsdSum, config :explorer, Explorer.Counters.AddressTokenUsdSum,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: address_tokens_usd_sum_cache_period
token_exchange_rate_cache_period =
case Integer.parse(System.get_env("TOKEN_EXCHANGE_RATE_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
config :explorer, Explorer.Chain.Cache.TokenExchangeRate, config :explorer, Explorer.Chain.Cache.TokenExchangeRate,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: token_exchange_rate_cache_period
token_holders_counter_cache_period =
case Integer.parse(System.get_env("TOKEN_HOLDERS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
config :explorer, Explorer.Counters.TokenHoldersCounter, config :explorer, Explorer.Counters.TokenHoldersCounter,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: token_holders_counter_cache_period
token_transfers_counter_cache_period =
case Integer.parse(System.get_env("TOKEN_TRANSFERS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
config :explorer, Explorer.Counters.TokenTransfersCounter, config :explorer, Explorer.Counters.TokenTransfersCounter,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: token_transfers_counter_cache_period
config :explorer, Explorer.Counters.AddressTransactionsCounter, config :explorer, Explorer.Counters.AddressTransactionsCounter,
enabled: true, enabled: true,
enable_consolidation: true, enable_consolidation: true
period: address_transactions_counter_cache_period
bridge_market_cap_update_interval = bridge_market_cap_update_interval =
if System.get_env("BRIDGE_MARKET_CAP_UPDATE_INTERVAL") do if System.get_env("BRIDGE_MARKET_CAP_UPDATE_INTERVAL") do
@ -263,14 +213,6 @@ config :spandex_ecto, SpandexEcto.EctoLogger,
tracer: Explorer.Tracer, tracer: Explorer.Tracer,
otp_app: :explorer otp_app: :explorer
market_history_cache_period =
case Integer.parse(System.get_env("MARKET_HISTORY_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(6)
end
config :explorer, Explorer.Market.MarketHistoryCache, period: market_history_cache_period
config :explorer, Explorer.Chain.Cache.Blocks, config :explorer, Explorer.Chain.Cache.Blocks,
ttl_check_interval: if(System.get_env("DISABLE_INDEXER") == "true", do: :timer.seconds(1), else: false), ttl_check_interval: if(System.get_env("DISABLE_INDEXER") == "true", do: :timer.seconds(1), else: false),
global_ttl: if(System.get_env("DISABLE_INDEXER") == "true", do: :timer.seconds(5)) global_ttl: if(System.get_env("DISABLE_INDEXER") == "true", do: :timer.seconds(5))

@ -8,7 +8,6 @@ defmodule Explorer.Chain.Cache.TokenExchangeRate do
@cache_name :token_exchange_rate @cache_name :token_exchange_rate
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -64,11 +63,12 @@ defmodule Explorer.Chain.Cache.TokenExchangeRate do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(symbol) do defp cache_expired?(symbol) do
cache_period = token_exchange_rate_cache_period()
updated_at = fetch_from_cache("#{cache_key(symbol)}_#{@last_update_key}") updated_at = fetch_from_cache("#{cache_key(symbol)}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -129,4 +129,11 @@ defmodule Explorer.Chain.Cache.TokenExchangeRate do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp token_exchange_rate_cache_period do
case Integer.parse(System.get_env("TOKEN_EXCHANGE_RATE_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -8,7 +8,6 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do
@cache_name :address_transactions_gas_usage_counter @cache_name :address_transactions_gas_usage_counter
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -61,12 +60,13 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address) do defp cache_expired?(address) do
cache_period = address_transactions_gas_usage_counter_cache_period()
address_hash_string = get_address_hash_string(address) address_hash_string = get_address_hash_string(address)
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -109,4 +109,11 @@ defmodule Explorer.Counters.AddressTransactionsGasUsageCounter do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp address_transactions_gas_usage_counter_cache_period do
case Integer.parse(System.get_env("ADDRESS_TRANSACTIONS_GAS_USAGE_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -8,7 +8,6 @@ defmodule Explorer.Counters.AddressTokenUsdSum do
@cache_name :address_tokens_usd_value @cache_name :address_tokens_usd_value
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -61,12 +60,13 @@ defmodule Explorer.Counters.AddressTokenUsdSum do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address_hash) do defp cache_expired?(address_hash) do
cache_period = address_tokens_usd_sum_cache_period()
address_hash_string = get_address_hash_string(address_hash) address_hash_string = get_address_hash_string(address_hash)
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -109,4 +109,11 @@ defmodule Explorer.Counters.AddressTokenUsdSum do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp address_tokens_usd_sum_cache_period do
case Integer.parse(System.get_env("ADDRESS_TOKENS_USD_SUM_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -8,7 +8,6 @@ defmodule Explorer.Counters.AddressTransactionsCounter do
@cache_name :address_transactions_counter @cache_name :address_transactions_counter
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -61,12 +60,13 @@ defmodule Explorer.Counters.AddressTransactionsCounter do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address) do defp cache_expired?(address) do
cache_period = address_transactions_counter_cache_period()
address_hash_string = get_address_hash_string(address) address_hash_string = get_address_hash_string(address)
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -109,4 +109,11 @@ defmodule Explorer.Counters.AddressTransactionsCounter do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp address_transactions_counter_cache_period do
case Integer.parse(System.get_env("ADDRESS_TRANSACTIONS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -11,8 +11,6 @@ defmodule Explorer.Counters.AverageBlockTime do
alias Explorer.Repo alias Explorer.Repo
alias Timex.Duration alias Timex.Duration
@refresh_period Application.compile_env(:explorer, __MODULE__)[:period]
@doc """ @doc """
Starts a process to periodically update the counter of the token holders. Starts a process to periodically update the counter of the token holders.
""" """
@ -41,7 +39,8 @@ defmodule Explorer.Counters.AverageBlockTime do
## Server ## Server
@impl true @impl true
def init(_) do def init(_) do
Process.send_after(self(), :refresh_timestamps, @refresh_period) refresh_period = average_block_cache_period()
Process.send_after(self(), :refresh_timestamps, refresh_period)
{:ok, refresh_timestamps()} {:ok, refresh_timestamps()}
end end
@ -56,7 +55,8 @@ defmodule Explorer.Counters.AverageBlockTime do
@impl true @impl true
def handle_info(:refresh_timestamps, _) do def handle_info(:refresh_timestamps, _) do
Process.send_after(self(), :refresh_timestamps, @refresh_period) refresh_period = Application.get_env(:explorer, __MODULE__)[:period]
Process.send_after(self(), :refresh_timestamps, refresh_period)
{:noreply, refresh_timestamps()} {:noreply, refresh_timestamps()}
end end
@ -128,4 +128,11 @@ defmodule Explorer.Counters.AverageBlockTime do
end) end)
|> elem(0) |> elem(0)
end end
defp average_block_cache_period do
case Integer.parse(System.get_env("AVERAGE_BLOCK_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.minutes(30)
end
end
end end

@ -8,7 +8,6 @@ defmodule Explorer.Counters.TokenHoldersCounter do
@cache_name :token_holders_counter @cache_name :token_holders_counter
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -61,12 +60,13 @@ defmodule Explorer.Counters.TokenHoldersCounter do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address_hash) do defp cache_expired?(address_hash) do
cache_period = token_holders_counter_cache_period()
address_hash_string = get_address_hash_string(address_hash) address_hash_string = get_address_hash_string(address_hash)
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -109,4 +109,11 @@ defmodule Explorer.Counters.TokenHoldersCounter do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp token_holders_counter_cache_period do
case Integer.parse(System.get_env("TOKEN_HOLDERS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -8,7 +8,6 @@ defmodule Explorer.Counters.TokenTransfersCounter do
@cache_name :token_holders_counter @cache_name :token_holders_counter
@last_update_key "last_update" @last_update_key "last_update"
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@ets_opts [ @ets_opts [
:set, :set,
@ -61,12 +60,13 @@ defmodule Explorer.Counters.TokenTransfersCounter do
def cache_name, do: @cache_name def cache_name, do: @cache_name
defp cache_expired?(address_hash) do defp cache_expired?(address_hash) do
cache_period = token_transfers_counter_cache_period()
address_hash_string = get_address_hash_string(address_hash) address_hash_string = get_address_hash_string(address_hash)
updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}") updated_at = fetch_from_cache("hash_#{address_hash_string}_#{@last_update_key}")
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -109,4 +109,11 @@ defmodule Explorer.Counters.TokenTransfersCounter do
end end
def enable_consolidation?, do: @enable_consolidation def enable_consolidation?, do: @enable_consolidation
defp token_transfers_counter_cache_period do
case Integer.parse(System.get_env("TOKEN_TRANSFERS_COUNTER_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(1)
end
end
end end

@ -12,7 +12,6 @@ defmodule Explorer.Market.MarketHistoryCache do
@last_update_key :last_update @last_update_key :last_update
@history_key :history @history_key :history
# 6 hours # 6 hours
@cache_period Application.compile_env(:explorer, __MODULE__)[:period]
@recent_days 30 @recent_days 30
def fetch do def fetch do
@ -32,11 +31,12 @@ defmodule Explorer.Market.MarketHistoryCache do
def recent_days_count, do: @recent_days def recent_days_count, do: @recent_days
defp cache_expired? do defp cache_expired? do
cache_period = market_history_cache_period()
updated_at = fetch_from_cache(@last_update_key) updated_at = fetch_from_cache(@last_update_key)
cond do cond do
is_nil(updated_at) -> true is_nil(updated_at) -> true
current_time() - updated_at > @cache_period -> true current_time() - updated_at > cache_period -> true
true -> false true -> false
end end
end end
@ -76,4 +76,11 @@ defmodule Explorer.Market.MarketHistoryCache do
DateTime.to_unix(utc_now, :millisecond) DateTime.to_unix(utc_now, :millisecond)
end end
defp market_history_cache_period do
case Integer.parse(System.get_env("MARKET_HISTORY_CACHE_PERIOD", "")) do
{secs, ""} -> :timer.seconds(secs)
_ -> :timer.hours(6)
end
end
end end

Loading…
Cancel
Save