Idempotency: Use special cache, keep for 6 hours.

This commit is contained in:
lain 2018-05-05 11:15:57 +02:00
parent 762f6edc29
commit c464355d1a
3 changed files with 30 additions and 7 deletions

View File

@ -23,6 +23,18 @@ defmodule Pleroma.Application do
limit: 2500 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.Web.Federator, []),
worker(Pleroma.Gopher.Server, []), worker(Pleroma.Gopher.Server, []),
worker(Pleroma.Stats, []) worker(Pleroma.Stats, [])

View File

@ -283,13 +283,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
{:ok, activity} = {:ok, activity} =
Cachex.get!( Cachex.get!(
:user_cache, :idempotency_cache,
"idem:#{idempotency_key}", idempotency_key,
fallback: fn _ -> CommonAPI.post(user, params) end 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}) render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end end

View File

@ -75,9 +75,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
"sensitive" => "false" "sensitive" => "false"
}) })
{:ok, ttl} = Cachex.ttl(:user_cache, "idem:#{idempotency_key}") {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key)
# 5 Minutes # Six hours
assert ttl > :timer.seconds(5 * 60 - 1) assert ttl > :timer.seconds(6 * 60 * 60 - 1)
assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} =
json_response(conn_one, 200) 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} = json_response(conn_two, 200)
assert id == second_id 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 end
test "posting a sensitive status", %{conn: conn} do test "posting a sensitive status", %{conn: conn} do