Being a trunk isn’t relevant here, and isn’t a term that SIP uses. You have different user, as well as domain, parts.
One way is with a queue, with an appropriate strategy.
Another way is with a global variable that you increment for each call and test for odd or even… You can probably tolerate race conditions, but if not, you may have to use locks around the increment and test.
If this is a new system, please consider chan_psjip, instead of chan_sip, as support for the latter is limited.
The channel variable ‘uniqueid’ consists of the ‘EPOCH’ (seconds since Unix time 0), a period, and the number of channels created since this instance of Asterisk was started. You could ‘even/odd’ this part and get a pretty good distribution depending on other channel creation activity.
(Please wrap code and console snippets in preformatted text tags so the forum doesn’t mung special characters like quotes and dollar signs.)
Not so much wrong, as not understanding.
When the call exits, the dial() application does not (usually) return to the next priority in the dialplan. Execution usually continues at the ‘h’ extension. Thus, your set()'s are never executed.
This default behavior can be changed with the ‘g’ option.
You can modify your dialplan so the set() is executed before the dial() or you can add the ‘g’ option. Personally, I’d go with the set() before the dial(). It seems more obvious and it may save the next guy a trip to the dox to figure out what ‘g’ does.
A few other suggestions on your dialplan:
Use same = n for a lot of reasons. Clarity, reliability, debugging, maintainability, etc.
Add some whitespace to ease reading and maintenance like:
same = n(dial2), set(GLOBAL(MYGLOBALVAR)=0)
same = n, dial(sip/7604683867@vitel-outbound,30,r)
same = n, hangup()
Start your context with something to help with debugging like:
same = n, verbose(1,[${EXTEN}@${CONTEXT}!${MYGLOBALVAR}])
Use a more descriptive variable name like DISTRIBUTOR
The GLOBAL() function is not needed to read the global variable value unless you also have a channel variable of the same name. After thinking about it for a couple billion nanoseconds, keep it in so it is more obvious to the next guy and everything won’t fall down if somebody hacks in a channel variable of the same name for some other purpose.
The first gotoif() is unnecessary.
(Note that several of these suggestions are just my personal preferences.)
So if I were writing this dialplan using a global variable, it would look something like:
; distribute calls between destinations
exten = 345687,1, verbose(1,[${EXTEN}@${CONTEXT}!${DISTRIBUTOR}])
same = n, gotoif($["${GLOBAL(DISTRIBUTOR)}" = "1"]?destination1)
; destination 0
same = n, set(GLOBAL(DISTRIBUTOR)=1)
same = n, dial(SIP/458686@test_new1)
same = n, hangup()
; destination 1
same = n(destination1), set(GLOBAL(DISTRIBUTOR)=0)
same = n, dial(SIP/458686@test_new2)
same = n, hangup()