You are here

function background_process_service_group_idle in Background Process 7.2

Load balancing based on most idle clients.

File

./background_process.module, line 468

Code

function background_process_service_group_idle($service_group) {
  $current = array(
    'host' => reset($service_group['hosts']),
    'clients' => -1,
  );
  $service_hosts = background_process_get_service_hosts();
  foreach ($service_group['hosts'] as $host) {
    $clients = background_process_current_clients($host);
    $max_clients = $service_hosts[$host]['max_clients'];
    $available = $max_clients - $clients;
    if ($available && $current['clients'] < $available) {
      $current = array(
        'host' => $host,
        'clients' => $available,
      );
    }
  }
  if ($current['clients'] == -1) {

    // No candidate found, use pseudo round-robin
    $current['host'] = background_process_service_group_round_robin($service_group);
  }
  return $current['host'];
}