[SR-Users] Dispatcher / ds_select_routes

Alex Balashov abalashov at evaristesys.com
Sat Sep 5 00:23:52 CEST 2020

Hi Peter,

I've never used ds_select_routes(), but--taking a stab in the dark--it 
seems to me the principle would be something like:

    modparam("dispatcher", "flags", 2)

    route[...] {

       if(!ds_select_routes("...", "d", "0")) {
          sl_send_reply("503", "No initial routes");



     failure_route[REROUTE] {

        if(!ds_next_dst()) {
           send_reply("503", "Out of routes");


A few important subtleties for which the documentation is indeed a bit 

- "Failover" of any kind is made possible by storing a list of 
destinations, in ascending order of preference based on the dispatcher 
algorithm chosen (and the way all the groups are concatenated, in the 
particular case of ds_select_routes()), in a transaction-persistent data 
structure called an XAVP. But this behaviour is not enabled by default, 
and is enabled by setting bit 2 in the "flags" modparam to dispatcher, 
as in the example above.

- Because the chosen destination is being pushed into the destination 
URI (Kamailio-idiosyncratic next-hop URI) rather than the domain part of 
the Request URI (mode "d" for ds_select_routes()), you must use 
ds_next_dst() to iterate to the next step in the result set. 
ds_next_domain() would be appropriate only if the next destination to 
attempt were being stored in the "ruri", as is the case in the docs 
example but not in the code you posted. So, use ds_next_dst() in your 
failure_route instead.

-- Alex

On 9/4/20 5:53 PM, Peter Gradwell wrote:
> Hello
> I'm doing a project where I need to route using dispatcher across 
> multiple groups. i.e. when the customer makes a call, the "core" server 
> makes a request to cgrates to get back a list of carriers that support 
> that, and then want to work through them in order of preference.
> e.g. the string we get back from cgrates via evapi might be "2,1,3". 
> i.e. try all Group 2 destinations, then group 1, then group 3.
> Each of these is then a dispatcher group (with multiple dispatcher 
> routes in each group).
> When the call then gets to the "Group 2" server that in turn is also 
> using dispatcher to handle multiple destinations, and if all of those 
> destinations fail, it returns a 503 to the "core", which is then meant 
> to try "Group 1", and so on.
> The documentation on this is a bit sparse, but what I think I am meant 
> to do (at the core level) is use ds_select_routes to load up the list of 
> groups 
> (https://kamailio.org/docs/modules/5.3.x/modules/dispatcher.html#dispatcher.f.ds_select_routes) 
> if(ds_select_routes("2=4;1=4;3=4", "d", "0"))
> {
>      t_on_failure("REROUTE");
>      t_relay();
>      exit;
> }
> and then in my failure_route, if the error code is 503 I call 
> ds_next_domain, e.g.
> failure_route[REROUTE] {
>      if(t_check_status("503")) {
>          if(ds_next_domain()) {
>              t_on_failure("REROUTE");
>              t_relay();
>              exit;
>          }
>      }
> }
> My problem is, that in applying theory to practice, I have ended up with 
> a bunch of looping and it's not working right!
> Does anyone have an example code for such a scenario?
> many thanks
> peter
> _______________________________________________
> Kamailio (SER) - Users Mailing List
> sr-users at lists.kamailio.org
> https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-users

Alex Balashov | Principal | Evariste Systems LLC

Tel: +1-706-510-6800 / +1-800-250-5920 (toll-free)
Web: http://www.evaristesys.com/, http://www.csrpswitch.com/

More information about the sr-users mailing list