Class TrafficResilienceHttpClientFilter.Builder
- Enclosing class:
- TrafficResilienceHttpClientFilter
TrafficResilienceHttpServiceFilter
instance builder.-
Constructor Summary
ConstructorsConstructorDescriptionBuilder
(Supplier<CapacityLimiter> capacityLimiterSupplier) ATrafficResilienceHttpClientFilter
with no partitioning schemes.Builder
(Supplier<Function<HttpRequestMetaData, CapacityLimiter>> capacityPartitionsSupplier, boolean rejectNotMatched) ATrafficResilienceHttpClientFilter
can support request partitioning schemes. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Invoke to build an instance ofTrafficResilienceHttpClientFilter
filter to be used inside the HttpClientBuilder.capacityPartitions
(Supplier<Function<HttpRequestMetaData, CapacityLimiter>> capacityPartitionsSupplier, boolean rejectNotMatched) DefineCapacityLimiter
partitions.circuitBreakerPartitions
(Supplier<Function<HttpRequestMetaData, CircuitBreaker>> circuitBreakerPartitionsSupplier) DefineCircuitBreaker
to manage local or remote errors.classifier
(Supplier<Function<HttpRequestMetaData, Classification>> classifier) Classification in the context of capacity management allows for hints to the relevantCapacityLimiter
to be influenced on the decision-making process by the class of therequest
.When a peer rejects arequest
due to an open-circuit (see.dryRun
(boolean dryRun) Use the resilience filter in dry-run mode.forceOpenCircuitOnPeerCircuitRejections
(Supplier<Function<HttpResponseMetaData, Duration>> delayProvider, Executor executor) When a peer rejects arequest
due to an open-circuit (see.observer
(TrafficResiliencyObserver observer) Provide an observer to track interactions of the filter and requests.onCancelTicketTerminal
(Consumer<CapacityLimiter.Ticket> onCancellation) Ticket
terminal callback override upon cancellation of the request operation.Ticket
terminal callback override upon erroneous completion of the request operation.peerUnavailableRejectionPredicate
(Predicate<HttpResponseMetaData> rejectionPredicate) Peers can reject requests due to service unavailability.Peers can reject and exception due to capacity reasons based on their own principals and implementation details.
-
Constructor Details
-
Builder
ATrafficResilienceHttpClientFilter
with no partitioning schemes.All requests will go through the
CapacityLimiter
.- Parameters:
capacityLimiterSupplier
- TheSupplier
to create a newCapacityLimiter
for each new filter created by thisfactory
.
-
Builder
public Builder(Supplier<Function<HttpRequestMetaData, CapacityLimiter>> capacityPartitionsSupplier, boolean rejectNotMatched) ATrafficResilienceHttpClientFilter
can support request partitioning schemes.A partition in the context of capacity management, is a set of requests that represent an application characteristic relevant to capacity, which can be isolated and have their own set of rules (ie.
CapacityLimiter
).An example of a partition can be to represent each customer in a multi-tenant service. If an application wants to introduce customer API quotas, they can do so by identifying that customer through the
HttpRequestMetaData
and providing a differentCapacityLimiter
for that customer.If a
partitions
doesn't return aCapacityLimiter
for the givenHttpRequestMetaData
then therejectNotMatched
is evaluated to decide what the filter should do with this request. Iftrue
then the request will berejected
.It's important that instances returned from this
mapper
are singletons and shared across the same matched partitions. Otherwise, capacity will not be controlled as expected, and there is also the risk forOutOfMemoryError
.- Parameters:
capacityPartitionsSupplier
- ASupplier
to create a newFunction
for each new filter created by thisfactory
. Function provides aCapacityLimiter
instance for the givenHttpRequestMetaData
.rejectNotMatched
- Flag that decides what the filter should do whenpartitions
doesn't return aCapacityLimiter
.
-
-
Method Details
-
capacityPartitions
public TrafficResilienceHttpClientFilter.Builder capacityPartitions(Supplier<Function<HttpRequestMetaData, CapacityLimiter>> capacityPartitionsSupplier, boolean rejectNotMatched) DefineCapacityLimiter
partitions.A partition in the context of capacity management, is a set of requests that represent an application characteristic relevant to capacity, which can be isolated and have their own set of rules (ie.
CapacityLimiter
).An example of a partition can be to represent each customer in a multi-tenant service. If an application wants to introduce customer API quotas, they can do so by identifying that customer through the
HttpRequestMetaData
and providing a differentCapacityLimiter
for that customer.If a
partitions
doesn't return aCapacityLimiter
for the givenHttpRequestMetaData
then therejectNotMatched
is evaluated to decide what the filter should do with this request. Iftrue
then the request will berejected
.It's important that instances returned from this
mapper
are singletons and shared across the same matched partitions. Otherwise, capacity will not be controlled as expected, and there is also the risk forOutOfMemoryError
.- Parameters:
capacityPartitionsSupplier
- ASupplier
to create a newFunction
for each new filter created by thisfactory
. Function provides aCapacityLimiter
instance for the givenHttpRequestMetaData
.rejectNotMatched
- Flag that decides what the filter should do whenpartitions
doesn't return aCapacityLimiter
.- Returns:
this
-
classifier
public TrafficResilienceHttpClientFilter.Builder classifier(Supplier<Function<HttpRequestMetaData, Classification>> classifier) Classification in the context of capacity management allows for hints to the relevantCapacityLimiter
to be influenced on the decision-making process by the class of therequest
.An example of classification, could be health checks that need to be given preference and still allowed a permit even under stress conditions. Another case, could be a separation of reads and writes, giving preference to the reads will result in a more available system under stress, by rejecting earlier writes.
The behavior of the classification and their thresholds could be different among different
CapacityLimiter
implementations, therefore the use of this API requires good understanding of how the algorithm in use will react for the different classifications.Classification works within the context of a single
Builder(Supplier, boolean)
partition} and not universally in the filter.It's worth noting that classification is strictly a hint and could be ignored by the
CapacityLimiter
.- Parameters:
classifier
- ASupplier
of aFunction
that maps an incomingHttpRequestMetaData
to aClassification
.- Returns:
this
.
-
circuitBreakerPartitions
public TrafficResilienceHttpClientFilter.Builder circuitBreakerPartitions(Supplier<Function<HttpRequestMetaData, CircuitBreaker>> circuitBreakerPartitionsSupplier) DefineCircuitBreaker
to manage local or remote errors.The breakers can either be universal or follow any partitioning scheme (i.e., API / service-path, customer etc) but is recommended to follow similar schematics between service and client if possible for best experience.
The matching
CircuitBreaker
for arequest
can be forced opened due to a remote open circuit-breaker (i.e.,HttpResponseStatus.SERVICE_UNAVAILABLE
) dissallowing further outgoing requests for a fixed periods;forceOpenCircuitOnPeerCircuitRejections(Supplier, Executor)
.- Parameters:
circuitBreakerPartitionsSupplier
- ASupplier
to create a newFunction
for each new filter created by thisfactory
. Function provides aCircuitBreaker
instance for the givenHttpRequestMetaData
.- Returns:
this
.
-
rejectionPolicy
Peers can reject and exception due to capacity reasons based on their own principals and implementation details. ATrafficResilienceHttpClientFilter
can benefit from this input as feedback for theCapacityLimiter
in use, that the request was dropped (i.e., rejected), thus it can also bring its local limit down to help with the overloaded peer. Since what defines a rejection/drop or request for backpressure is not universally common, one can define what response characteristics define that state.It's important to know that if the passed
rejectionPredicate
teststrue
for a givenHttpResponseMetaData
then the operation isSingle.failed(Throwable)
.Out of the box if nothing custom is defined, the filter recognises as rejections requests with the following status codes:
Allowing retry, requests will fail with a
DelayedRetryRequestDroppedException
to support retrying mechanisms (like retry-filters or retry operators) to re-attempt the same request. Requests that fail due to capacity limitation, are good candidates for a retry, since we anticipate they are safe to be executed again (no previous invocation actually started) and because this maximizes the success chances.- Parameters:
policy
- TheClientPeerRejectionPolicy
that represents the peer capacity rejection behavior.- Returns:
this
.- See Also:
-
forceOpenCircuitOnPeerCircuitRejections
public TrafficResilienceHttpClientFilter.Builder forceOpenCircuitOnPeerCircuitRejections(Supplier<Function<HttpResponseMetaData, Duration>> delayProvider, Executor executor) When a peer rejects arequest
due to an open-circuit (see.peerUnavailableRejectionPredicate(Predicate)
), the feedback can be used to also forcefully open the localrequest's
CircuitBreaker
. The localCircuitBreaker
will close again once a delay period passes as defined/extracted through thedelayProvider
.If the delay provided is not a positive value, then the
CircuitBreaker
will not be modified.To disable this behaviour see
dontForceOpenCircuitOnPeerCircuitRejections()
.- Parameters:
delayProvider
- A function to provide / extract a delay in milliseconds for theCircuitBreaker
to remain open.executor
- AExecutor
used to re-close theCircuitBreaker
once the delay expires.- Returns:
this
.
-
dontForceOpenCircuitOnPeerCircuitRejections
When a peer rejects arequest
due to an open-circuit (see.peerUnavailableRejectionPredicate(Predicate)
), ignore feedback and leave local matchingcircuit-breake partition
closed.To opt-in for this behaviour see
forceOpenCircuitOnPeerCircuitRejections(Supplier, Executor)
.- Returns:
this
.
-
onErrorTicketTerminal
public TrafficResilienceHttpClientFilter.Builder onErrorTicketTerminal(BiConsumer<CapacityLimiter.Ticket, Throwable> onError) Ticket
terminal callback override upon erroneous completion of the request operation. Erroneous completion in this context means, that an error occurred as part of the operation or therejectionPolicy(ClientPeerRejectionPolicy)
triggered an exception. By default the terminal callback isCapacityLimiter.Ticket.failed(Throwable)
.- Parameters:
onError
- Callback to override defaultticket
terminal event for an erroneous operation.- Returns:
this
.
-
onCancelTicketTerminal
public TrafficResilienceHttpClientFilter.Builder onCancelTicketTerminal(Consumer<CapacityLimiter.Ticket> onCancellation) Ticket
terminal callback override upon cancellation of the request operation. By default the terminal callback isCapacityLimiter.Ticket.dropped()
.You may need to adjust this callback depending on the ordering this filter was applied. For example if the filter is applied after the
timeout-filter
then you may want to alsodrop the ticket
to let the algorithm apply throttling accounting for this timeout.- Parameters:
onCancellation
- Callback to override defaultticket
terminal event when an operation is cancelled.- Returns:
this
.
-
observer
Provide an observer to track interactions of the filter and requests.- Parameters:
observer
- an observer to track interactions of the filter and requests.- Returns:
this
.
-
dryRun
Use the resilience filter in dry-run mode. In dry-run mode the capacity limiter will track requests and log their results but request which would have been rejected will instead pass through to the underlying client.- Parameters:
dryRun
- whether to use the resilience filter in dry-run mode.- Returns:
this
-
build
Invoke to build an instance ofTrafficResilienceHttpClientFilter
filter to be used inside the HttpClientBuilder.- Returns:
- An instance of
TrafficResilienceHttpClientFilter
with the characteristics of this builder input.
-