Skip to content

Commit ef6b201

Browse files
author
Matt Jacobs
committed
Add configuration for Hystrix thread pool maximumSize
1 parent 4ec663e commit ef6b201

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

hystrix-core/src/main/java/com/netflix/hystrix/HystrixThreadPoolProperties.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package com.netflix.hystrix;
1717

18-
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forBoolean;
1918
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forInteger;
20-
import static com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty.forString;
2119

2220
import java.util.concurrent.BlockingQueue;
2321
import java.util.concurrent.ThreadPoolExecutor;
@@ -32,11 +30,19 @@
3230
* Properties for instances of {@link HystrixThreadPool}.
3331
* <p>
3432
* Default implementation of methods uses Archaius (https://github.com/Netflix/archaius)
33+
*
34+
* Note a change in behavior in 1.5.7. Prior to that version, the configuration for 'coreSize' was used to control
35+
* both coreSize and maximumSize. This is a fixed-size threadpool that can never give up an unused thread. In 1.5.7+,
36+
* the values can diverge, and if you set coreSize < maximumSize, threads can be given up (subject to the keep-alive
37+
* time)
38+
*
39+
* It is OK to leave maximumSize unset using any version of Hystrix. If you do, then maximum size will default to
40+
* core size and you'll have a fixed-size threadpool.
3541
*/
3642
public abstract class HystrixThreadPoolProperties {
3743

3844
/* defaults */
39-
private Integer default_coreSize = 10; // size of thread pool
45+
private Integer default_coreSize = 10; // core size of thread pool
4046
private Integer default_keepAliveTimeMinutes = 1; // minutes to keep a thread alive (though in practice this doesn't get used as by default we set a fixed size)
4147
private Integer default_maxQueueSize = -1; // size of queue (this can't be dynamically changed so we use 'queueSizeRejectionThreshold' to artificially limit and reject)
4248
// -1 turns if off and makes us use SynchronousQueue
@@ -45,6 +51,7 @@ public abstract class HystrixThreadPoolProperties {
4551
private Integer default_threadPoolRollingNumberStatisticalWindowBuckets = 10; // number of buckets in rolling number (10 1-second buckets)
4652

4753
private final HystrixProperty<Integer> corePoolSize;
54+
private final HystrixProperty<Integer> maximumPoolSize;
4855
private final HystrixProperty<Integer> keepAliveTime;
4956
private final HystrixProperty<Integer> maxQueueSize;
5057
private final HystrixProperty<Integer> queueSizeRejectionThreshold;
@@ -61,6 +68,8 @@ protected HystrixThreadPoolProperties(HystrixThreadPoolKey key, Setter builder)
6168

6269
protected HystrixThreadPoolProperties(HystrixThreadPoolKey key, Setter builder, String propertyPrefix) {
6370
this.corePoolSize = getProperty(propertyPrefix, key, "coreSize", builder.getCoreSize(), default_coreSize);
71+
//if maximum size is not explicitly set, then default it to the core size of that pool.
72+
this.maximumPoolSize = getProperty(propertyPrefix, key, "maximumSize", builder.getMaximumSize(), builder.getCoreSize());
6473
this.keepAliveTime = getProperty(propertyPrefix, key, "keepAliveTimeMinutes", builder.getKeepAliveTimeMinutes(), default_keepAliveTimeMinutes);
6574
this.maxQueueSize = getProperty(propertyPrefix, key, "maxQueueSize", builder.getMaxQueueSize(), default_maxQueueSize);
6675
this.queueSizeRejectionThreshold = getProperty(propertyPrefix, key, "queueSizeRejectionThreshold", builder.getQueueSizeRejectionThreshold(), default_queueSizeRejectionThreshold);
@@ -84,6 +93,15 @@ public HystrixProperty<Integer> coreSize() {
8493
return corePoolSize;
8594
}
8695

96+
/**
97+
* Maximum thread-pool size that gets passed to {@link ThreadPoolExecutor#setMaximumPoolSize(int)}
98+
*
99+
* @return {@code HystrixProperty<Integer>}
100+
*/
101+
public HystrixProperty<Integer> maximumSize() {
102+
return maximumPoolSize;
103+
}
104+
87105
/**
88106
* Keep-alive time in minutes that gets passed to {@link ThreadPoolExecutor#setKeepAliveTime(long, TimeUnit)}
89107
*
@@ -169,6 +187,7 @@ public static Setter defaultSetter() {
169187
*/
170188
public static class Setter {
171189
private Integer coreSize = null;
190+
private Integer maximumSize = null;
172191
private Integer keepAliveTimeMinutes = null;
173192
private Integer maxQueueSize = null;
174193
private Integer queueSizeRejectionThreshold = null;
@@ -182,6 +201,10 @@ public Integer getCoreSize() {
182201
return coreSize;
183202
}
184203

204+
public Integer getMaximumSize() {
205+
return maximumSize;
206+
}
207+
185208
public Integer getKeepAliveTimeMinutes() {
186209
return keepAliveTimeMinutes;
187210
}
@@ -207,6 +230,11 @@ public Setter withCoreSize(int value) {
207230
return this;
208231
}
209232

233+
public Setter withMaximumSize(int value) {
234+
this.maximumSize = value;
235+
return this;
236+
}
237+
210238
public Setter withKeepAliveTimeMinutes(int value) {
211239
this.keepAliveTimeMinutes = value;
212240
return this;
@@ -237,7 +265,8 @@ public Setter withMetricsRollingStatisticalWindowBuckets(int value) {
237265
*/
238266
/* package */static Setter getUnitTestPropertiesBuilder() {
239267
return new Setter()
240-
.withCoreSize(10)// size of thread pool
268+
.withCoreSize(10)// core size of thread pool
269+
.withMaximumSize(15) //maximum size of thread pool
241270
.withKeepAliveTimeMinutes(1)// minutes to keep a thread alive (though in practice this doesn't get used as by default we set a fixed size)
242271
.withMaxQueueSize(100)// size of queue (but we never allow it to grow this big ... this can't be dynamically changed so we use 'queueSizeRejectionThreshold' to artificially limit and reject)
243272
.withQueueSizeRejectionThreshold(10)// number of items in queue at which point we reject (this can be dyamically changed)
@@ -260,6 +289,11 @@ public HystrixProperty<Integer> coreSize() {
260289
return HystrixProperty.Factory.asProperty(builder.coreSize);
261290
}
262291

292+
@Override
293+
public HystrixProperty<Integer> maximumSize() {
294+
return HystrixProperty.Factory.asProperty(builder.maximumSize);
295+
}
296+
263297
@Override
264298
public HystrixProperty<Integer> keepAliveTimeMinutes() {
265299
return HystrixProperty.Factory.asProperty(builder.keepAliveTimeMinutes);

0 commit comments

Comments
 (0)