-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from inside shapers to opuntia
- Loading branch information
1 parent
f44b5b0
commit bdf1b60
Showing
18 changed files
with
101 additions
and
326 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
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
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
-module(mongoose_shapers). | ||
|
||
-export([child_spec/0, get_shaper_rate/1, wait/5, reset_all_shapers/1]). | ||
-ignore_xref([reset_all_shapers/1]). | ||
|
||
-spec child_spec() -> supervisor:child_spec(). | ||
child_spec() -> | ||
WPoolOpts = [{workers, 10}, {worker, {opuntia_srv, {mongoose_shapers, #{}}}}], | ||
{mongoose_shapers, | ||
{wpool, start_pool, [mongoose_shapers, WPoolOpts]}, | ||
permanent, infinity, supervisor, [opuntia_srv]}. | ||
|
||
-spec get_shaper_rate(atom()) -> number(). | ||
get_shaper_rate(Name) -> | ||
case mongoose_config:lookup_opt([shaper, Name]) of | ||
{ok, #{max_rate := MaxRatePerSecond}} -> | ||
MaxRatePerSecond / 1000; | ||
{error, not_found} -> 0 | ||
end. | ||
|
||
%% @doc Shapes the caller from executing the action. | ||
-spec wait(HostType :: mongooseim:host_type_or_global(), | ||
Domain :: jid:server(), | ||
Action :: atom(), | ||
FromJID :: jid:jid(), | ||
Size :: integer()) -> continue | {error, max_delay_reached}. | ||
wait(HostType, Domain, Action, FromJID, Size) -> | ||
Worker = wpool_pool:hash_worker(mongoose_shapers, FromJID), | ||
Config = get_shaper_rate(get_shaper_name(HostType, Domain, Action, FromJID)), | ||
Key = new_key(Domain, Action, FromJID), | ||
opuntia_srv:wait(Worker, Key, Size, Config). | ||
|
||
-type key() :: {global | jid:server(), atom(), jid:jid()}. | ||
-spec new_key(jid:server() | global, atom(), jid:jid()) -> key(). | ||
new_key(Domain, Action, FromJID) -> | ||
{Domain, Action, FromJID}. | ||
|
||
%% @doc Ask all shaper servers to forget current shapers and read settings again | ||
reset_all_shapers(_HostType) -> | ||
[ opuntia_srv:reset_shapers(ProcName) | ||
|| ProcName <- wpool:get_workers(mongoose_shapers)]. | ||
|
||
-spec get_shaper_name(HostType :: mongooseim:host_type_or_global(), | ||
Domain :: global | jid:server(), | ||
Action :: atom(), | ||
FromJID :: jid:jid()) -> allow | none. | ||
get_shaper_name(HostType, Domain, Action, FromJID) -> | ||
case acl:match_rule(HostType, Domain, Action, FromJID) of | ||
deny -> default_shaper(); | ||
Value -> Value | ||
end. | ||
|
||
default_shaper() -> | ||
none. |
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
Oops, something went wrong.