项目作者: hadielmougy

项目描述 :
Fault tolerance library for java
高级语言: Java
项目地址: git://github.com/hadielmougy/shield.git
创建时间: 2020-02-08T22:42:02Z
项目社区:https://github.com/hadielmougy/shield

开源协议:Apache License 2.0

下载


shield

Fault tolerance library for java

Usage

  1. <dependency>
  2. <groupId>io.github.hadielmougy</groupId>
  3. <artifactId>shield</artifactId>
  4. <version>0.1.2</version>
  5. </dependency>

Supported Interceptors

Throttler

  1. final Supplier<Void> throttler = Shield.decorate(target)
  2. .with(Interceptor.throttler()
  3. .requests(1)
  4. .maxWaitMillis(500))
  5. .build();

Rate limit

  1. final Supplier<Void> limiter = Shield.decorate(target)
  2. .with(Interceptor.rateLimiter()
  3. .rate(1))
  4. .build();

Timeout

  1. final Supplier<Void> decorated = Shield.decorate(target)
  2. .with(Interceptor.timeout().waitMillis(1100))
  3. .with(Interceptor.retry().delayMillis(1000).maxRetries(5))
  4. .build();

Retry

  1. final Retry retry = Interceptor.retry()
  2. .delayMillis(500)
  3. .maxRetries(3)
  4. .onException(IllegalArgumentException.class);

Circuit-breaker

  1. // count based
  2. final Supplier<Void> comp = Shield.decorate( component)
  3. .with(Interceptor.circuitBreaker()
  4. .failureRateThreshold(50)
  5. .slidingWindowSize(4)
  6. .waitDurationInOpenState(Duration.ofSeconds(1))
  7. .slidingWindowType(CircuitBreaker.WindowType.COUNT_BASED))
  8. .build();
  9. // time based
  10. final Supplier<Void> comp = Shield.decorate(target)
  11. .with(Interceptor.circuitBreaker()
  12. .failureRateThreshold(50)
  13. .slidingWindowSize(1)
  14. .waitDurationInOpenState(Duration.ofSeconds(1))
  15. .slidingWindowType(CircuitBreaker.WindowType.TIME_BASED))
  16. .build();

Assemble

  1. final Supplier<Void> decorated = Shield.decorate(() -> ...)
  2. .with(retry)
  3. .with(...)
  4. .build();
  5. decorated.get();