Skip to content

Commit

Permalink
Simplified implementation for retry policy
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzhiguo committed May 7, 2024
1 parent bfaf9f9 commit f29160b
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.jd.live.agent.governance.interceptor;

import com.jd.live.agent.bootstrap.bytekit.context.MethodContext;
import com.jd.live.agent.core.plugin.definition.InterceptorAdaptor;
import com.jd.live.agent.governance.instance.Endpoint;
import com.jd.live.agent.governance.invoke.InboundInvocation;
Expand Down Expand Up @@ -179,7 +180,9 @@ protected O process(R request) {
*/
protected abstract Supplier<Response> createRetrySupplier(Object target, Method method, Object[] allArguments, Object result);

protected Response tryRetry(O invocation, Response response, Supplier<Response> retrySupplier) {
protected Object invokeWithRetry(O invocation, MethodContext ctx) {
Supplier<Response> retrySupplier = createRetrySupplier(ctx.getTarget(), ctx.getMethod(), ctx.getArguments(), ctx.getResult());
Response response = retrySupplier.get();
ServicePolicy servicePolicy = invocation == null ? null : invocation.getServiceMetadata().getServicePolicy();
RetryPolicy retryPolicy = servicePolicy == null ? null : servicePolicy.getRetryPolicy();
if (retryPolicy != null && retrierFactories != null) {
Expand All @@ -189,7 +192,7 @@ protected Response tryRetry(O invocation, Response response, Supplier<Response>
return retrier.execute(retrySupplier);
}
}
return null;
return response == null ? null : response.getResponse();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,7 @@ public void onEnter(ExecutableContext ctx) {
mc.setResult(result);
mc.setSkip(true);
}
final Supplier<Response> retrySupplier = createRetrySupplier(mc.getTarget(), mc.getMethod(), mc.getArguments(), mc.getResult());
Response result = null;
Throwable ex = null;
try {
result = retrySupplier.get();
} catch (Throwable throwable) {
ex = throwable;
}
Response tryResult = tryRetry(outboundInvocation, result, retrySupplier);
if (tryResult != null) {
result = tryResult;
}
mc.setResult(result == null ? null : result.getResponse());
mc.setResult(invokeWithRetry(outboundInvocation, mc));
mc.setSkip(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,7 @@ public void onEnter(ExecutableContext ctx) {
mc.setResult(result);
mc.setSkip(true);
}
final Supplier<Response> retrySupplier = createRetrySupplier(mc.getTarget(), mc.getMethod(), mc.getArguments(), mc.getResult());
Response result = null;
Throwable ex = null;
try {
result = retrySupplier.get();
} catch (Throwable throwable) {
ex = throwable;
}
Response tryResult = tryRetry(outboundInvocation, result, retrySupplier);
if (tryResult != null) {
result = tryResult;
}
mc.setResult(result == null ? null : result.getResponse());
mc.setResult(invokeWithRetry(outboundInvocation, mc));
mc.setSkip(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,7 @@ public void onEnter(ExecutableContext ctx) {
mc.setResult(result);
mc.setSkip(true);
}
final Supplier<Response> retrySupplier = createRetrySupplier(mc.getTarget(), mc.getMethod(), mc.getArguments(), mc.getResult());
Response result = null;
Throwable ex = null;
try {
result = retrySupplier.get();
} catch (Throwable throwable) {
ex = throwable;
}
Response tryResult = tryRetry(outboundInvocation, result, retrySupplier);
if (tryResult != null) {
result = tryResult;
}
mc.setResult(result == null ? null : result.getResponse());
mc.setResult(invokeWithRetry(outboundInvocation, mc));
mc.setSkip(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,7 @@ public void onEnter(ExecutableContext ctx) {
mc.setResult(response);
mc.setSkip(true);
}
final Supplier<Response> retrySupplier = createRetrySupplier(mc.getTarget(), mc.getMethod(), mc.getArguments(), mc.getResult());
Response result = null;
Throwable ex = null;
try {
result = retrySupplier.get();
} catch (Throwable throwable) {
ex = throwable;
}
Response tryResult = tryRetry(outboundInvocation, result, retrySupplier);
if (tryResult != null) {
result = tryResult;
}
mc.setResult(result == null ? null : result.getResponse());
mc.setResult(invokeWithRetry(outboundInvocation, mc));
mc.setSkip(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,7 @@ public void onEnter(ExecutableContext ctx) {
null,
StandardCharsets.UTF_8));
}
final Supplier<Response> retrySupplier = createRetrySupplier(mc.getTarget(), mc.getMethod(), mc.getArguments(), mc.getResult());
Response result = null;
Throwable ex = null;
try {
result = retrySupplier.get();
} catch (Throwable throwable) {
ex = throwable;
}
Response tryResult = tryRetry(outboundInvocation, result, retrySupplier);
if (tryResult != null) {
result = tryResult;
}
mc.setResult(result == null ? null : result.getResponse());
mc.setResult(invokeWithRetry(outboundInvocation, mc));
mc.setSkip(true);
}

Expand Down

0 comments on commit f29160b

Please sign in to comment.