-
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.
Merge pull request #4187 from esl/shapers
Migrate from inside shapers to opuntia
- Loading branch information
Showing
18 changed files
with
122 additions
and
331 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 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,70 @@ | ||
-module(mongoose_shaper). | ||
|
||
-export([child_spec/0]). | ||
-export([new/1, update/2, wait/5, reset_all_shapers/1]). | ||
-ignore_xref([reset_all_shapers/1]). | ||
|
||
-type shaper() :: opuntia:shaper(). | ||
-export_type([shaper/0]). | ||
|
||
-spec child_spec() -> supervisor:child_spec(). | ||
child_spec() -> | ||
WPoolOpts = [{workers, 10}, {worker, {opuntia_srv, {?MODULE, #{}}}}], | ||
#{id => ?MODULE, | ||
start => {wpool, start_pool, [?MODULE, WPoolOpts]}, | ||
restart => permanent, | ||
shutdown => infinity, | ||
type => supervisor}. | ||
|
||
-spec new(atom()) -> opuntia:shaper(). | ||
new(Name) -> | ||
opuntia:new(get_shaper_config(Name)). | ||
|
||
-spec update(opuntia:shaper(), opuntia:tokens()) -> {opuntia:shaper(), opuntia:delay()}. | ||
update(Shaper, Tokens) -> | ||
opuntia:update(Shaper, Tokens). | ||
|
||
%% @doc Shapes the caller from executing the action. | ||
-spec wait(HostType :: mongooseim:host_type_or_global(), | ||
Domain :: jid:lserver(), | ||
Action :: atom(), | ||
FromJID :: jid:jid(), | ||
Size :: integer()) -> continue | {error, max_delay_reached}. | ||
wait(HostType, Domain, Action, FromJID, Size) -> | ||
Worker = wpool_pool:hash_worker(?MODULE, FromJID), | ||
Config = get_shaper_config(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(?MODULE) ], | ||
ok. | ||
|
||
-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. | ||
|
||
-spec get_shaper_config(atom()) -> number(). | ||
get_shaper_config(Name) -> | ||
case mongoose_config:lookup_opt([shaper, Name]) of | ||
{ok, #{max_rate := MaxRatePerSecond}} -> | ||
Rate = MaxRatePerSecond div 1000, | ||
#{bucket_size => MaxRatePerSecond, rate => Rate, start_full => true}; | ||
{error, not_found} -> | ||
0 | ||
end. | ||
|
||
default_shaper() -> | ||
none. |
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
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.