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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 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
|
|
|
%%%
|
2007-12-24 13:58:05 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-02-09 20:17:23 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(shaper).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-09 20:17:23 +01:00
|
|
|
|
|
|
|
-export([new/1, new1/1, update/2]).
|
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2003-02-09 20:17:23 +01:00
|
|
|
-record(maxrate, {maxrate, lastrate, lasttime}).
|
|
|
|
|
|
|
|
|
|
|
|
new(Name) ->
|
2006-10-02 20:54:10 +02:00
|
|
|
Data = case ejabberd_config:get_global_option({shaper, Name, global}) of
|
2003-02-09 20:17:23 +01:00
|
|
|
undefined ->
|
|
|
|
none;
|
|
|
|
D ->
|
|
|
|
D
|
|
|
|
end,
|
|
|
|
new1(Data).
|
|
|
|
|
|
|
|
|
|
|
|
new1(none) ->
|
|
|
|
none;
|
|
|
|
new1({maxrate, MaxRate}) ->
|
|
|
|
#maxrate{maxrate = MaxRate,
|
|
|
|
lastrate = 0,
|
|
|
|
lasttime = now_to_usec(now())}.
|
|
|
|
|
|
|
|
|
2006-01-13 02:55:20 +01:00
|
|
|
update(none, _Size) ->
|
|
|
|
{none, 0};
|
2003-02-09 20:17:23 +01:00
|
|
|
update(#maxrate{} = State, Size) ->
|
|
|
|
MinInterv = 1000 * Size /
|
|
|
|
(2 * State#maxrate.maxrate - State#maxrate.lastrate),
|
|
|
|
Interv = (now_to_usec(now()) - State#maxrate.lasttime) / 1000,
|
2008-03-21 19:58:07 +01:00
|
|
|
?DEBUG("State: ~p, Size=~p~nM=~p, I=~p~n",
|
2008-03-21 15:44:16 +01:00
|
|
|
[State, Size, MinInterv, Interv]),
|
2006-01-13 02:55:20 +01:00
|
|
|
Pause = if
|
|
|
|
MinInterv > Interv ->
|
|
|
|
1 + trunc(MinInterv - Interv);
|
|
|
|
true ->
|
|
|
|
0
|
|
|
|
end,
|
|
|
|
NextNow = now_to_usec(now()) + Pause * 1000,
|
|
|
|
{State#maxrate{
|
|
|
|
lastrate = (State#maxrate.lastrate +
|
|
|
|
1000000 * Size / (NextNow - State#maxrate.lasttime))/2,
|
|
|
|
lasttime = NextNow},
|
|
|
|
Pause}.
|
2003-02-09 20:17:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
now_to_usec({MSec, Sec, USec}) ->
|
|
|
|
(MSec*1000000 + Sec)*1000000 + USec.
|