mirror of
https://github.com/processone/ejabberd.git
synced 2024-06-02 21:17:12 +02:00
New shaper implementation
This commit is contained in:
parent
3ec623f329
commit
3099702039
|
@ -30,7 +30,7 @@
|
||||||
|
|
||||||
-author('alexey@process-one.net').
|
-author('alexey@process-one.net').
|
||||||
|
|
||||||
-export([start_link/0, new/1, new1/1, update/2,
|
-export([start_link/0, new/1, update/2,
|
||||||
get_max_rate/1, transform_options/1, load_from_config/0,
|
get_max_rate/1, transform_options/1, load_from_config/0,
|
||||||
opt_type/1]).
|
opt_type/1]).
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
|
@ -40,11 +40,13 @@
|
||||||
-include("logger.hrl").
|
-include("logger.hrl").
|
||||||
|
|
||||||
-record(maxrate, {maxrate = 0 :: integer(),
|
-record(maxrate, {maxrate = 0 :: integer(),
|
||||||
lastrate = 0.0 :: float(),
|
burst_size = 0 :: integer(),
|
||||||
|
acquired_credit = 0 :: integer(),
|
||||||
lasttime = 0 :: integer()}).
|
lasttime = 0 :: integer()}).
|
||||||
|
|
||||||
-record(shaper, {name :: {atom(), global},
|
-record(shaper, {name :: {atom(), global},
|
||||||
maxrate :: integer()}).
|
maxrate :: integer(),
|
||||||
|
burst_size :: integer()}).
|
||||||
|
|
||||||
-record(state, {}).
|
-record(state, {}).
|
||||||
|
|
||||||
|
@ -88,10 +90,13 @@ load_from_config() ->
|
||||||
case mnesia:transaction(
|
case mnesia:transaction(
|
||||||
fun() ->
|
fun() ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun({Name, MaxRate}) ->
|
fun({Name, MaxRate, BurstSize}) ->
|
||||||
mnesia:write(#shaper{name = {Name, global},
|
mnesia:write(
|
||||||
maxrate = MaxRate})
|
#shaper{name = {Name, global},
|
||||||
end, Shapers)
|
maxrate = MaxRate,
|
||||||
|
burst_size = BurstSize})
|
||||||
|
end,
|
||||||
|
Shapers)
|
||||||
end) of
|
end) of
|
||||||
{atomic, ok} ->
|
{atomic, ok} ->
|
||||||
ok;
|
ok;
|
||||||
|
@ -112,62 +117,56 @@ get_max_rate(Name) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec new(atom()) -> shaper().
|
-spec new(atom()) -> shaper().
|
||||||
|
|
||||||
new(none) ->
|
new(none) ->
|
||||||
none;
|
none;
|
||||||
new(Name) ->
|
new(Name) ->
|
||||||
MaxRate = case ets:lookup(shaper, {Name, global}) of
|
case ets:lookup(shaper, {Name, global}) of
|
||||||
[#shaper{maxrate = R}] ->
|
[#shaper{maxrate = R, burst_size = B}] ->
|
||||||
R;
|
|
||||||
|
#maxrate{maxrate = R, burst_size = B,
|
||||||
|
acquired_credit = B,
|
||||||
|
lasttime = p1_time_compat:system_time(micro_seconds)};
|
||||||
[] ->
|
[] ->
|
||||||
none
|
none
|
||||||
end,
|
end.
|
||||||
new1(MaxRate).
|
|
||||||
|
|
||||||
-spec new1(none | integer()) -> shaper().
|
|
||||||
|
|
||||||
new1(none) -> none;
|
|
||||||
new1(MaxRate) ->
|
|
||||||
#maxrate{maxrate = MaxRate, lastrate = 0.0,
|
|
||||||
lasttime = p1_time_compat:system_time(micro_seconds)}.
|
|
||||||
|
|
||||||
-spec update(shaper(), integer()) -> {shaper(), integer()}.
|
-spec update(shaper(), integer()) -> {shaper(), integer()}.
|
||||||
|
|
||||||
update(none, _Size) -> {none, 0};
|
update(none, _Size) -> {none, 0};
|
||||||
update(#maxrate{} = State, Size) ->
|
update(#maxrate{maxrate = MR, burst_size = BS,
|
||||||
MinInterv = 1000 * Size /
|
acquired_credit = AC, lasttime = L} = State, Size) ->
|
||||||
(2 * State#maxrate.maxrate - State#maxrate.lastrate),
|
Now = p1_time_compat:system_time(micro_seconds),
|
||||||
Interv = (p1_time_compat:system_time(micro_seconds) - State#maxrate.lasttime) /
|
AC2 = min(BS, AC + (MR*(Now - L) div 1000000) - Size),
|
||||||
1000,
|
|
||||||
?DEBUG("State: ~p, Size=~p~nM=~p, I=~p~n",
|
Pause = if AC2 >= 0 -> 0;
|
||||||
[State, Size, MinInterv, Interv]),
|
true -> -1000*AC2 div MR
|
||||||
Pause = if MinInterv > Interv ->
|
|
||||||
1 + trunc(MinInterv - Interv);
|
|
||||||
true -> 0
|
|
||||||
end,
|
end,
|
||||||
NextNow = p1_time_compat:system_time(micro_seconds) + Pause * 1000,
|
?DEBUG("MaxRate=~p, BurstSize=~p, AcquiredCredit=~p, Size=~p, NewAcquiredCredit=~p, Pause=~p",
|
||||||
Div = case NextNow - State#maxrate.lasttime of
|
[MR, BS, AC, Size, AC2, Pause]),
|
||||||
0 -> 1;
|
{State#maxrate{acquired_credit = AC2, lasttime = Now},
|
||||||
V -> V
|
|
||||||
end,
|
|
||||||
{State#maxrate{lastrate =
|
|
||||||
(State#maxrate.lastrate +
|
|
||||||
1000000 * Size / Div)
|
|
||||||
/ 2,
|
|
||||||
lasttime = NextNow},
|
|
||||||
Pause}.
|
Pause}.
|
||||||
|
|
||||||
transform_options(Opts) ->
|
transform_options(Opts) ->
|
||||||
lists:foldl(fun transform_options/2, [], Opts).
|
lists:foldl(fun transform_options/2, [], Opts).
|
||||||
|
|
||||||
transform_options({OptName, Name, {maxrate, N}}, Opts) when OptName == shaper ->
|
transform_options({shaper, Name, {maxrate, N}}, Opts) ->
|
||||||
[{shaper, [{Name, N}]}|Opts];
|
[{shaper, [{Name, N}]} | Opts];
|
||||||
transform_options({OptName, Name, none}, Opts) when OptName == shaper ->
|
transform_options({shaper, Name, none}, Opts) ->
|
||||||
[{shaper, [{Name, none}]}|Opts];
|
[{shaper, [{Name, none}]} | Opts];
|
||||||
|
transform_options({shaper, List}, Opts) when is_list(List) ->
|
||||||
|
R = lists:map(
|
||||||
|
fun({Name, Args}) when is_list(Args) ->
|
||||||
|
MaxRate = proplists:get_value(rate, Args, 1000),
|
||||||
|
BurstSize = proplists:get_value(burst_size, Args, MaxRate),
|
||||||
|
{Name, MaxRate, BurstSize};
|
||||||
|
({Name, Val}) ->
|
||||||
|
{Name, Val, Val}
|
||||||
|
end, List),
|
||||||
|
[{shaper, R} | Opts];
|
||||||
transform_options(Opt, Opts) ->
|
transform_options(Opt, Opts) ->
|
||||||
[Opt|Opts].
|
[Opt | Opts].
|
||||||
|
|
||||||
-spec opt_type(shaper) -> fun((any()) -> any());
|
-spec opt_type(shaper) -> fun((any()) -> any());
|
||||||
(atom()) -> [atom()].
|
(atom()) -> [atom()].
|
||||||
opt_type(shaper) -> fun (V) -> V end;
|
opt_type(shaper) -> fun(V) -> V end;
|
||||||
opt_type(_) -> [shaper].
|
opt_type(_) -> [shaper].
|
||||||
|
|
Loading…
Reference in New Issue
Block a user