Skip to content

Commit

Permalink
dispatcher: proper filling of weights array when summ is less than 100
Browse files Browse the repository at this point in the history
- more comments on building the array for weight based distribution
  • Loading branch information
miconda committed Dec 15, 2014
1 parent ecd5c58 commit a2b52c5
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions modules/dispatcher/dispatch.c
Expand Up @@ -416,7 +416,10 @@ int add_dest2list(int id, str uri, int flags, int priority, str *attrs,
}

/**
*
* Initialize the weight distribution for a destination set
* - build an array of 0..99 where to keep the index of the
* destination address to be used. The Nth call will use
* the address with the index at possition N%100
*/
int dp_init_weights(ds_set_t *dset)
{
Expand All @@ -431,6 +434,11 @@ int dp_init_weights(ds_set_t *dset)
if(dset->dlist[0].attrs.weight==0)
return 0;

/* first fill the array based on the weight of each destination
* - the weight is the percentage (e.g., if weight=20, the afferent
* address gets its index 20 times in the array)
* - if the sum of weights is more than 100, the addresses over the
* limit are ignored */
t = 0;
for(j=0; j<dset->nr; j++)
{
Expand All @@ -442,10 +450,15 @@ int dp_init_weights(ds_set_t *dset)
t++;
}
}
j = (t-1>=0)?t-1:0;
/* if the array was not completely filled (i.e., the sum of weights is
* less than 100), then use last address to fill the rest */
for(; t<100; t++)
dset->wlist[t] = (unsigned int)j;
dset->wlist[t] = (unsigned int)(dset->nr-1);
randomize:
/* shuffle the content of the array in order to mix the selection
* of the addresses (e.g., if first address has weight=20, avoid
* sending first 20 calls to it, but ensure that within a 100 calls,
* 20 go to first address */
srand(time(0));
for (j=0; j<100; j++)
{
Expand Down

0 comments on commit a2b52c5

Please sign in to comment.