|
|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
defmodule Plausible.Site.GateKeeper do
|
|
|
|
|
@type policy() :: :allow | :not_found | :block | :throttle
|
|
|
|
|
@type policy() :: :allow | :not_found | :block | :throttle | :payment_required
|
|
|
|
|
@policy_for_non_existing_sites :not_found
|
|
|
|
|
|
|
|
|
|
@type t() :: {:allow, Plausible.Site.t()} | {:deny, policy()}
|
|
|
|
|
@ -43,16 +43,18 @@ defmodule Plausible.Site.GateKeeper do
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp policy(domain, opts) when is_binary(domain) do
|
|
|
|
|
result =
|
|
|
|
|
case Cache.get(domain, Keyword.get(opts, :cache_opts, [])) do
|
|
|
|
|
nil ->
|
|
|
|
|
@policy_for_non_existing_sites
|
|
|
|
|
|
|
|
|
|
%Site{} = site ->
|
|
|
|
|
check_rate_limit(site, opts)
|
|
|
|
|
with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])),
|
|
|
|
|
site = %Site{accept_traffic_until: accept_traffic_until} <- from_cache do
|
|
|
|
|
if not is_nil(accept_traffic_until) and
|
|
|
|
|
NaiveDateTime.after?(NaiveDateTime.utc_now(), accept_traffic_until) do
|
|
|
|
|
:payment_required
|
|
|
|
|
else
|
|
|
|
|
check_rate_limit(site, opts)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
else
|
|
|
|
|
_ ->
|
|
|
|
|
@policy_for_non_existing_sites
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp check_rate_limit(%Site{ingest_rate_limit_threshold: nil} = site, _opts) do
|
|
|
|
|
|