Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Change timeouts (#5799)
Artur Pata5b82979bfae4
diff --git a/extra/lib/plausible/installation_support/browserless_config.ex b/extra/lib/plausible/installation_support/browserless_config.exindex 4dcac95798..fa40fb645c 100644--- a/extra/lib/plausible/installation_support/browserless_config.ex+++ b/extra/lib/plausible/installation_support/browserless_config.ex@@ -8,8 +8,8 @@ defmodule Plausible.InstallationSupport.BrowserlessConfig docase status do# rate limit429 -> {:delay, 1000}- # timeout- 408 -> {:delay, 500}+ # unexpected but we've manually verified these requests can succeed on retry+ 400 -> {:delay, 500}# other errors_ -> falseenddiff --git a/extra/lib/plausible/installation_support/check.ex b/extra/lib/plausible/installation_support/check.exindex fea1f00430..6be4ee7faf 100644--- a/extra/lib/plausible/installation_support/check.ex+++ b/extra/lib/plausible/installation_support/check.ex@@ -13,6 +13,7 @@ defmodule Plausible.InstallationSupport.Check do"""@type state() :: Plausible.InstallationSupport.State.t()@callback report_progress_as() :: String.t()+ @callback timeout_ms() :: integer()@callback perform(state()) :: state()defmacro __using__(_) do@@ -24,15 +25,32 @@ defmodule Plausible.InstallationSupport.Check do@behaviour Plausible.InstallationSupport.Check+ def timeout_ms, do: 10_000++ defoverridable timeout_ms: 0+def perform_safe(state) do- perform(state)- catch- _, e ->- Logger.error(- "Error running check #{inspect(__MODULE__)} on #{state.url}: #{inspect(e)}"- )-- put_diagnostics(state, service_error: e)+ task =+ Task.async(fn ->+ try do+ perform(state)+ catch+ _, e ->+ Logger.error(+ "Error running check #{inspect(__MODULE__)} on #{state.url}: #{inspect(e)}"+ )++ put_diagnostics(state, service_error: e)+ end+ end)++ try do+ Task.await(task, timeout_ms())+ catch+ :exit, {:timeout, _} ->+ Task.shutdown(task, :brutal_kill)+ put_diagnostics(state, service_error: :check_timeout)+ endendendenddiff --git a/extra/lib/plausible/installation_support/checks/detection.ex b/extra/lib/plausible/installation_support/checks/detection.exindex d6d8875b3e..ab2aeaa559 100644--- a/extra/lib/plausible/installation_support/checks/detection.ex+++ b/extra/lib/plausible/installation_support/checks/detection.ex@@ -54,7 +54,7 @@ defmodule Plausible.InstallationSupport.Checks.Detection do"""# We define a timeout for the browserless endpoint call to avoid waiting too long for a response- @endpoint_timeout_ms 3_000+ @endpoint_timeout_ms 5_000# This timeout determines how long we wait for window.plausible to be initialized on the page, used for detecting whether v1 installed@plausible_window_check_timeout_ms 1_500@@ -62,6 +62,9 @@ defmodule Plausible.InstallationSupport.Checks.Detection do# To support browserless API being unavailable or overloaded, we retry the endpoint call if it doesn't return a successful response@max_retries 1+ @impl true+ def timeout_ms, do: 6_000+@impl truedef report_progress_as, do: "We're checking your site to recommend the best installation method"diff --git a/extra/lib/plausible/installation_support/checks/installation_v2.ex b/extra/lib/plausible/installation_support/checks/installation_v2.exindex 004e366895..7371679aed 100644--- a/extra/lib/plausible/installation_support/checks/installation_v2.ex+++ b/extra/lib/plausible/installation_support/checks/installation_v2.ex@@ -75,7 +75,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do@max_retries 1# We define a timeout for the browserless endpoint call to avoid waiting too long for a response- @endpoint_timeout_ms 10_000+ @endpoint_timeout_ms 15_000# This timeout determines how long we wait for window.plausible to be initialized on the page, including sending the test event@plausible_window_check_timeout_ms 4_000@@ -84,6 +84,9 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 do@max_attempts 2@timeout_between_attempts_ms 500+ @impl true+ def timeout_ms, do: 20_000+@impl truedef report_progress_as, do: "We're verifying that your visitors are being counted correctly"@@ -107,7 +110,8 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 doparams: %{timeout: @endpoint_timeout_ms},retry: &BrowserlessConfig.retry_browserless_request/2,retry_log_level: :warning,- max_retries: @max_retries+ max_retries: @max_retries,+ receive_timeout: @endpoint_timeout_ms + 2_000]extra_opts = Application.get_env(:plausible, __MODULE__)[:req_opts] || []diff --git a/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex b/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.exindex 2e39f987a4..81e9245e21 100644--- a/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex+++ b/extra/lib/plausible/installation_support/checks/installation_v2_cache_bust.ex@@ -15,6 +15,9 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2CacheBust doalias Plausible.InstallationSupportuse Plausible.InstallationSupport.Check+ @impl true+ def timeout_ms, do: 20_000+@impl truedef report_progress_as, do: "We're verifying that your visitors are being counted correctly"diff --git a/test/plausible/installation_support/check_test.exs b/test/plausible/installation_support/check_test.exsnew file mode 100644index 0000000000..71ab107811--- /dev/null+++ b/test/plausible/installation_support/check_test.exs@@ -0,0 +1,97 @@+defmodule Plausible.InstallationSupport.CheckTest do+ use Plausible+ use Plausible.DataCase, async: true++ @moduletag :ee_only++ on_ee do+ import ExUnit.CaptureLog+ import Plausible.AssertMatches+ alias Plausible.InstallationSupport.{State, Verification}++ @moduletag :capture_log++ test "a check that raises" do+ defmodule FaultyCheckRaise do+ use Plausible.InstallationSupport.Check++ @impl true+ def report_progress_as, do: "Faulty check"++ @impl true+ def perform(_), do: raise("boom")+ end++ state = %State{+ url: "https://example.com",+ report_to: self(),+ diagnostics: %Verification.Diagnostics{}+ }++ {result, log} =+ with_log(fn ->+ FaultyCheckRaise.perform_safe(state)+ end)++ assert log =~+ ~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckRaise on https://example.com: %RuntimeError{message: "boom"}|++ assert_matches %Verification.Diagnostics{service_error: %RuntimeError{message: "boom"}} =+ result.diagnostics+ end++ test "a check that throws" do+ defmodule FaultyCheckThrow do+ use Plausible.InstallationSupport.Check++ @impl true+ def report_progress_as, do: "Faulty check"++ @impl true+ def perform(_), do: :erlang.throw(:boom)+ end++ state = %State{+ url: "https://example.com",+ report_to: self(),+ diagnostics: %Verification.Diagnostics{}+ }++ {result, log} =+ with_log(fn ->+ FaultyCheckThrow.perform_safe(state)+ end)++ assert log =~+ ~s|Error running check Plausible.InstallationSupport.CheckTest.FaultyCheckThrow on https://example.com: :boom|++ assert_matches %Verification.Diagnostics{service_error: :boom} = result.diagnostics+ end++ test "a check that times out" do+ defmodule FaultyCheckTimeout do+ use Plausible.InstallationSupport.Check++ @impl true+ def timeout_ms, do: 100++ @impl true+ def report_progress_as, do: "Faulty check"++ @impl true+ def perform(_), do: :timer.sleep(500)+ end++ state = %State{+ url: "https://example.com",+ report_to: self(),+ diagnostics: %Verification.Diagnostics{}+ }++ result =+ FaultyCheckTimeout.perform_safe(state)++ assert_matches %Verification.Diagnostics{service_error: :check_timeout} = result.diagnostics+ end+ end+enddiff --git a/test/plausible/installation_support/legacy_verification/checks_test.exs b/test/plausible/installation_support/legacy_verification/checks_test.exsindex c8e0d5cb02..45e571bdca 100644--- a/test/plausible/installation_support/legacy_verification/checks_test.exs+++ b/test/plausible/installation_support/legacy_verification/checks_test.exs@@ -437,54 +437,6 @@ defmodule Plausible.InstallationSupport.LegacyVerification.ChecksTest do|> assert_error(@errors.no_snippet_wp)end- test "a check that raises" do- defmodule FaultyCheckRaise do- use Plausible.InstallationSupport.Check-- @impl true- def report_progress_as, do: "Faulty check"-- @impl true- def perform(_), do: raise("boom")- end-- {result, log} =- with_log(fn ->- run_checks(checks: [FaultyCheckRaise])- end)-- assert log =~- ~s|Error running check Plausible.InstallationSupport.LegacyVerification.ChecksTest.FaultyCheckRaise on https://example.com: %RuntimeError{message: "boom"}|-- result- |> LegacyVerification.Checks.interpret_diagnostics()- |> assert_error(@errors.unreachable, url: "https://example.com")- end-- test "a check that throws" do- defmodule FaultyCheckThrow do- use Plausible.InstallationSupport.Check-- @impl true- def report_progress_as, do: "Faulty check"-- @impl true- def perform(_), do: :erlang.throw(:boom)- end-- {result, log} =- with_log(fn ->- run_checks(checks: [FaultyCheckThrow])- end)-- assert log =~- ~s|Error running check Plausible.InstallationSupport.LegacyVerification.ChecksTest.FaultyCheckThrow on https://example.com: :boom|-- result- |> LegacyVerification.Checks.interpret_diagnostics()- |> assert_error(@errors.unreachable, url: "https://example.com")- end-test "disallowed via content-security-policy" dostub_fetch_body(fn conn ->conn