Skip to content

Commit 30bb4a7

Browse files
Dan MaasRobWin
Dan Maas
authored andcommitted
Commonize framework configs (ReactiveX#460)
1 parent 06ff9fd commit 30bb4a7

File tree

107 files changed

+1609
-1733
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1609
-1733
lines changed

libraries.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ext {
2727
micrometerVersion = '1.1.4'
2828
hibernateValidatorVersion = '6.0.16.Final'
2929
wiremockVersion = '2.22.0'
30+
validationApiVersion = '2.0.1.Final'
3031
kotlinCoroutinesVersion = '1.2.0'
3132

3233
libraries = [
@@ -114,6 +115,9 @@ ext {
114115

115116
// Groovy
116117
groovy: "org.codehaus.groovy:groovy-all:2.5.6",
118+
119+
// validation
120+
validationApi: "javax.validation:validation-api:${validationApiVersion}",
117121

118122
// Kotlin addon
119123
kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib-jdk8",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.resilience4j.core;
2+
3+
import io.github.resilience4j.core.lang.Nullable;
4+
5+
import java.lang.reflect.Constructor;
6+
import java.util.function.Predicate;
7+
8+
public class ClassUtils {
9+
10+
@Nullable
11+
@SuppressWarnings("unchecked")
12+
public static <T> Predicate<T> instantiatePredicateClass(Class<? extends Predicate<T>> clazz) {
13+
try {
14+
Constructor<? extends Predicate> c = clazz.getConstructor();
15+
if (c != null) {
16+
return c.newInstance();
17+
} else {
18+
return null;
19+
}
20+
} catch (Exception e) {
21+
throw new InstantiationException("Unable to create instance of class: " + clazz.getName(), e);
22+
}
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.github.resilience4j.core;
2+
3+
public class InstantiationException extends RuntimeException {
4+
5+
public InstantiationException() {
6+
}
7+
8+
public InstantiationException(String message) {
9+
super(message);
10+
}
11+
12+
public InstantiationException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
public InstantiationException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
public InstantiationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
21+
super(message, cause, enableSuppression, writableStackTrace);
22+
}
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.resilience4j.core;
2+
3+
import io.github.resilience4j.core.lang.Nullable;
4+
5+
public class StringUtils {
6+
7+
public static boolean isNotEmpty(@Nullable String string) {
8+
return string != null && !string.isEmpty();
9+
}
10+
11+
}

resilience4j-framework-common/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ dependencies {
44
compile project(':resilience4j-ratelimiter')
55
compile project(':resilience4j-retry')
66
compile project(':resilience4j-bulkhead')
7+
8+
compile libraries.validationApi
9+
compile libraries.hibernate_validator
710
}
811

912
ext.moduleName='io.github.resilience4j.framework-common'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2019 Dan Maas
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.resilience4j.common.bulkhead.configuration;
17+
18+
import io.github.resilience4j.core.ConfigurationNotFoundException;
19+
import io.github.resilience4j.core.StringUtils;
20+
import io.github.resilience4j.core.lang.Nullable;
21+
22+
import javax.validation.constraints.Min;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class BulkheadConfigurationProperties {
27+
28+
private Map<String, InstanceProperties> instances = new HashMap<>();
29+
private Map<String, InstanceProperties> configs = new HashMap<>();
30+
31+
public io.github.resilience4j.bulkhead.BulkheadConfig createBulkheadConfig(InstanceProperties instanceProperties) {
32+
if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) {
33+
InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig());
34+
if (baseProperties == null) {
35+
throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig());
36+
}
37+
return buildConfigFromBaseConfig(baseProperties, instanceProperties);
38+
}
39+
return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), instanceProperties);
40+
}
41+
42+
private io.github.resilience4j.bulkhead.BulkheadConfig buildConfigFromBaseConfig(InstanceProperties baseProperties, InstanceProperties instanceProperties) {
43+
io.github.resilience4j.bulkhead.BulkheadConfig baseConfig = buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), baseProperties);
44+
return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.from(baseConfig), instanceProperties);
45+
}
46+
47+
private io.github.resilience4j.bulkhead.BulkheadConfig buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.Builder builder, InstanceProperties instanceProperties) {
48+
if (instanceProperties.getMaxConcurrentCalls() != null) {
49+
builder.maxConcurrentCalls(instanceProperties.getMaxConcurrentCalls());
50+
}
51+
if (instanceProperties.getMaxWaitTime() != null) {
52+
builder.maxWaitTime(instanceProperties.getMaxWaitTime());
53+
}
54+
return builder.build();
55+
}
56+
57+
@Nullable
58+
public InstanceProperties getBackendProperties(String backend) {
59+
return instances.get(backend);
60+
}
61+
62+
public Map<String, InstanceProperties> getInstances() {
63+
return instances;
64+
}
65+
66+
/**
67+
* For backwards compatibility when setting backends in configuration properties.
68+
*/
69+
public Map<String, InstanceProperties> getBackends() {
70+
return instances;
71+
}
72+
73+
public Map<String, InstanceProperties> getConfigs() {
74+
return configs;
75+
}
76+
77+
/**
78+
* Bulkhead config adapter for integration with Ratpack. {@link #maxWaitTime} should
79+
* almost always be set to 0, so the compute threads would not be blocked upon execution.
80+
*/
81+
public static class InstanceProperties {
82+
83+
@Min(1)
84+
private Integer maxConcurrentCalls;
85+
@Min(0)
86+
private Long maxWaitTime;
87+
@Nullable
88+
private String baseConfig;
89+
@Min(1)
90+
private Integer eventConsumerBufferSize = 100;
91+
92+
public InstanceProperties setMaxConcurrentCalls(Integer maxConcurrentCalls) {
93+
this.maxConcurrentCalls = maxConcurrentCalls;
94+
return this;
95+
}
96+
97+
public InstanceProperties setMaxWaitTime(Long maxWaitTime) {
98+
this.maxWaitTime = maxWaitTime;
99+
return this;
100+
}
101+
102+
public InstanceProperties setBaseConfig(String baseConfig) {
103+
this.baseConfig = baseConfig;
104+
return this;
105+
}
106+
107+
public InstanceProperties eventConsumerBufferSize(Integer eventConsumerBufferSize) {
108+
this.eventConsumerBufferSize = eventConsumerBufferSize;
109+
return this;
110+
}
111+
112+
public Integer getMaxConcurrentCalls() {
113+
return maxConcurrentCalls;
114+
}
115+
116+
public Long getMaxWaitTime() {
117+
return maxWaitTime;
118+
}
119+
120+
public String getBaseConfig() {
121+
return baseConfig;
122+
}
123+
124+
public Integer getEventConsumerBufferSize() {
125+
return eventConsumerBufferSize;
126+
}
127+
128+
}
129+
130+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.github.resilience4j.bulkhead.monitoring.endpoint;
16+
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;
1717

1818
import java.util.List;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.github.resilience4j.bulkhead.monitoring.endpoint;
16+
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;
1717

1818
import io.github.resilience4j.bulkhead.event.BulkheadEvent;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.github.resilience4j.bulkhead.monitoring.endpoint;
16+
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;
1717

1818
import io.github.resilience4j.bulkhead.event.BulkheadEvent;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.github.resilience4j.bulkhead.monitoring.endpoint;
16+
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;
1717

1818
import java.util.List;
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
@NonNullApi
2020
@NonNullFields
21-
package io.github.resilience4j.circuitbreaker.monitoring.endpoint;
21+
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;
2222

2323
import io.github.resilience4j.core.lang.NonNullApi;
2424
import io.github.resilience4j.core.lang.NonNullFields;

0 commit comments

Comments
 (0)