2003-02-09 20:17:23 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : shaper.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-02-09 20:17:23 +01:00
|
|
|
%%% Purpose : Functions to control connections traffic
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 9 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-02-09 20:17:23 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(shaper).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-export([start/0, new/1, new1/1, update/2,
|
|
|
|
get_max_rate/1, transform_options/1, load_from_config/0,
|
|
|
|
opt_type/1]).
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2008-03-21 15:44:16 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(maxrate, {maxrate = 0 :: integer(),
|
|
|
|
lastrate = 0.0 :: float(),
|
|
|
|
lasttime = 0 :: integer()}).
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-record(shaper, {name :: {atom(), global},
|
|
|
|
maxrate :: integer()}).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-type shaper() :: none | #maxrate{}.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export_type([shaper/0]).
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec start() -> ok.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
start() ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, shaper,
|
2013-08-12 14:25:05 +02:00
|
|
|
[{ram_copies, [node()]},
|
|
|
|
{local_content, true},
|
|
|
|
{attributes, record_info(fields, shaper)}]),
|
|
|
|
mnesia:add_table_copy(shaper, node(), ram_copies),
|
|
|
|
load_from_config(),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
-spec load_from_config() -> ok | {error, any()}.
|
|
|
|
|
|
|
|
load_from_config() ->
|
|
|
|
Shapers = ejabberd_config:get_option(
|
|
|
|
shaper, fun(V) -> V end, []),
|
|
|
|
case mnesia:transaction(
|
|
|
|
fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Name, MaxRate}) ->
|
|
|
|
mnesia:write(#shaper{name = {Name, global},
|
|
|
|
maxrate = MaxRate})
|
|
|
|
end, Shapers)
|
|
|
|
end) of
|
|
|
|
{atomic, ok} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
{error, Err}
|
|
|
|
end.
|
|
|
|
|
2014-05-08 14:08:07 +02:00
|
|
|
-spec get_max_rate(atom()) -> none | non_neg_integer().
|
|
|
|
|
|
|
|
get_max_rate(none) ->
|
|
|
|
none;
|
|
|
|
get_max_rate(Name) ->
|
|
|
|
case ets:lookup(shaper, {Name, global}) of
|
|
|
|
[#shaper{maxrate = R}] ->
|
|
|
|
R;
|
|
|
|
[] ->
|
|
|
|
none
|
|
|
|
end.
|
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec new(atom()) -> shaper().
|
|
|
|
|
|
|
|
new(none) ->
|
|
|
|
none;
|
2003-02-09 20:17:23 +01:00
|
|
|
new(Name) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
MaxRate = case ets:lookup(shaper, {Name, global}) of
|
|
|
|
[#shaper{maxrate = R}] ->
|
|
|
|
R;
|
|
|
|
[] ->
|
|
|
|
none
|
|
|
|
end,
|
|
|
|
new1(MaxRate).
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec new1(none | integer()) -> shaper().
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
new1(none) -> none;
|
2013-08-12 14:25:05 +02:00
|
|
|
new1(MaxRate) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#maxrate{maxrate = MaxRate, lastrate = 0.0,
|
2015-12-07 16:08:57 +01:00
|
|
|
lasttime = p1_time_compat:system_time(micro_seconds)}.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
-spec update(shaper(), integer()) -> {shaper(), integer()}.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
update(none, _Size) -> {none, 0};
|
2003-02-09 20:17:23 +01:00
|
|
|
update(#maxrate{} = State, Size) ->
|
|
|
|
MinInterv = 1000 * Size /
|
2013-03-14 10:33:02 +01:00
|
|
|
(2 * State#maxrate.maxrate - State#maxrate.lastrate),
|
2015-12-07 16:08:57 +01:00
|
|
|
Interv = (p1_time_compat:system_time(micro_seconds) - State#maxrate.lasttime) /
|
2013-03-14 10:33:02 +01:00
|
|
|
1000,
|
2008-03-21 19:58:07 +01:00
|
|
|
?DEBUG("State: ~p, Size=~p~nM=~p, I=~p~n",
|
2013-03-14 10:33:02 +01:00
|
|
|
[State, Size, MinInterv, Interv]),
|
|
|
|
Pause = if MinInterv > Interv ->
|
|
|
|
1 + trunc(MinInterv - Interv);
|
|
|
|
true -> 0
|
2006-01-13 02:55:20 +01:00
|
|
|
end,
|
2015-12-07 16:08:57 +01:00
|
|
|
NextNow = p1_time_compat:system_time(micro_seconds) + Pause * 1000,
|
2016-04-29 10:57:34 +02:00
|
|
|
Div = case NextNow - State#maxrate.lasttime of
|
|
|
|
0 -> 1;
|
|
|
|
V -> V
|
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
{State#maxrate{lastrate =
|
|
|
|
(State#maxrate.lastrate +
|
2016-04-29 10:57:34 +02:00
|
|
|
1000000 * Size / Div)
|
2013-03-14 10:33:02 +01:00
|
|
|
/ 2,
|
|
|
|
lasttime = NextNow},
|
2006-01-13 02:55:20 +01:00
|
|
|
Pause}.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_options(Opts) ->
|
|
|
|
lists:foldl(fun transform_options/2, [], Opts).
|
|
|
|
|
2013-12-10 08:40:43 +01:00
|
|
|
transform_options({OptName, Name, {maxrate, N}}, Opts) when OptName == shaper ->
|
2013-08-12 14:25:05 +02:00
|
|
|
[{shaper, [{Name, N}]}|Opts];
|
2013-12-10 08:40:43 +01:00
|
|
|
transform_options({OptName, Name, none}, Opts) when OptName == shaper ->
|
2013-08-12 14:25:05 +02:00
|
|
|
[{shaper, [{Name, none}]}|Opts];
|
|
|
|
transform_options(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
opt_type(shaper) -> fun (V) -> V end;
|
|
|
|
opt_type(_) -> [shaper].
|