-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ab4749
commit 4949ffd
Showing
8 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
warn_export_all | ||
]}. | ||
|
||
{deps, [jsx]}. | ||
{deps, [jsx, fuse]}. | ||
|
||
{profiles, [ | ||
{test, [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
{"1.2.0", | ||
[{<<"jsx">>,{pkg,<<"jsx">>,<<"3.1.0">>},0}]}. | ||
[{<<"fuse">>,{pkg,<<"fuse">>,<<"2.5.0">>},0}, | ||
{<<"jsx">>,{pkg,<<"jsx">>,<<"3.1.0">>},0}]}. | ||
[ | ||
{pkg_hash,[ | ||
{<<"fuse">>, <<"71AFA90BE21DA4E64F94ABBA9D36472FAA2D799C67FEDC3BD1752A88EA4C4753">>}, | ||
{<<"jsx">>, <<"D12516BAA0BB23A59BB35DCCAF02A1BD08243FCBB9EFE24F2D9D056CCFF71268">>}]}, | ||
{pkg_hash_ext,[ | ||
{<<"fuse">>, <<"7F52A1C84571731AD3C91D569E03131CC220EBAA7E2A11034405F0BAC46A4FEF">>}, | ||
{<<"jsx">>, <<"0C5CC8FDC11B53CC25CF65AC6705AD39E54ECC56D1C22E4ADB8F5A53FB9427F3">>}]} | ||
]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-module(raven_rate_limit). | ||
|
||
% API | ||
-export([setup/0]). | ||
-export([teardown/0]). | ||
-export([run/1]). | ||
-export([delay/1]). | ||
|
||
%--- API ----------------------------------------------------------------------- | ||
|
||
setup() -> | ||
Result = case application:get_env(raven, rate_limit, false) of | ||
{Intensity, Period} -> | ||
FuseConfig = {{standard, Intensity, Period}, {reset, Period}}, | ||
ok = fuse:install(?MODULE, FuseConfig), | ||
{enabled, atomics:new(1, [{signed, false}])}; | ||
false -> | ||
disabled | ||
end, | ||
persistent_term:put(?MODULE, Result). | ||
|
||
teardown() -> | ||
case persistent_term:get(?MODULE) of | ||
{enabled, _} -> fuse:remove(?MODULE); | ||
disabled -> ok | ||
end, | ||
persistent_term:erase(?MODULE). | ||
|
||
run(Fun) -> | ||
case persistent_term:get(?MODULE) of | ||
disabled -> | ||
Fun(); | ||
{enabled, Atomics} -> | ||
RetryAfter = atomics:get(Atomics, 1), | ||
case erlang:system_time(second) > RetryAfter of | ||
true -> | ||
case fuse:ask(?MODULE, async_dirty) of | ||
blown -> | ||
{error, {rate_limit, local}}; | ||
_ -> | ||
ok = fuse:melt(?MODULE), | ||
Fun() | ||
end; | ||
false -> | ||
{error, {rate_limit, remote}} | ||
end | ||
end. | ||
|
||
delay(Seconds) when Seconds > 0 -> | ||
case persistent_term:get(?MODULE) of | ||
disabled -> ok; | ||
{enabled, Atomics} -> | ||
RetryAfter = erlang:system_time(second) + Seconds + 1, | ||
atomics:put(Atomics, 1, RetryAfter) | ||
end. |