Skip to content

Commit 892c352

Browse files
Add some default factory methods
1 parent ca93833 commit 892c352

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/CorePoolConnectionPoolStrategy.java

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
final class CorePoolConnectionPoolStrategy<C extends LoadBalancedConnection>
4545
implements ConnectionPoolStrategy<C> {
4646

47+
private static final int DEFAULT_CORE_POOL_SIZE = 2;
48+
private static final boolean DEFAULT_FORCE_CORE_POOL = false;
49+
4750
private final int corePoolSize;
4851
private final boolean forceCorePool;
4952

@@ -83,4 +86,18 @@ public C select(List<C> connections, Predicate<C> selector, @Nullable final Cont
8386
// So sad, we didn't find a healthy connection.
8487
return null;
8588
}
89+
90+
ConnectionPoolStrategyFactory<C> defaultFactory() {
91+
return factory(DEFAULT_CORE_POOL_SIZE, DEFAULT_FORCE_CORE_POOL);
92+
}
93+
94+
ConnectionPoolStrategyFactory<C> factory(int corePoolSize, boolean forceCorePool) {
95+
ensureNonNegative(corePoolSize, "corePoolSize");
96+
return new ConnectionPoolStrategyFactory<C>() {
97+
@Override
98+
public <T extends C> ConnectionPoolStrategy<T> buildStrategy() {
99+
return new CorePoolConnectionPoolStrategy<>(corePoolSize, forceCorePool);
100+
}
101+
};
102+
}
86103
}

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/P2CConnectionPoolStrategy.java

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import javax.annotation.Nullable;
2828

2929
import static io.servicetalk.utils.internal.NumberUtils.ensureNonNegative;
30+
import static io.servicetalk.utils.internal.NumberUtils.ensurePositive;
3031
import static java.lang.Math.min;
3132

3233
public class P2CConnectionPoolStrategy<C extends LoadBalancedConnection> implements ConnectionPoolStrategy<C> {
@@ -128,4 +129,16 @@ private C p2cPick(ThreadLocalRandom rnd, int randomSearchSpace, List<C> connecti
128129
// Neither connection was acceptable.
129130
return null;
130131
}
132+
133+
static <C extends LoadBalancedConnection> ConnectionPoolStrategyFactory<C> factory(
134+
final int maxEffort, final int corePoolSize, final boolean forceCorePool) {
135+
ensurePositive(maxEffort, " maxEffort");
136+
ensureNonNegative(corePoolSize, "corePoolSize");
137+
return new ConnectionPoolStrategyFactory<C>() {
138+
@Override
139+
public <T extends C> ConnectionPoolStrategy<T> buildStrategy() {
140+
return new P2CConnectionPoolStrategy<T>(maxEffort, corePoolSize, forceCorePool);
141+
}
142+
};
143+
}
131144
}

0 commit comments

Comments
 (0)