From a2b52c52e8a2503c53825b82fb66d7af11b9bc4e Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Mon, 15 Dec 2014 12:37:42 +0100 Subject: [PATCH] dispatcher: proper filling of weights array when summ is less than 100 - more comments on building the array for weight based distribution --- modules/dispatcher/dispatch.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/dispatcher/dispatch.c b/modules/dispatcher/dispatch.c index e8da58b54f7..434593a402e 100644 --- a/modules/dispatcher/dispatch.c +++ b/modules/dispatcher/dispatch.c @@ -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) { @@ -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; jnr; j++) { @@ -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++) {