Created
June 12, 2018 22:04
-
-
Save dangets/7ed97d3cf775e99fc6b86cc6fbc1f9a3 to your computer and use it in GitHub Desktop.
playing with jodah failesafe library
This file contains 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
package com.dangets; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.concurrent.TimeUnit; | |
import net.jodah.failsafe.CircuitBreaker; | |
public class FailRateLimiter { | |
public static void main(String[] args) throws InterruptedException, IOException { | |
CircuitBreaker cb = new CircuitBreaker() | |
.withDelay( 4, TimeUnit.SECONDS) | |
.withFailureThreshold(2) | |
.withSuccessThreshold(5) | |
.onOpen(() -> System.out.println("breaker open")) | |
.onHalfOpen(() -> System.out.println("breaker half-open")) | |
.onClose(() -> System.out.println("breaker closed")); | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
while (true) { | |
while (!cb.allowsExecution()) { | |
System.out.println("breaker currently open - waiting ..."); | |
Thread.sleep(2_000); | |
} | |
String line = br.readLine().trim(); | |
switch (line) { | |
case "p": | |
System.out.println("pass"); | |
cb.recordSuccess(); | |
break; | |
case "f": | |
System.out.println("fail"); | |
cb.recordFailure(new Exception()); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment