Limiting inbound calls in FreeSWITCH

I have fallen into this trap so many times and keep forgetting about it. I set a limit for an inbound number but still calls go through even they where supposed to be rejected. What gives? FreeSWTICH?

Limiting inbound calls in FreeSWITCH

limit feature

Limiting calls in FreeSWITCH seem to be simple at first but they can be tricky.

Consulting the FreeSWITCH documentation on limit it really looks simple.

limit <backend> <realm> <resource> <max[/interval]> [<transfer_destination_number> [<dialplan> [<context>]]

Putting this in a lua script would then look like this:

session:execute("limit", "hash inbound" .." ".. result.pfx .." ".. 1 .." !USER_BUSY")

Now make an inbound call, and in fs_cli you can check the limit counter

limit_usage hash inbound <number goes here>

If you are not answering the call on this FreeSWITCH machine put sending it out to a gateway, you will see a value of 0 and the second call which should be rejected will be processed as there was no call on the machine.

wtf FreeSWITCH?

Well, the thing is, as soon as the call is bridged, the limit counter is being decreased so that’s no help at all to limit the number of concurrent calls through your system!

execute_on_answer to the rescue

What you need to do is to increase the counter when the b-leg is answered. This can be accomplished with execute_on_answer

But you also want to ensure it’s being set on the b-leg by prefixing it with a nolocal:

For more details consult the FreeSWITCH page on execute_on_answer

The Lua script now will look like this, where result.pfx is our dialed destination number, we set up a counter for every inbound number.

session:execute("limit", "hash inbound" .." ".. result.pfx .." ".. 1 .." !USER_BUSY")
session:setVariable("nolocal:execute_on_answer", "limit hash inbound" .." ".. result.pfx .." ".. 1 .." !USER_BUSY")

Now if you check fs_cli you will see that the counter goes up for each incoming call when it’s answered, a second call will be rejected with USER_BUSY and no new call will be sent to the gateway for the same destination_number until a call is hung up.