-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retrier definition and implementation based on Resilience4j
- Loading branch information
1 parent
46b5cb4
commit 14f42e2
Showing
8 changed files
with
198 additions
and
23 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...ylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/Retrier.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.jd.live.agent.governance.invoke.retry; | ||
|
||
import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Retrier | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public interface Retrier { | ||
|
||
/** | ||
* Execute retry logic | ||
* | ||
* @param supplier Retry logic | ||
* @param <T> Response type | ||
* @return Response | ||
*/ | ||
<T> T execute(Supplier<T> supplier); | ||
|
||
/** | ||
* Get failover policy | ||
* | ||
* @return policy | ||
*/ | ||
FailoverPolicy getPolicy(); | ||
} |
50 changes: 50 additions & 0 deletions
50
...overnance-api/src/main/java/com/jd/live/agent/governance/invoke/retry/RetrierFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.jd.live.agent.governance.invoke.retry; | ||
|
||
import com.jd.live.agent.core.extension.annotation.Extensible; | ||
import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
/** | ||
* RetrierFactory | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
@Extensible("RetrierFactory") | ||
public interface RetrierFactory { | ||
|
||
Map<Long, AtomicReference<Retrier>> RETRIERS = new ConcurrentHashMap<>(); | ||
|
||
default Retrier get(FailoverPolicy policy) { | ||
if (policy == null) { | ||
return null; | ||
} | ||
AtomicReference<Retrier> reference = RETRIERS.computeIfAbsent(policy.getId(), n -> new AtomicReference<>()); | ||
Retrier retrier = reference.get(); | ||
if (retrier != null && retrier.getPolicy().getVersion() >= policy.getVersion()) { | ||
return retrier; | ||
} | ||
Retrier newLimiter = create(policy); | ||
while (true) { | ||
retrier = reference.get(); | ||
if (retrier == null || retrier.getPolicy().getVersion() < policy.getVersion()) { | ||
if (reference.compareAndSet(retrier, newLimiter)) { | ||
retrier = newLimiter; | ||
break; | ||
} | ||
} | ||
} | ||
return retrier; | ||
} | ||
|
||
/** | ||
* Create Retrier | ||
* | ||
* @param failoverPolicy Failure retry policy | ||
* @return Retrier | ||
*/ | ||
Retrier create(FailoverPolicy failoverPolicy); | ||
|
||
} |
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
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
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
51 changes: 51 additions & 0 deletions
51
.../java/com/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrier.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.jd.live.agent.implement.flowcontrol.resilience4j.retry; | ||
|
||
import com.jd.live.agent.governance.invoke.retry.Retrier; | ||
import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; | ||
import io.github.resilience4j.retry.Retry; | ||
import io.github.resilience4j.retry.RetryConfig; | ||
import io.github.resilience4j.retry.RetryRegistry; | ||
|
||
import java.time.Duration; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Resilience4jRetrier | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public class Resilience4jRetrier implements Retrier { | ||
|
||
private final FailoverPolicy policy; | ||
|
||
private final Retry retry; | ||
|
||
public Resilience4jRetrier(FailoverPolicy policy) { | ||
this.policy = policy; | ||
RetryConfig config = RetryConfig.custom() | ||
.maxAttempts(policy.getRetry()) | ||
.waitDuration(Duration.ofMillis(policy.getTimeoutInMilliseconds())) | ||
// TODO | ||
// .retryOnResult(response -> response.getStatus() == 500) | ||
.failAfterMaxAttempts(true) | ||
.build(); | ||
RetryRegistry registry = RetryRegistry.of(config); | ||
retry = registry.retry(policy.getId().toString()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public <T> T execute(Supplier<T> supplier) { | ||
return retry.executeSupplier(supplier); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public FailoverPolicy getPolicy() { | ||
return policy; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...om/jd/live/agent/implement/flowcontrol/resilience4j/retry/Resilience4jRetrierFactory.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jd.live.agent.implement.flowcontrol.resilience4j.retry; | ||
|
||
import com.jd.live.agent.governance.invoke.retry.Retrier; | ||
import com.jd.live.agent.governance.invoke.retry.RetrierFactory; | ||
import com.jd.live.agent.governance.policy.service.failover.FailoverPolicy; | ||
|
||
/** | ||
* Resilience4jRetrierFactory | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public class Resilience4jRetrierFactory implements RetrierFactory { | ||
|
||
@Override | ||
public Retrier create(FailoverPolicy failoverPolicy) { | ||
return new Resilience4jRetrier(failoverPolicy); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...main/resources/META-INF/services/com.jd.live.agent.governance.invoke.retry.RetrierFactory
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.jd.live.agent.implement.flowcontrol.resilience4j.retry.Resilience4jRetrierFactory |