- Notifications
You must be signed in to change notification settings - Fork122
Added a circuit breaker handler using failsafe#64
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
2 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions...common/src/main/java/com/stubbornjava/common/undertow/handlers/CircuitBreakerHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.stubbornjava.common.undertow.handlers; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import io.undertow.server.HttpHandler; | ||
| import io.undertow.server.HttpServerExchange; | ||
| import net.jodah.failsafe.CircuitBreaker; | ||
| import net.jodah.failsafe.Failsafe; | ||
| // {{start:handler}} | ||
| public class CircuitBreakerHandler implements HttpHandler { | ||
| private static final Logger log = LoggerFactory.getLogger(CircuitBreakerHandler.class); | ||
| private final CircuitBreaker circuitBreaker; | ||
| private final HttpHandler delegate; | ||
| private final HttpHandler failureHandler; | ||
| public CircuitBreakerHandler(CircuitBreaker circuitBreaker, HttpHandler delegate, HttpHandler failureHandler) { | ||
| super(); | ||
| this.circuitBreaker = circuitBreaker; | ||
| this.delegate = delegate; | ||
| this.failureHandler = failureHandler; | ||
| } | ||
| @Override | ||
| public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
| Failsafe.with(circuitBreaker) | ||
| .withFallback(() -> failureHandler.handleRequest(exchange)) | ||
| // We need to call get here instead of execute so we can return the | ||
| // mutated exchange to run checks on it | ||
| .get(() -> { | ||
| delegate.handleRequest(exchange); | ||
| return exchange; | ||
| }); | ||
| } | ||
| } | ||
| // {{end:handler}} | ||
128 changes: 128 additions & 0 deletions...bornjava-examples/src/main/java/com/stubbornjava/examples/failsafe/FailsafeWebserver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package com.stubbornjava.examples.failsafe; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import com.stubbornjava.common.HttpClient; | ||
| import com.stubbornjava.common.undertow.Exchange; | ||
| import com.stubbornjava.common.undertow.SimpleServer; | ||
| import com.stubbornjava.common.undertow.handlers.CircuitBreakerHandler; | ||
| import com.stubbornjava.common.undertow.handlers.CustomHandlers; | ||
| import io.undertow.server.HttpHandler; | ||
| import io.undertow.server.HttpServerExchange; | ||
| import io.undertow.util.StatusCodes; | ||
| import net.jodah.failsafe.CircuitBreaker; | ||
| import okhttp3.HttpUrl; | ||
| import okhttp3.Request; | ||
| public class FailsafeWebserver { | ||
| private static final Logger log = LoggerFactory.getLogger(FailsafeWebserver.class); | ||
| // {{start:handlers}} | ||
| private static final void serverError(HttpServerExchange exchange) { | ||
| exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); | ||
| Exchange.body().sendText(exchange, "500 - Internal Server Error"); | ||
| } | ||
| // This handler helps simulate errors, bad requests, and successful requests. | ||
| private static final void circuitClosed(HttpServerExchange exchange) { | ||
| boolean error = Exchange.queryParams().queryParamAsBoolean(exchange, "error").orElse(false); | ||
| boolean exception = Exchange.queryParams().queryParamAsBoolean(exchange, "exception").orElse(false); | ||
| if (error) { | ||
| exchange.setStatusCode(StatusCodes.BAD_REQUEST); | ||
| Exchange.body().sendText(exchange, "Bad Request"); | ||
| } else if (exception) { | ||
| throw new RuntimeException("boom"); | ||
| } else { | ||
| Exchange.body().sendText(exchange, "Circuit is open everything is functioning properly."); | ||
| } | ||
| } | ||
| private static final HttpHandler CIRCUIT_BREAKER_HANDLER; | ||
| static { | ||
| CircuitBreaker breaker = new CircuitBreaker() | ||
| // Trigger circuit breaker failure on exceptions or bad requests | ||
| .failIf((HttpServerExchange exchange, Throwable ex) -> { | ||
| return (exchange != null && StatusCodes.BAD_REQUEST == exchange.getStatusCode()) | ||
| || ex != null; | ||
| }) | ||
| // If 7 out of 10 requests fail Open the circuit | ||
| .withFailureThreshold(7, 10) | ||
| // When half open if 3 out of 5 requests succeed close the circuit | ||
| .withSuccessThreshold(3, 5) | ||
| // Delay this long before half opening the circuit | ||
| .withDelay(2, TimeUnit.SECONDS) | ||
| .onClose(() -> log.info("Circuit Closed")) | ||
| .onOpen(() -> log.info("Circuit Opened")) | ||
| .onHalfOpen(() -> log.info("Circuit Half-Open")); | ||
| CIRCUIT_BREAKER_HANDLER = new CircuitBreakerHandler(breaker, | ||
| FailsafeWebserver::circuitClosed, | ||
| FailsafeWebserver::serverError); | ||
| } | ||
| // {{end:handlers}} | ||
| // {{start:request}} | ||
| private static void request(boolean error, boolean exception) { | ||
| HttpUrl url = HttpUrl.parse("http://localhost:8080") | ||
| .newBuilder() | ||
| .addQueryParameter("error", String.valueOf(error)) | ||
| .addQueryParameter("exception", String.valueOf(exception)) | ||
| .build(); | ||
| Request request = new Request.Builder().get().url(url).build(); | ||
| try { | ||
| log.info(HttpClient.globalClient().newCall(request).execute().body().string()); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| // {{end:request}} | ||
| // {{start:main}} | ||
| public static void main(String[] args) { | ||
| HttpHandler exceptionHandler = | ||
| CustomHandlers.exception(CIRCUIT_BREAKER_HANDLER) | ||
| .addExceptionHandler(Throwable.class, FailsafeWebserver::serverError); | ||
| SimpleServer server = SimpleServer.simpleServer(exceptionHandler); | ||
| server.start(); | ||
| // Warm-up the circuit breaker it needs to hit at least max executions | ||
| // Before it will reject anything. This will make that easier. | ||
| for (int i = 0; i < 10; i++) { | ||
| request(false, false); | ||
| } | ||
| ScheduledExecutorService schedExec = Executors.newScheduledThreadPool(1); | ||
| // A simple request that should always succeed | ||
| schedExec.scheduleAtFixedRate(() -> request(false, false), 0, 500, TimeUnit.MILLISECONDS); | ||
| // Send a batch of 15 bad requests to trigger the circuit breaker | ||
| Runnable errors = () -> { | ||
| log.info("Executing bad requests!"); | ||
| for (int i = 0; i < 15; i++) { | ||
| request(true, false); | ||
| } | ||
| }; | ||
| schedExec.schedule(errors, 1, TimeUnit.SECONDS); | ||
| // Send a batch of 15 requests that throw exceptions | ||
| Runnable exceptions = () -> { | ||
| log.info("Executing requests that throw exceptions!"); | ||
| for (int i = 0; i < 15; i++) { | ||
| request(false, true); | ||
| } | ||
| }; | ||
| schedExec.schedule(exceptions, 5, TimeUnit.SECONDS); | ||
| } | ||
| // {{end:main}} | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.