1
#include "source/extensions/load_balancing_policies/random/random_lb.h"
2

            
3
namespace Envoy {
4
namespace Upstream {
5

            
6
6
HostConstSharedPtr RandomLoadBalancer::peekAnotherHost(LoadBalancerContext* context) {
7
6
  if (tooManyPreconnects(stashed_random_.size(), total_healthy_hosts_)) {
8
2
    return nullptr;
9
2
  }
10
4
  return peekOrChoose(context, true);
11
6
}
12

            
13
16700
HostConstSharedPtr RandomLoadBalancer::chooseHostOnce(LoadBalancerContext* context) {
14
16700
  return peekOrChoose(context, false);
15
16700
}
16

            
17
16704
HostConstSharedPtr RandomLoadBalancer::peekOrChoose(LoadBalancerContext* context, bool peek) {
18
16704
  uint64_t random_hash = random(peek);
19
16704
  const absl::optional<HostsSource> hosts_source = hostSourceToUse(context, random_hash);
20
16704
  if (!hosts_source) {
21
2
    return nullptr;
22
2
  }
23

            
24
16702
  const HostVector& hosts_to_use = hostSourceToHosts(*hosts_source);
25
16702
  if (hosts_to_use.empty()) {
26
2
    return nullptr;
27
2
  }
28

            
29
16700
  return hosts_to_use[random_hash % hosts_to_use.size()];
30
16702
}
31

            
32
} // namespace Upstream
33
} // namespace Envoy