<commit_before>= resilience4j-feign

Resilience4j decorators for https://github.com/OpenFeign/feign[feign].
Similar to https://github.com/OpenFeign/feign/tree/master/hystrix[HystrixFeign], 
resilience4j-feign makes it easy to incorporate "fault tolerance" patterns into the feign framework, such as 
 the CircuitBreaker and RateLimiter. 


== Current Features
* CircuitBreaker
* RateLimiter
* Fallback
 
 
== Decorating Feign Interfaces

The `Resilience4jFeign.builder` is the main class for creating fault tolerance instances of feign. 
It extends the `Feign.builder` and can be configured in the same way with the exception of adding a custom 
`InvocationHandlerFactory`. Resilience4jFeign uses its own `InvocationHandlerFactory` to apply the decorators.
Decorators can be built using the `FeignDecorators` class. Multiple decorators can be combined  

The following example shows how to decorate a feign interface with a RateLimiter and CircuitBreaker:
``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String getGreeting();
            
            @RequestLine("POST /greeting")
            String createGreeting();
        }

        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");
        RateLimiter rateLimiter = RateLimiter.ofDefaults("backendName");
        FeignDecorators decorators = FeignDecorators.builder()
                                         .withRateLimiter(rateLimiter)
                                         .withCircuitBreaker(circuitBreaker)
                                         .build();
        MyService myService = Resilience4jFeign.builder(decorators).target(MyService.class, "http://localhost:8080/");
```

Calling any method of the `MyService` instance will invoke a CircuitBreaker and then a RateLimiter.
If one of these mechanisms take affect, then the corresponding RuntimeException will be thrown, for example, `CircuitBreakerOpenException` or `RequestNotPermitted` (Hint: These do not extend the `FeignException` class).

The following diagram illustrates how the decorators are stacked:
image::feign-decorators.png[]


== Ordering of Decorators
The order in which decorators are applied correspond to the order in which they are declared. 
When building `FeignDecorators`, it is important to be wary of this, since the order affects the resulting behavior.

For example,
``` java
        FeignDecorators decoratorsA = FeignDecorators.builder()
                                         .withCircuitBreaker(circuitBreaker)
                                         .withRateLimiter(rateLimiter)
                                         .build();
                                         
        FeignDecorators decoratorsB = FeignDecorators.builder()
                                         .withRateLimiter(rateLimiter)
                                         .withCircuitBreaker(circuitBreaker)
                                         .build();
```        

With `decoratorsA` the RateLimiter will be called before the CircuitBreaker. That means that even if the CircuitBreaker is open, the RateLimiter will still limit the rate of calls.
`decoratorsB` applies the reserve order. Meaning that once the CircuitBreaker is open, the RateLimiter will no longer be in affect.


== Fallback
Fallbacks can be defined that are called when Exceptions are thrown. Exceptions can occur when the HTTP request fails, but also when one of the `FeignDecorators` activates, for example, the CircuitBreaker.

``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String greeting();
        }

        MyService requestFailedFallback = () -> "fallback greeting";
        MyService circuitBreakerFallback = () -> "CircuitBreaker is open!";
        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");
        FeignDecorators decorators = FeignDecorators.builder()
                                         .withFallback(requestFailedFallback, FeignException.class)
                                         .withFallback(circuitBreakerFallback, CircuitBreakerOpenException.class)
                                         .build();
        MyService myService = Resilience4jFeign.builder(decorators).target(MyService.class, "http://localhost:8080/", fallback);
```
In this example, the `requestFailedFallback` is called when a `FeignException` is thrown (usually when the HTTP request fails), whereas
 the `circuitBreakerFallback` is only called in the case of a `CircuitBreakerOpenException`. 
 Check the `FeignDecorators` class for more ways to filter fallbacks.

