Skip to content

Instantly share code, notes, and snippets.

@mender
Last active January 1, 2016 10:19
Show Gist options
  • Select an option

  • Save mender/8130951 to your computer and use it in GitHub Desktop.

Select an option

Save mender/8130951 to your computer and use it in GitHub Desktop.
/*
* Select load balancing node
*/
int select_load_balancing_node(void)
{
int selected_slot;
double total_weight,r;
int i;
/* choose a backend in random manner with weight */
selected_slot = MASTER_NODE_ID;
total_weight = 0.0;
for (i=0;i<NUM_BACKENDS;i++)
{
if (VALID_BACKEND(i))
{
total_weight += BACKEND_INFO(i).backend_weight;
}
}
#if defined(sun) || defined(__sun)
r = (((double)rand())/RAND_MAX) * total_weight;
#else
r = (((double)random())/RAND_MAX) * total_weight;
#endif
total_weight = 0.0;
for (i=0;i<NUM_BACKENDS;i++)
{
if (VALID_BACKEND(i) && BACKEND_INFO(i).backend_weight > 0.0)
{
if(r >= total_weight)
selected_slot = i;
else
break;
total_weight += BACKEND_INFO(i).backend_weight;
}
}
pool_debug("select_load_balancing_node: selected backend id is %d", selected_slot);
return selected_slot;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment