Fix behaviour of max_waiting = 0 with max_running = 1

This commit is contained in:
Jordan Bracco 2020-09-01 19:41:13 +02:00
parent 6f45866724
commit 3aa46650e2
3 changed files with 19 additions and 0 deletions

View file

@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
- Decrement counter when max retries has been reached.
- Fixes behaviour of `max_waiting = 0` with `max_size = 1`.
## [0.1.0] - 2020-05-16

View file

@ -127,6 +127,13 @@ defmodule ConcurrentLimiter do
scope: "max"
})
max_waiting == 0 ->
:telemetry.execute([:concurrent_limiter, :overload], %{counter: counter}, %{limiter: name, scope: "max"})
dec(ref, name)
{:error, :overload}
counter > max ->
:telemetry.execute([:concurrent_limiter, :overload], %{counter: counter}, %{limiter: name, scope: "max"})
dec(ref, name)
{:error, :overload}

View file

@ -6,6 +6,17 @@ defmodule ConcurrentLimiterTest do
use ExUnit.Case
doctest ConcurrentLimiter
test "limited to one" do
name = "l1"
ConcurrentLimiter.new(name, 1, 0, max_retries: 0)
endless = fn() -> :timer.sleep(10000) end
spawn(fn() -> ConcurrentLimiter.limit(name, endless) end)
:timer.sleep(5)
{:error, :overload} = ConcurrentLimiter.limit(name, endless)
{:error, :overload} = ConcurrentLimiter.limit(name, endless)
{:error, :overload} = ConcurrentLimiter.limit(name, endless)
end
test "limiter is atomic" do
name = "test"
ConcurrentLimiter.new(name, 2, 2)