Skip to content

Commit

Permalink
Commonize framework configs (ReactiveX#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Maas authored and RobWin committed Jun 5, 2019
1 parent 06ff9fd commit 30bb4a7
Show file tree
Hide file tree
Showing 107 changed files with 1,609 additions and 1,733 deletions.
4 changes: 4 additions & 0 deletions libraries.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ext {
micrometerVersion = '1.1.4'
hibernateValidatorVersion = '6.0.16.Final'
wiremockVersion = '2.22.0'
validationApiVersion = '2.0.1.Final'
kotlinCoroutinesVersion = '1.2.0'

libraries = [
Expand Down Expand Up @@ -114,6 +115,9 @@ ext {

// Groovy
groovy: "org.codehaus.groovy:groovy-all:2.5.6",

// validation
validationApi: "javax.validation:validation-api:${validationApiVersion}",

// Kotlin addon
kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib-jdk8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.resilience4j.core;

import io.github.resilience4j.core.lang.Nullable;

import java.lang.reflect.Constructor;
import java.util.function.Predicate;

public class ClassUtils {

@Nullable
@SuppressWarnings("unchecked")
public static <T> Predicate<T> instantiatePredicateClass(Class<? extends Predicate<T>> clazz) {
try {
Constructor<? extends Predicate> c = clazz.getConstructor();
if (c != null) {
return c.newInstance();
} else {
return null;
}
} catch (Exception e) {
throw new InstantiationException("Unable to create instance of class: " + clazz.getName(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.resilience4j.core;

public class InstantiationException extends RuntimeException {

public InstantiationException() {
}

public InstantiationException(String message) {
super(message);
}

public InstantiationException(String message, Throwable cause) {
super(message, cause);
}

public InstantiationException(Throwable cause) {
super(cause);
}

public InstantiationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.github.resilience4j.core;

import io.github.resilience4j.core.lang.Nullable;

public class StringUtils {

public static boolean isNotEmpty(@Nullable String string) {
return string != null && !string.isEmpty();
}

}
3 changes: 3 additions & 0 deletions resilience4j-framework-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ dependencies {
compile project(':resilience4j-ratelimiter')
compile project(':resilience4j-retry')
compile project(':resilience4j-bulkhead')

compile libraries.validationApi
compile libraries.hibernate_validator
}

ext.moduleName='io.github.resilience4j.framework-common'
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 2019 Dan Maas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.common.bulkhead.configuration;

import io.github.resilience4j.core.ConfigurationNotFoundException;
import io.github.resilience4j.core.StringUtils;
import io.github.resilience4j.core.lang.Nullable;

import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.Map;

public class BulkheadConfigurationProperties {

private Map<String, InstanceProperties> instances = new HashMap<>();
private Map<String, InstanceProperties> configs = new HashMap<>();

public io.github.resilience4j.bulkhead.BulkheadConfig createBulkheadConfig(InstanceProperties instanceProperties) {
if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) {
InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig());
if (baseProperties == null) {
throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig());
}
return buildConfigFromBaseConfig(baseProperties, instanceProperties);
}
return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), instanceProperties);
}

private io.github.resilience4j.bulkhead.BulkheadConfig buildConfigFromBaseConfig(InstanceProperties baseProperties, InstanceProperties instanceProperties) {
io.github.resilience4j.bulkhead.BulkheadConfig baseConfig = buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), baseProperties);
return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.from(baseConfig), instanceProperties);
}

private io.github.resilience4j.bulkhead.BulkheadConfig buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.Builder builder, InstanceProperties instanceProperties) {
if (instanceProperties.getMaxConcurrentCalls() != null) {
builder.maxConcurrentCalls(instanceProperties.getMaxConcurrentCalls());
}
if (instanceProperties.getMaxWaitTime() != null) {
builder.maxWaitTime(instanceProperties.getMaxWaitTime());
}
return builder.build();
}

@Nullable
public InstanceProperties getBackendProperties(String backend) {
return instances.get(backend);
}

public Map<String, InstanceProperties> getInstances() {
return instances;
}

/**
* For backwards compatibility when setting backends in configuration properties.
*/
public Map<String, InstanceProperties> getBackends() {
return instances;
}

public Map<String, InstanceProperties> getConfigs() {
return configs;
}

/**
* Bulkhead config adapter for integration with Ratpack. {@link #maxWaitTime} should
* almost always be set to 0, so the compute threads would not be blocked upon execution.
*/
public static class InstanceProperties {

@Min(1)
private Integer maxConcurrentCalls;
@Min(0)
private Long maxWaitTime;
@Nullable
private String baseConfig;
@Min(1)
private Integer eventConsumerBufferSize = 100;

public InstanceProperties setMaxConcurrentCalls(Integer maxConcurrentCalls) {
this.maxConcurrentCalls = maxConcurrentCalls;
return this;
}

public InstanceProperties setMaxWaitTime(Long maxWaitTime) {
this.maxWaitTime = maxWaitTime;
return this;
}

public InstanceProperties setBaseConfig(String baseConfig) {
this.baseConfig = baseConfig;
return this;
}

public InstanceProperties eventConsumerBufferSize(Integer eventConsumerBufferSize) {
this.eventConsumerBufferSize = eventConsumerBufferSize;
return this;
}

public Integer getMaxConcurrentCalls() {
return maxConcurrentCalls;
}

public Long getMaxWaitTime() {
return maxWaitTime;
}

public String getBaseConfig() {
return baseConfig;
}

public Integer getEventConsumerBufferSize() {
return eventConsumerBufferSize;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.bulkhead.monitoring.endpoint;
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.bulkhead.monitoring.endpoint;
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.bulkhead.monitoring.endpoint;
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.resilience4j.bulkhead.monitoring.endpoint;
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
@NonNullApi
@NonNullFields
package io.github.resilience4j.circuitbreaker.monitoring.endpoint;
package io.github.resilience4j.common.bulkhead.monitoring.endpoint;

import io.github.resilience4j.core.lang.NonNullApi;
import io.github.resilience4j.core.lang.NonNullFields;
Loading

0 comments on commit 30bb4a7

Please sign in to comment.