Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ env:

jobs:

build:

runs-on: ubuntu-20.04
OTP25:
runs-on: ubuntu-24.04

strategy:
matrix:
Expand All @@ -24,12 +23,44 @@ jobs:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar}}
- name: Restore _build
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: _build
key: _build-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
- name: Restore rebar3's cache
uses: actions/cache@v4
with:
path: ~/.cache/rebar3
key: rebar3-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
- name: Compile
run: rebar3 compile
- name: Format check
run: rebar3 format --verify
- name: Run tests and verifications
run: rebar3 test

OTP27:
runs-on: ubuntu-24.04

strategy:
matrix:
otp: ['27.3.4']
rebar: ['3.24.0']

steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
id: setup-beam
with:
otp-version: ${{matrix.otp}}
rebar3-version: ${{matrix.rebar}}
- name: Restore _build
uses: actions/cache@v4
with:
path: _build
key: _build-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
- name: Restore rebar3's cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ~/.cache/rebar3
key: rebar3-cache-for-os-${{runner.os}}-otp-${{steps.setup-beam.outputs.otp-version}}-rebar3-${{steps.setup-beam.outputs.rebar3-version}}-hash-${{hashFiles('rebar.lock')}}
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ Example of use
----------------

A process about to execute a named section of code whose maximum parallelism
should be limited will call spillway:enter/2/3 with the name, the weight, and limit.
should be limited will call `spillway:enter/2/3` with the name, the weight, and limit.

If the return value is the 2-tuple {true, TotalWeight}, the process may enter the section of code
If the return value is the 2-tuple `{true, TotalWeight}`, the process may enter the section of code
(there now being TotalWeight in use concurrently-executing accesses), and otherwise not.

If the process entered the section of code, it should call spillway:leave/2 with the name and weight
If the process entered the section of code, it should call `spillway:leave/2` with the name and weight
after completion.

No special arrangement is made to handle process exits. If a process dies without
calling spillway:leave/1, the counter will be inaccurate. This is intentional,
calling `spillway:leave/1`, the counter will be inaccurate. This is intentional,
and callers should make arrangements to mitigate this occurrence.

```
Expand Down Expand Up @@ -58,8 +58,11 @@ Spillway is implemented based on ETS-based bounded named counters.

Build
-----
$ make
$ make ct

```shell
make
make ct
```

1.x Changelog
-------------
Expand Down
19 changes: 19 additions & 0 deletions src/spillway_srv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

-record(counter, {name, value = 0 :: non_neg_integer()}).

-elvis([{elvis_style, dont_repeat_yourself, disable}]).

%%%===================================================================
%%% External functions
%%%===================================================================
Expand Down Expand Up @@ -68,10 +70,27 @@ enter(Name, Size, Limit) when Size > 0 ->

%% Attempt to decrement the named counter, with a lower limit of 0. Return the new value.
%% The counter must already exist.
-if(?OTP_RELEASE =< 25).

-spec leave(term(), non_neg_integer()) -> non_neg_integer().
leave(Name, Size) ->
ets:update_counter(?TID, Name, {#counter.value, -Size, 0, 0}).

- else .

-spec leave(term(), non_neg_integer()) -> non_neg_integer().
leave(Name, Size) ->
case ets:update_counter(?TID, Name, {#counter.value, -Size, 0, 0}) of
[] ->
0;
[Result | _] ->
Result;
Result ->
Result
end.

-endif.

%% Return the current counter value.
-spec cur(term()) -> non_neg_integer().
cur(Name) ->
Expand Down