<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello,<br>
    <br>
    nice approach and glad to see I could throw some seeds for new ideas
    out there!<br>
    <br>
    You should use $shtinc(...), like:<br>
    <br>
    $var(rps) =
    $shtinc(htable_source_group_rates=>$var(rateHashSec));<br>
    $var(rpm) =
    $shtinc(htable_source_group_rates=>$var(rateHashMin));<br>
    <br>
    In this way you avoid races on increment operation done with '+'.
    Alternatives would be to use locks via cfgutils module.<br>
    <br>
    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).<br>
    <br>
    Cheers,<br>
    Daniel<br>
    <br>
    <div class="moz-cite-prefix">On 9/12/13 9:21 PM, Gareth Rylance
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAOLii26rxomExQwUrQgTaev5C3pKn0om7iAJC71N5mVoRPaFYw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div><br>
        </div>
        <div><br>
        </div>
        <div>I was inspired by Daniels ClueCon 2013 talk and have decide
          to try to add simple rate limiting using a HTable.</div>
        <div>$var(srcgroup) is an id that identifies the invites source/
          I wish to limit invites from a given source.<br>
        </div>
        <div><br>
        </div>
        <div>$var(rateHashMin) was added just to allow simple
          monitoring.</div>
        <div>I query the table via XML-RPC to get an idea of current
          load. </div>
        <div>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.</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>I am interested in any comments.<br>
        </div>
        <div><br>
        </div>
        <div> <br>
        </div>
        <div>----------------------</div>
        <div>
          <div>modparam("htable", "htable",
"htable_source_group_rates=>size=8;dbtable=htable_source_group_rates;initval=0;autoexpire=300;")</div>
          <div>modparam("htable", "htable",
"htable_source_group_limits=>size=8;dbtable=htable_source_group_limits;initval=500;")</div>
        </div>
        <div>----------------------<br>
        </div>
        <div>----------------------<br>
        </div>
        <div>route[SOURCE_GROUP_LIMIT] {</div>
        <div><br>
        </div>
        <div><span class="" style="white-space:pre"> </span>if
          (is_method("INVITE")){</div>
        <div><span class="" style="white-space:pre"> </span>$var(rateHashSec)
          = (str)$var(srcgroup)+":sec:"+$timef(%Y/%m/%d_%H_%M_%S);</div>
        <div><span class="" style="white-space:pre"> </span>$var(rateHashMin)
          = (str)$var(srcgroup)+":min:"+$timef(%Y/%m/%d_%H_%M_00);</div>
        <div><span class="" style="white-space:pre"> </span></div>
        <div><span class="" style="white-space:pre"> </span></div>
        <div><span class="" style="white-space:pre"> </span>$sht(htable_source_group_rates=>$var(rateHashSec))
          = $sht(htable_source_group_rates=>$var(rateHashSec)) + 1;</div>
        <div><span class="" style="white-space:pre"> </span>$sht(htable_source_group_rates=>$var(rateHashMin))
          = $sht(htable_source_group_rates=>$var(rateHashMin)) + 1;<span
            class="" style="white-space:pre"> </span></div>
        <div><span class="" style="white-space:pre"> </span></div>
        <div><span class="" style="white-space:pre"> </span>if($sht(htable_source_group_rates=>$var(rateHashSec))
          >   $sht(htable_source_group_limits=>$var(srcgroup)))</div>
        <div><span class="" style="white-space:pre"> </span>{</div>
        <div><span class="" style="white-space:pre"> </span>sl_send_reply("503",
          "$si over source Limit of
          $sht(htable_source_group_rates=>$var(rateHashSec)) /
           $sht(htable_source_group_limits=>$var(srcgroup)");</div>
        <div><span class="" style="white-space:pre"> </span>exit;</div>
        <div><span class="" style="white-space:pre"> </span>}</div>
        <div><span class="" style="white-space:pre"> </span>}</div>
        <div>}</div>
        <div>----------------------<br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div>Thanks Gareth</div>
      </div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
SIP Express Router (SER) and Kamailio (OpenSER) - sr-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:sr-users@lists.sip-router.org">sr-users@lists.sip-router.org</a>
<a class="moz-txt-link-freetext" href="http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users">http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-users</a>
</pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 
Daniel-Constantin Mierla - <a class="moz-txt-link-freetext" href="http://www.asipto.com">http://www.asipto.com</a>
<a class="moz-txt-link-freetext" href="http://twitter.com/#!/miconda">http://twitter.com/#!/miconda</a> - <a class="moz-txt-link-freetext" href="http://www.linkedin.com/in/miconda">http://www.linkedin.com/in/miconda</a>
Kamailio Advanced Trainings - Berlin, Oct 21-24; Miami, Nov 11-13, 2013
  - more details about Kamailio trainings at <a class="moz-txt-link-freetext" href="http://www.asipto.com">http://www.asipto.com</a> -
</pre>
  </body>
</html>