All fallbacks must implement the same interface that is declared in the "target" (Resilience4jFeign.Builder#target) method, otherwise an IllegalArgumentException will be thrown.
Multiple fallbacks can be assigned to handle the same Exception with the next fallback being called when the previous one fails.

A fallback can consume a thrown Exception if needed. This can be useful if the fallback may have different behaviours depending on the Exception, or to simply log the Exception.
Be aware that such a fallback will be instantiated for every exceptions thrown.

``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String greeting();
        }

        public class MyFallback implements MyService {
            private Exception cause;

            public MyFallback(Exception cause) {
                this.cause = cause;
            }

            public String greeting() {
                if (cause instanceOf FeignException) {
                    return "Feign Exception";
                } else {
                    return "Other exception";
                }
            }
        }

        FeignDecorators decorators = FeignDecorators.builder()<commit_msg>ReplaceD CircuitBreakerOpenException with CallNotPermittedException (#1119)<commit_after>= resilience4j-feign

Resilience4j decorators for https://github.com/OpenFeign/feign[feign].
Similar to https://github.com/OpenFeign/feign/tree/master/hystrix[HystrixFeign], 
resilience4j-feign makes it easy to incorporate "fault tolerance" patterns into the feign framework, such as 
 the CircuitBreaker and RateLimiter. 


== Current Features
* CircuitBreaker
* RateLimiter
* Fallback
 
 
== Decorating Feign Interfaces

The `Resilience4jFeign.builder` is the main class for creating fault tolerance instances of feign. 
It extends the `Feign.builder` and can be configured in the same way with the exception of adding a custom 
`InvocationHandlerFactory`. Resilience4jFeign uses its own `InvocationHandlerFactory` to apply the decorators.
Decorators can be built using the `FeignDecorators` class. Multiple decorators can be combined  

The following example shows how to decorate a feign interface with a RateLimiter and CircuitBreaker:
``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String getGreeting();
            
            @RequestLine("POST /greeting")
            String createGreeting();
        }

        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");
        RateLimiter rateLimiter = RateLimiter.ofDefaults("backendName");
        FeignDecorators decorators = FeignDecorators.builder()
                                         .withRateLimiter(rateLimiter)
                                         .withCircuitBreaker(circuitBreaker)
                                         .build();
        MyService myService = Resilience4jFeign.builder(decorators).target(MyService.class, "http://localhost:8080/");
```

Calling any method of the `MyService` instance will invoke a CircuitBreaker and then a RateLimiter.
If one of these mechanisms take affect, then the corresponding RuntimeException will be thrown, for example, `CallNotPermittedException` or `RequestNotPermitted` (Hint: These do not extend the `FeignException` class).

The following diagram illustrates how the decorators are stacked:
image::feign-decorators.png[]


== Ordering of Decorators
The order in which decorators are applied correspond to the order in which they are declared. 
When building `FeignDecorators`, it is important to be wary of this, since the order affects the resulting behavior.

For example,
``` java
        FeignDecorators decoratorsA = FeignDecorators.builder()
                                         .withCircuitBreaker(circuitBreaker)
                                         .withRateLimiter(rateLimiter)
                                         .build();
                                         
        FeignDecorators decoratorsB = FeignDecorators.builder()
                                         .withRateLimiter(rateLimiter)
                                         .withCircuitBreaker(circuitBreaker)
                                         .build();
```        

With `decoratorsA` the RateLimiter will be called before the CircuitBreaker. That means that even if the CircuitBreaker is open, the RateLimiter will still limit the rate of calls.
`decoratorsB` applies the reserve order. Meaning that once the CircuitBreaker is open, the RateLimiter will no longer be in affect.


== Fallback
Fallbacks can be defined that are called when Exceptions are thrown. Exceptions can occur when the HTTP request fails, but also when one of the `FeignDecorators` activates, for example, the CircuitBreaker.

``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String greeting();
        }

        MyService requestFailedFallback = () -> "fallback greeting";
        MyService circuitBreakerFallback = () -> "CircuitBreaker is open!";
        CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("backendName");
        FeignDecorators decorators = FeignDecorators.builder()
                                         .withFallback(requestFailedFallback, FeignException.class)
                                         .withFallback(circuitBreakerFallback, CallNotPermittedException.class)
                                         .build();
        MyService myService = Resilience4jFeign.builder(decorators).target(MyService.class, "http://localhost:8080/", fallback);
```
In this example, the `requestFailedFallback` is called when a `FeignException` is thrown (usually when the HTTP request fails), whereas
 the `circuitBreakerFallback` is only called in the case of a `CallNotPermittedException`.
 Check the `FeignDecorators` class for more ways to filter fallbacks.

All fallbacks must implement the same interface that is declared in the "target" (Resilience4jFeign.Builder#target) method, otherwise an IllegalArgumentException will be thrown.
Multiple fallbacks can be assigned to handle the same Exception with the next fallback being called when the previous one fails.

A fallback can consume a thrown Exception if needed. This can be useful if the fallback may have different behaviours depending on the Exception, or to simply log the Exception.
Be aware that such a fallback will be instantiated for every exceptions thrown.

``` java
        public interface MyService {
            @RequestLine("GET /greeting")
            String greeting();
        }

        public class MyFallback implements MyService {
            private Exception cause;

            public MyFallback(Exception cause) {
                this.cause = cause;
            }

            public String greeting() {
                if (cause instanceOf FeignException) {
                    return "Feign Exception";
                } else {
                    return "Other exception";
                }
            }
        }

        FeignDecorators decorators = FeignDecorators.builder()
{'fraction_non_alphanumeric': 0.061623459413514665, 'fraction_numerical': 0.003059923501912452, 'mean_word_length': 2.543975903614458, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 50, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4379947', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2721, 'n_words': 1031}