You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/site/gate_keeper.ex

79 lines
2.5 KiB
Elixir

defmodule Plausible.Site.GateKeeper do
@type policy() :: :allow | :not_found | :block | :throttle | :payment_required
@policy_for_non_existing_sites :not_found
@type t() :: {:allow, Plausible.Site.t()} | {:deny, policy()}
@moduledoc """
Thin wrapper around `Plausible.RateLimit` for gate keeping domain-specific events
during the ingestion phase. When the site is allowed, gate keeping
check returns `:allow`, otherwise a `:deny` tagged tuple is returned
with one of the following policy markers:
* `:not_found` (indicates site not found in cache)
* `:block` (indicates disabled sites)
* `:throttle` (indicates rate limiting)
Rate Limiting buckets are configured per site (externally via the CRM).
See: `Plausible.Site`
To look up each site's configuration, the RateLimiter fetches
a Site by domain using `Plausible.Cache` interface.
The module defines two policies outside the regular bucket inspection:
* when the site is not found in cache: #{@policy_for_non_existing_sites}
* when the underlying rate limiting mechanism returns
Conditionally support switching between v1 and v2 clickhouse schemas (#2780) * Remove ClickhouseSetup module This has been an implicit point of contact to many tests. From now on the goal is for each test to maintain its own, isolated setup so that no accidental clashes and implicit assumptions are relied upon. * Implement v2 schema check An environment variable V2_MIGRATION_DONE acts like a feature flag, switching plausible from using old events/sessions schemas to v2 schemas introduced by NumericIDs migration. * Run both test suites sequentially While the code for v1 and v2 schemas must be kept still, we will from now on run tests against both code paths. Secondary test run will set V2_MIGRATION_DONE=1 variable, thus making all `Plausible.v2?()` checks return `true'. * Remove unused function This is a remnant from the short period when we would check for existing events before allowing creating a new site. * Update test setups/factories with v2 migration check * Make GateKeeper return site id along with :allow * Make Billing module check for v2 schema * Make ingestion aware of v2 schema * Disable site transfers for when v2 is live In a separate changeset we will implement simplified site transfer for when v2 migration is complete. The new transfer will only rename the site domain in postgres and keep track of the original site prior to the transfer so we keep an ingestion grace period until the customers redeploy their scripting. * Make Stats base queries aware of v2 schema switch * Update breakdown with v2 conditionals * Update pageview local start with v2 check * Update current visitoris with v2 check * Update stats controller with v2 checks * Update external controller with v2 checks * Update remaining tests with proper fixtures * Rewrite redundant assignment * Remove unused alias * Mute credo, this is not the right time * Add test_helper prompt * Fetch priv dir so it works with a release * Fetch distinct partitions only * Don't limit inspect output for partitions * Ensure SQL is printed to IO * Remove redundant domain fixture
4 years ago
an internal error: :allow
"""
alias Plausible.{Site, RateLimit}
alias Plausible.Site.Cache
require Logger
@spec check(String.t(), Keyword.t()) :: t()
def check(domain, opts \\ []) when is_binary(domain) do
case policy(domain, opts) do
{:allow, site} -> {:allow, site}
other -> {:deny, other}
end
end
@spec key(String.t()) :: String.t()
def key(domain) do
"ingest:site:#{domain}"
end
defp policy(domain, opts) when is_binary(domain) do
with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])),
site = %Site{owner: %{accept_traffic_until: accept_traffic_until}} <- from_cache do
if not is_nil(accept_traffic_until) and
Date.after?(Date.utc_today(), accept_traffic_until) do
:payment_required
else
check_rate_limit(site, opts)
end
else
_ ->
@policy_for_non_existing_sites
end
end
defp check_rate_limit(%Site{ingest_rate_limit_threshold: nil} = site, _opts) do
{:allow, site}
end
defp check_rate_limit(%Site{ingest_rate_limit_threshold: 0}, _opts) do
:block
end
defp check_rate_limit(%Site{ingest_rate_limit_threshold: threshold} = site, opts)
when is_integer(threshold) do
key = Keyword.get(opts, :key, key(site.domain))
scale_ms = site.ingest_rate_limit_scale_seconds * 1_000
case RateLimit.check_rate(key, scale_ms, threshold) do
{:deny, _} -> :throttle
{:allow, _} -> {:allow, site}
end
end
end