Kamailio如何限制呼叫并发数或者限制cps等等

推荐链接:​​​​​​​​​​​​​​​​​​​​​https://lists.kamailio.org//pipermail/sr-users/2013-September/079713.html

此外,文章很好:

Kamailio limit CPS/CPM/Concurrent calls per gateway.
Small task to make some generic limiters for calls per gateway, which in my case - destination.
Limiting concurrent calls is based on dialog module, limiting CPS/CPM - on htable module, actual solution taken from here.

On a modparam I'm pointing only on settings, that needed for this solution, so, if you want to adopt it to your case - use your settings along with provided.

loadmodule "htable.so"
...
modparam("htable", "htable", "rhs=>size=32;initval=0;autoexpire=300;")
modparam("htable", "htable", "rhm=>size=32;initval=0;autoexpire=1800;")

loadmodule "dialog.so"
...
modparam("dialog", "profiles_with_value", "concurrent_calls")

...

request_route {
...
   if (is_method("INVITE")) {
      route(LIMIT_CALLS)
   }
...
}

route[LIMIT_CALLS] {
    # Limit oncurrent calls per gateway
    if (!dlg_isflagset("1")) {
        if (get_profile_size("concurrent_calls", "$td", "$avp(calls)")) {
            $avp(concCallLimit) = <ACTUAL_CONCURRENT_CALLS_LIMIT_HERE>;
            if ($avp(calls) >= $avp(concCallLimit)) {
                xlog("L_INFO", "Concurrent calls to $td limit reached");
                send_reply("503", "Calls limit reached");
                exit;
            } else {
                dlg_manage();
                dlg_setflag("1");
                set_dlg_profile("concurrent_calls","$td");
            }
        }
    }

    $avp(rateHashSec) = "$td:sec:"+$timef(%Y/%m/%d_%H_%M_%S);
    $avp(rateHashMin) = "$td:min:"+$timef(%Y/%m/%d_%H_%M_00);

    $avp(ratePerSec) = $shtinc(rhs=>$avp(rateHashSec));
    $avp(ratePerMin) = $shtinc(rhm=>$avp(rateHashMin));
    $avp(limitPerSec) = <ACTUAL_LIMIT_PER_SECOND_HERE>;
    $avp(limitPerMin) = <ACTUAL_LIMIT_PER_MINUTE_HERE>;
    if ($avp(ratePerSec) > $avp(limitPerSec) || $avp(ratePerMin) > $avp(limitPerMin)) {
        xlog("L_INFO", "CPS/CPM Limit on $td");
        send_reply("503", "CPS/CPM Limit on $td");
        exit;
    }
}

Main idea in concurrent calls - use dialog profiles and ability to count them. Point, it can be used only in stateful mode, so dialogs are tracked and deleted after it's end. Flag "1" is used not to count same dialog several times.

Idea in CPS/CPM limiter - using htable with keys, that can be accessible only in specific time due name formation rules.

And $td is used to limit to current destination, that was set up previously (no touching this part)
This one is not very best for copy-paste, so, adopt it to your needs :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值