From f77c37800f99d9721e935618b03a6e78872864cc Mon Sep 17 00:00:00 2001
From: Uku Taht <uku.taht@gmail.com>
Date: Mon, 18 Oct 2021 11:27:21 +0200
Subject: [PATCH] Use safe version of Cachex.fetch

Fixes #905
---
 .../controllers/api/external_controller.ex          | 13 ++++++++++---
 mix.exs                                             |  2 +-
 mix.lock                                            |  2 +-
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex
index 874090d7..26ec39cc 100644
--- a/lib/plausible_web/controllers/api/external_controller.ex
+++ b/lib/plausible_web/controllers/api/external_controller.ex
@@ -50,9 +50,16 @@ defmodule PlausibleWeb.Api.ExternalController do
     user_agent = Plug.Conn.get_req_header(conn, "user-agent") |> List.first()
 
     if user_agent do
-      Cachex.fetch!(:user_agents, user_agent, fn ua ->
-        {:commit, UAInspector.parse(ua)}
-      end)
+      res =
+        Cachex.fetch(:user_agents, user_agent, fn ua ->
+          UAInspector.parse(ua)
+        end)
+
+      case res do
+        {:ok, user_agent} -> user_agent
+        {:commit, user_agent} -> user_agent
+        _ -> nil
+      end
     end
   end
 
diff --git a/mix.exs b/mix.exs
index 221a0b67..293e4689 100644
--- a/mix.exs
+++ b/mix.exs
@@ -90,7 +90,7 @@ defmodule Plausible.MixProject do
       {:clickhouse_ecto, git: "https://github.com/plausible/clickhouse_ecto.git"},
       {:geolix_adapter_mmdb2, "~> 0.5.0"},
       {:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
-      {:cachex, "~> 3.3"},
+      {:cachex, "~> 3.4"},
       {:dialyxir, "~> 1.0", only: [:dev, :test], runtime: false},
       {:credo, "~> 1.5", only: [:dev, :test], runtime: false},
       {:kaffy, "~> 0.9.0"},
diff --git a/mix.lock b/mix.lock
index 27c2b154..3f8cf0bf 100644
--- a/mix.lock
+++ b/mix.lock
@@ -10,7 +10,7 @@
   "bertex": {:hex, :bertex, "1.3.0", "0ad0df9159b5110d9d2b6654f72fbf42a54884ef43b6b651e6224c0af30ba3cb", [:mix], [], "hexpm", "0a5d5e478bb5764b7b7bae37cae1ca491200e58b089df121a2fe1c223d8ee57a"},
   "browser": {:hex, :browser, "0.4.4", "bd6436961a6b2299c6cb38d0e49761c1161d869cd0db46369cef2bf6b77c3665", [:mix], [{:plug, "~> 1.2", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d476ca309d4a4b19742b870380390aabbcb323c1f6f8745e2da2dfd079b4f8d7"},
   "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
-  "cachex": {:hex, :cachex, "3.3.0", "6f2ebb8f27491fe39121bd207c78badc499214d76c695658b19d6079beeca5c2", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "d90e5ee1dde14cef33f6b187af4335b88748b72b30c038969176cd4e6ccc31a1"},
+  "cachex": {:hex, :cachex, "3.4.0", "868b2959ea4aeb328c6b60ff66c8d5123c083466ad3c33d3d8b5f142e13101fb", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "370123b1ab4fba4d2965fb18f87fd758325709787c8c5fce35b3fe80645ccbe5"},
   "certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
   "clickhouse_ecto": {:git, "https://github.com/plausible/clickhouse_ecto.git", "93d86c48230f85797555c348dbe9e8738d3b8cc2", []},
   "clickhousex": {:git, "https://github.com/plausible/clickhousex", "0832dd4b1af1f0eba1d1018c231bf0d8d281f031", []},
-- 
GitLab