25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-10-31 15:21:38 +01:00
xmpp.chapril.org-ejabberd/src/shaper.erl

88 lines
2.8 KiB
Erlang
Raw Normal View History

%%%----------------------------------------------------------------------
%%% File : shaper.erl
%%% Author : Alexey Shchepin <alexey@process-one.net>
%%% Purpose : Functions to control connections traffic
%%% Created : 9 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
2012-02-23 16:52:34 +01:00
%%% ejabberd, Copyright (C) 2002-2012 ProcessOne
%%%
%%% 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.
%%%
%%% 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
%%%
%%%----------------------------------------------------------------------
-module(shaper).
2012-09-11 15:45:59 +02:00
-author('alexey@process-one.net').
-export([new/1, new1/1, update/2]).
-include("ejabberd.hrl").
2012-09-11 15:45:59 +02:00
-record(maxrate, {maxrate = 0 :: integer(),
lastrate = 0.0 :: float(),
lasttime = 0 :: integer()}).
-type maxrate() :: none | #maxrate{}.
-type shaper() :: maxrate() | {maxrate(), integer()}.
2012-09-11 15:45:59 +02:00
-export_type([shaper/0]).
-spec new(atom()) -> maxrate().
new(Name) ->
2012-09-11 15:45:59 +02:00
Data = ejabberd_config:get_global_option(
{shaper, Name, global},
fun({maxrate, R}) when is_integer(R), R>0 ->
{maxrate, R};
(none) ->
none
end, none),
new1(Data).
2012-09-11 15:45:59 +02:00
-spec new1(none | {maxrate, integer()}) -> maxrate().
2012-09-11 15:45:59 +02:00
new1(none) -> none;
new1({maxrate, MaxRate}) ->
2012-09-11 15:45:59 +02:00
#maxrate{maxrate = MaxRate, lastrate = 0.0,
lasttime = now_to_usec(now())}.
2012-09-11 15:45:59 +02:00
-spec update(maxrate(), integer()) -> {maxrate(), integer()}.
2012-09-11 15:45:59 +02:00
update(none, _Size) -> {none, 0};
update(#maxrate{} = State, Size) ->
MinInterv = 1000 * Size /
2012-09-11 15:45:59 +02:00
(2 * State#maxrate.maxrate - State#maxrate.lastrate),
Interv = (now_to_usec(now()) - State#maxrate.lasttime) /
1000,
?DEBUG("State: ~p, Size=~p~nM=~p, I=~p~n",
2012-09-11 15:45:59 +02:00
[State, Size, MinInterv, Interv]),
Pause = if MinInterv > Interv ->
1 + trunc(MinInterv - Interv);
true -> 0
end,
NextNow = now_to_usec(now()) + Pause * 1000,
2012-09-11 15:45:59 +02:00
{State#maxrate{lastrate =
(State#maxrate.lastrate +
1000000 * Size / (NextNow - State#maxrate.lasttime))
/ 2,
lasttime = NextNow},
Pause}.
now_to_usec({MSec, Sec, USec}) ->
2012-09-11 15:45:59 +02:00
(MSec * 1000000 + Sec) * 1000000 + USec.