[Kamailio-Users] htable - grabbing a value randomly

Mik Cheez michael_bulk at wildgate.com
Thu Mar 12 00:16:30 CET 2009


Daniel-Constantin Mierla wrote:
> Hello,
> 
> On 03/11/2009 06:21 PM, Mik Cheez wrote:
>> I'm hoping to store several gateway IP's in a hash table using HTable, 
>> then grab one of them randomly.  Does anyone have a recommendation as 
>> to how this can be done, or perhaps a better way to do it?  I chose 
>> HTable because a) I want to reload the values every x minutes and b) 
>> I'm using it for other purposes.
>>   
> add in htable several keys:
> - "gateway[x]"  - where x is 0 to the number of gateways
> - "gateway::size" -  where to store the number of gateways
> 
> Then use the cfgutils module and get your gateway with:
> $var(x) = $RANDOM % $sht(gateway::size);
> $var(gwip) = $sht(gateway[$var(x)]);
> 
> FYI - although you may know - dispatcher has an algorithm for random 
> selection of destination.
> 
> Cheers,
> Daniel
> 


Very cool Daniel . . . thanks for the quick response.  There is one 
question, however; What I'm hoping to do is keep x number of gateways in 
the hash table, letting them expire after 10 minutes.  If the system 
finds that the entries are gone (e.g. $sht(a=>gateway) == null ) then it 
would reload the table.  My reason for doing this is in case I need to 
take one of the gateways out or add another.

Any thoughts, or am I missing something?

Mik



More information about the Users mailing list