我有一个Java类型的actor,负责可能暂时不可用的外部资源上的过滤器/重试逻辑。演员的领域,常用的方法有:无限期地等待可能永远不会到达的消息
public class MyActorImpl implements MyActor {
private static final long MINWAIT = 50;
private static final long MAXWAIT = 1000;
private static final long DEFAULTWAIT = 0;
private static final double BACKOFFMULTIPLIER = 1.5;
private long updateWait(long currentWait) {
return Math.min(Math.max((long) (currentWait * BACKOFFMULTIPLIER), MINWAIT), MAXWAIT);
}
// mutable
private long opWait = DEFAULTWAIT;
private final Queue<OpInput> opBuffer = new ArrayDeque<>();
// called from external actor
public void operation(OpInput opInput) {
operation(opInput, DEFAULTWAIT);
}
// called internally
public void operation(OpInput opInput, long currentWait);
}
演员有几个操作所有或多或少相同的重试/缓冲逻辑;每个操作都有自己的[op]Wait
和[op]Buffer
字段。
- 父演员调用
void operation(OpInput opInput)
void operation(OpInput opInput, long currentWait)
使用DEFAULTWAIT
用于第二参数前述方法调用- 如果
currentWait
参数不等于opWait
然后输入被存储在opBuffer
,否则该输入被发送到外部资源。 - 如果外部资源返回成功,则
opWait
设置为DEFAULTWAIT
,并且opBuffer
的内容通过operation(opInput)
方法发回。如果外部资源(或更可能是网络)返回错误,则我使用opWait
毫秒的延迟更新opWait = updateWait(opWait)
和调度参与者系统调度器上的operation(opInput, opWait)
。
I.e.我使用actor系统调度器来实现指数退避;我正在使用currentWait
参数来标识正在重试的消息,并正在缓冲其他消息,直到主消息被外部资源成功处理。
问题是,如果预定的operation(opInput, currentWait)
消息丢失,那么我将永远缓冲消息,因为currentWait == opWait
防护将对所有其他消息失败。我可以使用像spring-retry这样的实现指数回退,但我没有看到合并操作的重试循环的方式,这意味着我可以在每个重试循环中使用一个线程(而使用actor系统的调度器不会放得太多系统上的应变)。
我正在寻找一种更容错的方法来实现角色和外部资源之间的接口上的缓冲和指数退避,而不必为任务分配太多资源。
“调用外部”我的意思是该方法被另一个actor调用,而“内部调用”方法只能通过'self.operation(...)调用。并感谢您使用未来的建议,而不是预定的消息,这大大简化了代码 –