Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added config support for bulkheads, breakers, timeouts and retries (master) #4357

Merged
merged 1 commit into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions fault-tolerance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-metadata-processor</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
Expand All @@ -62,5 +74,10 @@
<artifactId>helidon-config-testing</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

/**
* Bulkhead protects a resource that cannot serve unlimited parallel
Expand All @@ -44,6 +47,7 @@ static Builder builder() {
/**
* Fluent API builder for {@link io.helidon.faulttolerance.Bulkhead}.
*/
@Configured
class Builder implements io.helidon.common.Builder<Builder, Bulkhead> {
private static final int DEFAULT_LIMIT = 10;
private static final int DEFAULT_QUEUE_LENGTH = 10;
Expand Down Expand Up @@ -80,6 +84,7 @@ public Builder executor(Supplier<? extends ExecutorService> executor) {
* @param limit maximal number of parallel calls, defaults is {@value DEFAULT_LIMIT}
* @return updated builder instance
*/
@ConfiguredOption("10")
public Builder limit(int limit) {
this.limit = limit;
return this;
Expand All @@ -93,6 +98,7 @@ public Builder limit(int limit) {
* @param queueLength length of queue
* @return updated builder instance
*/
@ConfiguredOption("10")
public Builder queueLength(int queueLength) {
this.queueLength = queueLength;
return this;
Expand All @@ -104,6 +110,7 @@ public Builder queueLength(int queueLength) {
* @param name the name
* @return updated builder instance
*/
@ConfiguredOption("Bulkhead-")
public Builder name(String name) {
this.name = name;
return this;
Expand All @@ -116,11 +123,56 @@ public Builder name(String name) {
* @param cancelSource cancel source policy
* @return updated builder instance
*/
@ConfiguredOption("true")
public Builder cancelSource(boolean cancelSource) {
this.cancelSource = cancelSource;
return this;
}

/**
* <p>
* Load all properties for this bulkhead from configuration.
* </p>
* <table class="config">
* <caption>Configuration</caption>
* <tr>
* <th>key</th>
* <th>default value</th>
* <th>description</th>
* </tr>
* <tr>
* <td>name</td>
* <td>Bulkhead-N</td>
* <td>Name used for debugging</td>
* </tr>
* <tr>
* <td>limit</td>
* <td>{@value DEFAULT_LIMIT}</td>
* <td>Max number of parallel calls</td>
* </tr>
* <tr>
* <td>queue-length</td>
* <td>{@value DEFAULT_QUEUE_LENGTH}</td>
* <td>Max number of queued calls</td>
* </tr>
* <tr>
* <td>cancel-source</td>
* <td>true</td>
* <td>Cancel task source if task is cancelled</td>
* </tr>
* </table>
*
* @param config the config node to use
* @return updated builder instance
*/
public Builder config(Config config) {
config.get("limit").asInt().ifPresent(this::limit);
config.get("queue-length").asInt().ifPresent(this::queueLength);
config.get("name").asString().ifPresent(this::name);
config.get("cancel-source").asBoolean().ifPresent(this::cancelSource);
return this;
}

int limit() {
return limit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import java.util.concurrent.ScheduledExecutorService;

import io.helidon.common.LazyValue;
import io.helidon.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;

/**
* CircuitBreaker protects a potentially failing endpoint from overloading and the application
Expand Down Expand Up @@ -89,6 +92,7 @@ enum State {
/**
* Fluent API builder for {@link io.helidon.faulttolerance.CircuitBreaker}.
*/
@Configured
class Builder implements io.helidon.common.Builder<Builder, CircuitBreaker> {
private final Set<Class<? extends Throwable>> skipOn = new HashSet<>();
private final Set<Class<? extends Throwable>> applyOn = new HashSet<>();
Expand Down Expand Up @@ -118,6 +122,7 @@ public CircuitBreaker build() {
* @param delay to wait
* @return updated builder instance
*/
@ConfiguredOption("PT5S")
public Builder delay(Duration delay) {
this.delay = delay;
return this;
Expand All @@ -132,6 +137,7 @@ public Builder delay(Duration delay) {
* @return updated builder instance
* @see #volume(int)
*/
@ConfiguredOption("60")
public Builder errorRatio(int ratio) {
this.ratio = ratio;
return this;
Expand All @@ -144,6 +150,7 @@ public Builder errorRatio(int ratio) {
* @param successThreshold number of calls
* @return updated builder instance
*/
@ConfiguredOption("1")
public Builder successThreshold(int successThreshold) {
this.successThreshold = successThreshold;
return this;
Expand All @@ -156,6 +163,7 @@ public Builder successThreshold(int successThreshold) {
* @return updated builder instance
* @see #errorRatio(int)
*/
@ConfiguredOption("10")
public Builder volume(int volume) {
this.volume = volume;
return this;
Expand Down Expand Up @@ -238,6 +246,7 @@ public Builder executor(ScheduledExecutorService scheduledExecutor) {
* @param name the name
* @return updated builder instance
*/
@ConfiguredOption("CircuitBreaker-")
public Builder name(String name) {
this.name = name;
return this;
Expand All @@ -250,11 +259,68 @@ public Builder name(String name) {
* @param cancelSource cancel source policy
* @return updated builder instance
*/
@ConfiguredOption("true")
public Builder cancelSource(boolean cancelSource) {
this.cancelSource = cancelSource;
return this;
}

/**
* <p>
* Load all properties for this circuit breaker from configuration.
* </p>
* <table class="config">
* <caption>Configuration</caption>
* <tr>
* <th>key</th>
* <th>default value</th>
* <th>description</th>
* </tr>
* <tr>
* <td>delay</td>
* <td>5 seconds</td>
* <td>Delay to transition from open to half-open</td>
* </tr>
* <tr>
* <td>name</td>
* <td>CircuitBreaker-N</td>
* <td>Name used for debugging</td>
* </tr>
* <tr>
* <td>error-ratio</td>
* <td>60</td>
* <td>Failure percentage that will open the breaker</td>
* </tr>
* <tr>
* <td>success-threshold</td>
* <td>1</td>
* <td>Number of successful calls will close a half-open breaker</td>
* </tr>
* <tr>
* <td>volume</td>
* <td>10</td>
* <td>Rolling window size</td>
* </tr>
* <tr>
* <td>cancel-source</td>
* <td>true</td>
* <td>Cancel task source if task is cancelled</td>
* </tr>
* </table>
*
* @param config the config node to use
* @return updated builder instance
*/
public Builder config(Config config) {
config.get("delay").as(Duration.class).ifPresent(this::delay);
config.get("error-ratio").asInt().ifPresent(this::errorRatio);
config.get("success-threshold").asInt().ifPresent(this::successThreshold);
config.get("volume").asInt().ifPresent(this::volume);
config.get("name").asString().ifPresent(this::name);
config.get("cancel-source").asBoolean().ifPresent(this::cancelSource);
return this;
}

LazyValue<? extends ScheduledExecutorService> executor() {
return executor;
}
Expand Down
Loading