Blockchain explorer for Ethereum based network and a tool for inspecting and analyzing EVM based blockchains.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
blockscout/lib/explorer/servers/chain_statistics.ex

37 lines
871 B

defmodule Explorer.Servers.ChainStatistics do
@moduledoc "Stores the latest chain statistics."
use GenServer
alias Explorer.Chain
@interval 1_000
def fetch do
case GenServer.whereis(__MODULE__) do
nil -> Chain.fetch()
_ -> GenServer.call(__MODULE__, :fetch)
end
end
def start_link, do: start_link(nil)
def start_link(_), do: GenServer.start_link(__MODULE__, nil, name: __MODULE__)
def init(_) do
refresh()
{:ok, %Chain{}}
end
def refresh, do: refresh(@interval)
def refresh(interval), do: Process.send_after(self(), :refresh, interval)
def handle_info(:refresh, _) do
chain = Chain.fetch()
refresh()
{:noreply, chain}
end
def handle_info(_, tasks), do: {:noreply, tasks}
def handle_call(:fetch, _, chain), do: {:reply, chain, chain}
def handle_call(_, _, chain), do: {:noreply, chain}
end