Hello,

nice approach and glad to see I could throw some seeds for new ideas out there!

You should use $shtinc(...), like:

$var(rps) = $shtinc(htable_source_group_rates=>$var(rateHashSec));
$var(rpm) = $shtinc(htable_source_group_rates=>$var(rateHashMin));

In this way you avoid races on increment operation done with '+'. Alternatives would be to use locks via cfgutils module.

Then you can use $var(...) for test if you want to allow the requests that came below the limit, or the current value with $sht(...) to block if there were other requests coming meanwhile (after the increment).

Cheers,
Daniel

On 9/12/13 9:21 PM, Gareth Rylance wrote:


I was inspired by Daniels ClueCon 2013 talk and have decide to try to add simple rate limiting using a HTable.
$var(srcgroup) is an id that identifies the invites source/ I wish to limit invites from a given source.

$var(rateHashMin) was added just to allow simple monitoring.
I query the table via XML-RPC to get an idea of current load. 
I think it would be better to add it to a seperate table and then I could increase the exprires time to capture more of the course grain points.



I am interested in any comments.

 
----------------------
modparam("htable", "htable", "htable_source_group_rates=>size=8;dbtable=htable_source_group_rates;initval=0;autoexpire=300;")
modparam("htable", "htable", "htable_source_group_limits=>size=8;dbtable=htable_source_group_limits;initval=500;")
----------------------
----------------------
route[SOURCE_GROUP_LIMIT] {

if (is_method("INVITE")){
$var(rateHashSec) = (str)$var(srcgroup)+":sec:"+$timef(%Y/%m/%d_%H_%M_%S);
$var(rateHashMin) = (str)$var(srcgroup)+":min:"+$timef(%Y/%m/%d_%H_%M_00);
$sht(htable_source_group_rates=>$var(rateHashSec)) = $sht(htable_source_group_rates=>$var(rateHashSec)) + 1;
$sht(htable_source_group_rates=>$var(rateHashMin)) = $sht(htable_source_group_rates=>$var(rateHashMin)) + 1;
if($sht(htable_source_group_rates=>$var(rateHashSec)) >   $sht(htable_source_group_limits=>$var(srcgroup)))
{
sl_send_reply("503", "$si over source Limit of $sht(htable_source_group_rates=>$var(rateHashSec)) /  $sht(htable_source_group_limits=>$var(srcgroup)");
exit;
}
}
}
----------------------



Thanks Gareth


_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
sr-users@lists.sip-router.org
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users

-- 
Daniel-Constantin Mierla - http://www.asipto.com
http://twitter.com/#!/miconda - http://www.linkedin.com/in/miconda
Kamailio Advanced Trainings - Berlin, Oct 21-24; Miami, Nov 11-13, 2013
  - more details about Kamailio trainings at http://www.asipto.com -