Skip to content

hadielmougy/shield

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

shield

Fault tolerance library for java

Usage

<dependency>
    <groupId>io.github.hadielmougy</groupId>
    <artifactId>shield</artifactId>
    <version>0.1.1</version>
</dependency>

Supported Interceptors

Throttler

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

   

Rate limit

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

Timeout

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

Retry

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

Circuit-breaker

    // count based
    final Supplier<Void> comp = Shield.decorate( component)
                .with(Interceptor.circuitBreaker()
                        .failureRateThreshold(50)
                        .slidingWindowSize(4)
                        .waitDurationInOpenState(Duration.ofSeconds(1))
                        .slidingWindowType(CircuitBreaker.WindowType.COUNT_BASED))
                .build();

    // time based
    final Supplier<Void> comp = Shield.decorate(target)
                .with(Interceptor.circuitBreaker()
                        .failureRateThreshold(50)
                        .slidingWindowSize(1)
                        .waitDurationInOpenState(Duration.ofSeconds(1))
                        .slidingWindowType(CircuitBreaker.WindowType.TIME_BASED))
                .build();

Assemble

    final Supplier<Void> decorated = Shield.decorate(() -> ...)
            .with(retry)
            .with(...)
            .build();
    
    decorated.get();