diff options
author | rinpatch <rinpatch@sdf.org> | 2020-02-28 17:35:01 +0300 |
---|---|---|
committer | rinpatch <rinpatch@sdf.org> | 2020-03-01 01:13:07 +0300 |
commit | 4d416343fae4a9e0b1654b12bd476017be63a7e9 (patch) | |
tree | 272e3a611db85c2344f553f0e956cafaae27d4be /test | |
parent | df2173343accec7a7a311d85df2f13d5141b7bc7 (diff) | |
download | pleroma-4d416343fae4a9e0b1654b12bd476017be63a7e9.tar.gz pleroma-4d416343fae4a9e0b1654b12bd476017be63a7e9.zip |
rate limiter: Fix a race condition
When multiple requests are processed by rate limiter plug at the same
time and the bucket is not yet initialized, both would try to initialize
the bucket resulting in an internal server error.
Diffstat (limited to 'test')
-rw-r--r-- | test/plugs/rate_limiter_test.exs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/test/plugs/rate_limiter_test.exs b/test/plugs/rate_limiter_test.exs index 104d67611..8cdc8d1a2 100644 --- a/test/plugs/rate_limiter_test.exs +++ b/test/plugs/rate_limiter_test.exs @@ -242,4 +242,35 @@ defmodule Pleroma.Plugs.RateLimiterTest do refute conn_2.halted end end + + test "doesn't crash due to a race condition when multiple requests are made at the same time and the bucket is not yet initialized" do + limiter_name = :test_race_condition + Pleroma.Config.put([:rate_limit, limiter_name], {1000, 5}) + Pleroma.Config.put([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8}) + + opts = RateLimiter.init(name: limiter_name) + + conn = conn(:get, "/") + conn_2 = conn(:get, "/") + + %Task{pid: pid1} = + task1 = + Task.async(fn -> + receive do + :process2_up -> + RateLimiter.call(conn, opts) + end + end) + + task2 = + Task.async(fn -> + send(pid1, :process2_up) + RateLimiter.call(conn_2, opts) + end) + + Task.await(task1) + Task.await(task2) + + refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts) + end end |