irockasterisk: How to set the concurrent calls limit on SIP trunk in Asterisk?

Method2 (preferred):

Looking at cons of using call-limit, I decided not to use it even though I found it to be really easy implementation. Similar goal can be achieved via the use of GROUP() and GROUP-COUNT() functions available in Asterisk Dialplan. You don’t have to make any adjustments on SIP Trunk configuration for call limit (which is good thing).

GROUP() function defines the trunk group
GROUP_COUNT() function returns the number of concurrent calls on the given trunk group

#vi sip.conf
#SIP configuration on Server1

;Call from this trunk lands to [FromServer2Trunk] context
We are directing the call from server2 to [FromServer2Trunk] context in our dialplan.

#vi extensions.conf
#Asterisk dialplan on Server1

exten => _X.,1,Verbose(1,***** Server2 dialing ${EXTEN} *******)
same => n,Set(GROUP()=server2Trunkgroup)
same => n,Verbose(1,**** Number of concurrent calls are ${GROUP_COUNT(server2Trunkgroup)})
same => n,GotoIf($[${GROUP_COUNT(server2Trunkgroup)} > 100]?999)
same => n,Dial(SIP/telco/${EXTEN})

same => 999,Verbose(1,***Number of concurrent calls are ${GROUP_COUNT(server2Trunkgroup)} over limit)

Since we are directing only the calls from Server2 to [FromServer2Trunk] context in our dialplan in Server1, we can safely use GROUP() function to define the name of the trunk group associated with the incoming calls from Server2. I named it as server2Trunkgroup.

Now we can use GROUP_COUNT(server2Trunkgroup) to count the number of concurrent-calls flowing in to Server1 from Server2. We can use GotoIf statement to compare the concurrent calls with threshold value (say 100 in my case). If number of concurrent calls are less than threshold value, accept the call, otherwise drop the call with channel status: CHANUNAVAIL.

You can implement more sophisticated logic to achieve your goal.
I hope this helped you. Good Luck!