Skip to content

Commit 78d884c

Browse files
authored
Optimize instance selection performance (#1363)
1 parent 4a71291 commit 78d884c

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/RandomLoadBalancer.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,11 +29,13 @@
2929
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
3030
import org.springframework.cloud.client.loadbalancer.Request;
3131
import org.springframework.cloud.client.loadbalancer.Response;
32+
import org.springframework.util.function.SingletonSupplier;
3233

3334
/**
3435
* A random-based implementation of {@link ReactorServiceInstanceLoadBalancer}.
3536
*
3637
* @author Olga Maciaszek-Sharma
38+
* @author Nan Chiu
3739
* @since 2.2.7
3840
*/
3941
public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
@@ -42,7 +44,7 @@ public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
4244

4345
private final String serviceId;
4446

45-
private ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
47+
private final SingletonSupplier<ServiceInstanceListSupplier> serviceInstanceListSingletonSupplier;
4648

4749
/**
4850
* @param serviceInstanceListSupplierProvider a provider of
@@ -52,14 +54,15 @@ public class RandomLoadBalancer implements ReactorServiceInstanceLoadBalancer {
5254
public RandomLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
5355
String serviceId) {
5456
this.serviceId = serviceId;
55-
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
57+
this.serviceInstanceListSingletonSupplier = SingletonSupplier.of(
58+
() -> serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new)
59+
);
5660
}
5761

5862
@SuppressWarnings("rawtypes")
5963
@Override
6064
public Mono<Response<ServiceInstance>> choose(Request request) {
61-
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
62-
.getIfAvailable(NoopServiceInstanceListSupplier::new);
65+
ServiceInstanceListSupplier supplier = serviceInstanceListSingletonSupplier.obtain();
6366
return supplier.get(request)
6467
.next()
6568
.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));

spring-cloud-loadbalancer/src/main/java/org/springframework/cloud/loadbalancer/core/RoundRobinLoadBalancer.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,13 +30,15 @@
3030
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
3131
import org.springframework.cloud.client.loadbalancer.Request;
3232
import org.springframework.cloud.client.loadbalancer.Response;
33+
import org.springframework.util.function.SingletonSupplier;
3334

3435
/**
3536
* A Round-Robin-based implementation of {@link ReactorServiceInstanceLoadBalancer}.
3637
*
3738
* @author Spencer Gibb
3839
* @author Olga Maciaszek-Sharma
3940
* @author Zhuozhi JI
41+
* @author Nan Chiu
4042
*/
4143
public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
4244

@@ -46,7 +48,7 @@ public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalance
4648

4749
final String serviceId;
4850

49-
ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider;
51+
private final SingletonSupplier<ServiceInstanceListSupplier> serviceInstanceListSingletonSupplier;
5052

5153
/**
5254
* @param serviceInstanceListSupplierProvider a provider of
@@ -67,7 +69,9 @@ public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> servic
6769
public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> serviceInstanceListSupplierProvider,
6870
String serviceId, int seedPosition) {
6971
this.serviceId = serviceId;
70-
this.serviceInstanceListSupplierProvider = serviceInstanceListSupplierProvider;
72+
this.serviceInstanceListSingletonSupplier = SingletonSupplier.of(
73+
() -> serviceInstanceListSupplierProvider.getIfAvailable(NoopServiceInstanceListSupplier::new)
74+
);
7175
this.position = new AtomicInteger(seedPosition);
7276
}
7377

@@ -77,8 +81,7 @@ public RoundRobinLoadBalancer(ObjectProvider<ServiceInstanceListSupplier> servic
7781
// https://github.com/Netflix/ocelli/blob/master/ocelli-core/
7882
// src/main/java/netflix/ocelli/loadbalancer/RoundRobinLoadBalancer.java
7983
public Mono<Response<ServiceInstance>> choose(Request request) {
80-
ServiceInstanceListSupplier supplier = serviceInstanceListSupplierProvider
81-
.getIfAvailable(NoopServiceInstanceListSupplier::new);
84+
ServiceInstanceListSupplier supplier = serviceInstanceListSingletonSupplier.obtain();
8285
return supplier.get(request)
8386
.next()
8487
.map(serviceInstances -> processInstanceResponse(supplier, serviceInstances));

0 commit comments

Comments
 (0)