Idempotency: Use special cache, keep for 6 hours.
authorlain <lain@soykaf.club>
Sat, 5 May 2018 09:15:57 +0000 (11:15 +0200)
committerlain <lain@soykaf.club>
Sat, 5 May 2018 09:15:57 +0000 (11:15 +0200)
lib/pleroma/application.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index 89826f515fb34c6fecf0fafd9a6e25a975c4e741..e1e3bcd63fd3533961e2d4d7c7be159ddaa69047 100644 (file)
@@ -23,6 +23,18 @@ defmodule Pleroma.Application do
             limit: 2500
           ]
         ]),
+        worker(
+          Cachex,
+          [
+            :idempotency_cache,
+            [
+              default_ttl: :timer.seconds(6 * 60 * 60),
+              ttl_interval: :timer.seconds(60),
+              limit: 2500
+            ]
+          ],
+          id: :cachex_idem
+        ),
         worker(Pleroma.Web.Federator, []),
         worker(Pleroma.Gopher.Server, []),
         worker(Pleroma.Stats, [])
index bbd16482a52bca3d4a74c98cd823b957492f077e..2b4e9e72b2c58fdc6b78b965d214e9618fcc6eff 100644 (file)
@@ -283,13 +283,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
 
     {:ok, activity} =
       Cachex.get!(
-        :user_cache,
-        "idem:#{idempotency_key}",
+        :idempotency_cache,
+        idempotency_key,
         fallback: fn _ -> CommonAPI.post(user, params) end
       )
 
-    Cachex.expire(:user_cache, "idem:#{idempotency_key}", :timer.seconds(5 * 60))
-
     render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
   end
 
index 69a0299acf19bd37251a8b777ed88296f689e12e..883ebc61e085cefb37d89f8368c145c78fd6cf16 100644 (file)
@@ -75,9 +75,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
         "sensitive" => "false"
       })
 
-    {:ok, ttl} = Cachex.ttl(:user_cache, "idem:#{idempotency_key}")
-    # 5 Minutes
-    assert ttl > :timer.seconds(5 * 60 - 1)
+    {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
+    # Six hours
+    assert ttl > :timer.seconds(6 * 60 * 60 - 1)
 
     assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
              json_response(conn_one, 200)
@@ -97,6 +97,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     assert %{"id" => second_id} = json_response(conn_two, 200)
 
     assert id == second_id
+
+    conn_three =
+      conn
+      |> assign(:user, user)
+      |> post("/api/v1/statuses", %{
+        "status" => "cofe",
+        "spoiler_text" => "2hu",
+        "sensitive" => "false"
+      })
+
+    assert %{"id" => third_id} = json_response(conn_three, 200)
+
+    refute id == third_id
   end
 
   test "posting a sensitive status", %{conn: conn} do