Administrator
发布于 2026-11-16 / 4 阅读
0
0

FreeSWITCH Practice 19 | Limit Concurrent Calls and Call Rate

English
中文

A trunk can run out of capacity because existing calls are still active or because a burst brings too many new calls in a short time. Use extensions 7101 and 7102 to reproduce both cases on one internal FreeSWITCH node. One test number rejects the second call while the first one is active. The other rejects a new attempt after the earlier calls have already ended. The lab stays inside the local dialplan and does not use a carrier trunk.

The example follows the FreeSWITCH v1.11.3 vanilla configuration. Both phones should already register and reach the default context. Use a client that can display SIP responses or client logs. Check that 7198 and 7199 are available and that no catch-all route takes precedence. For a custom context, put the file in its actual include directory and update both transfer targets.

Use a separate number for each limit

Find the configuration root with fs_cli -x 'global_getvar conf_dir'. Verify that dialplan/default.xml includes default/*.xml, then create dialplan/default/19_fs19_limit.xml. Separating concurrency from rate makes the source of each refusal easy to identify.

<include>
  <!-- 7198:测试同一资源同时只允许一个呼叫。 -->
  <extension name="fs19-concurrency">
    <condition field="destination_number" expression="^7198$">
      <!-- 先占位,再接听;超限送往内部拒绝分支。 -->
      <action application="limit" data="hash fs19 concurrency 1 fs19-reject XML default"/>
      <action application="answer"/>
      <!-- 保持 30 秒,给第二部话机留出测试时间。 -->
      <action application="sleep" data="30000"/>
      <action application="hangup" data="NORMAL_CLEARING"/>
    </condition>
  </extension>
  <!-- 7199:测试同一窗口最多放行两次。 -->
  <extension name="fs19-rate">
    <condition field="destination_number" expression="^7199$">
      <action application="limit" data="hash fs19 rate 2/15 fs19-reject XML default"/>
      <!-- 接通即正常挂断,方便连续拨测。 -->
      <action application="answer"/>
      <action application="hangup" data="NORMAL_CLEARING"/>
    </condition>
  </extension>
  <!-- 两条路径共用拒绝响应,日志通过 UUID 关联。 -->
  <extension name="fs19-reject">
    <condition field="destination_number" expression="^fs19-reject$">
      <action application="log" data="WARNING fs19 limit branch uuid=${uuid}"/>
      <action application="respond" data="503 Service Unavailable"/>
    </condition>
  </extension>
</include>

hash selects the backend provided by mod_hash. fs19 is the realm and concurrency is the resource within it. Since both callers reach the same pair, they share a single slot. The threshold 1 permits one active channel against that resource at a time. The limit runs before answer. The 30-second sleep gives you enough time to dial from the other phone.

The second rule uses a different resource named rate. Pass 2/15 as one argument. It allows two attempts per 15-second counting window. In v1.11.3, the first attempt opens the window, and the next attempt after expiration starts a new one. The implementation does not look back over the past 15 seconds for every call. Rejected attempts also increment the rate count within the window. Hanging up frees active usage and leaves the rate count in place until the window expires.

fs19-reject XML default specifies the destination, dialplan type, and context. The branch logs the channel UUID and sends a 503 while the incoming SIP call is still unanswered. When the destination is omitted, the application defaults to limit_exceeded, which requires a matching route. A backend failure also takes the failure branch. A 503 alone cannot identify whether the limit was reached or the backend failed.

Load and test with two phones

Check both modules and reload the XML after saving. The two module_exists commands should return true. If a module is missing, inspect autoload_configs/modules.conf.xml. An installed but unloaded module can be loaded with load mod_hash or load mod_dptools on the test node after checking the scope. Both entries are present in the vanilla template.

# 查看节点和拨号计划需要的模块。
fs_cli -x 'status'
fs_cli -x 'module_exists mod_dptools'
fs_cli -x 'module_exists mod_hash'
# 文件保存后重新加载 XML。
fs_cli -x 'reloadxml'
# 按相同的 realm 和资源名查询。
fs_cli -x 'limit_usage hash fs19 concurrency'
fs_cli -x 'limit_usage hash fs19 rate rate'
  • Dial 7198 from 7101 and keep it connected. The concurrency count should read 1. Within 30 seconds, dial 7198 from 7102. Expect a 503 while 7101 stays connected. Hang up 7101, wait for usage to return to 0, then call again from 7102. It should connect. If both calls connect, check for overlap and confirm from the dialplan log that they used the same resource.
  • Leave 7199 idle for at least 15 seconds. Make three attempts in one 15-second window across 7101 and 7102, letting each call finish before starting the next. The first two should answer and clear immediately. The third should receive a 503. Wait at least 15 seconds and repeat once to verify recovery. The final rate in the CLI command is an option that returns active usage followed by the rate count, separated by a slash. Record timestamps, call results, and the refusal log while testing.

If the phone only shows “busy” or “failed,” read the SIP response and the matching FreeSWITCH rejection log. If even the first call to 7198 fails, check which rule ran. The vanilla template includes default/*.xml after existing rules, so another match may take the call. Choose unused numbers and update both the configuration and the test. This lab uses one node. A local hash count on one node does not enforce a shared allocation across nodes. For a shared limit, verify how the chosen backend handles concurrent updates and failures.

Scope the trunk and core limits separately

On a real route, place the limit before bridge and name the resource for the trunk or allocation it protects. If multiple routes share a carrier allocation, give them the same resource. For tenant quotas, use a trusted tenant identifier mapped after authentication. Keep distinct keys for rate and concurrency when applying both. With the rate check first, an attempt that passes it and then fails the concurrency check will already count toward the rate. Choose the order based on which attempts you want to count. A fallback trunk needs its own allocation and must avoid routing back through the same failure branch. RFC 3261 permits a Retry-After header with 503 and advises SIP clients to try an alternate server. This lab sends no such header. Check the actual upstream retry and failover behavior before adapting the example to a carrier route.

The core configuration in autoload_configs/switch.conf.xml controls the whole node. The settings entries below limit active sessions and new sessions per second. A bridge typically involves an incoming and an outgoing leg. Transfers and parallel dialing affect channel usage as well. Check status for the node and limit_usage for the route. Both measurements matter when planning capacity.

<!-- 修改 switch.conf.xml 的 settings 中现有同名项。 -->
<!-- 这里是 v1.11.3 vanilla 模板示例值。 -->
<param name="max-sessions" value="1000"/>
<param name="sessions-per-second" value="30"/>

The numbers come from the versioned template. The lab changes the short codes only. To tune core settings separately, capture the previous file and running values. fsctl max_sessions <value> and fsctl sps <value> change the running settings. Confirm them with status during a maintenance window and persist approved values in the configuration. Apply changes one node at a time, keep the prior settings for rollback, and check whether normal new calls begin failing after a lower threshold takes effect.

A carrier may count only answered calls. This rule reserves a slot before answer, so ringing and waiting also count. Confirm where the carrier starts counting before comparing the two numbers. When a user reports that a trunk is full, trace one attempt on both sides. Did the local route reach bridge? Did the carrier receive a new INVITE? How did active usage change around answer? That separates local admission decisions from calls handed off to the carrier. Check that your CDR configuration retains unanswered calls if you use it to investigate refusals.

To roll back the lab, move 19_fs19_limit.xml out of the include directory and run reloadxml. Check that 7198 and 7199 follow their original dialplan behavior, then place an ordinary call between 7101 and 7102. The sample was checked against source and configuration. The call results above are expected observations from the lab procedure and were not measured on a running node.


评论