惰性超时解决方案断路器抛出 ArgumentOutOfRangeException
Ayende 发布了修改 到 Davy Brion 的 断路器,其中他更改了超时解决惰性模型的问题。
private readonly DateTime expiry;
public OpenState(CircuitBreaker outer)
: base(outer)
{
expiry = DateTime.UtcNow + outer.timeout;
}
public override void ProtectedCodeIsAboutToBeCalled()
{
if(DateTime.UtcNow < expiry)
throw new OpenCircuitException();
outer.MoveToHalfOpenState();
}
但是,构造函数可能会失败,因为 TimeSpan
可以快速溢出 日期时间
。例如,当断路器的超时时间是TimeSpan的最大值时。
System.ArgumentOutOfRangeException 被捕获
Message="添加或减去的值会导致无法表示的日期时间。"
...
在 System.DateTime.op_Addition(DateTime d, TimeSpan t)
我们如何避免这个问题并保持预期的行为?
Ayende posted a modification to Davy Brion's circuit breaker, wherein he changed the timeout resolution to a lazy model.
private readonly DateTime expiry;
public OpenState(CircuitBreaker outer)
: base(outer)
{
expiry = DateTime.UtcNow + outer.timeout;
}
public override void ProtectedCodeIsAboutToBeCalled()
{
if(DateTime.UtcNow < expiry)
throw new OpenCircuitException();
outer.MoveToHalfOpenState();
}
However, the constructor can fail because a TimeSpan
can quickly overflow the maximum value of a DateTime
. For example, when the circuit breaker’s timeout is the maximum value of a TimeSpan.
System.ArgumentOutOfRangeException was caught
Message="The added or subtracted value results in an un-representable DateTime."
...
at System.DateTime.op_Addition(DateTime d, TimeSpan t)
How do we avoid this problem and maintain the expected behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做一点数学计算
确定超时是否大于
DateTime
的最大值和当前DateTime
的余数。最大TimeSpan
通常表示功能上的无限超时(使其成为“一次性”断路器)。Do a little bit of math
Determine if the timeout is greater than the remainder of the maximum value of a
DateTime
and the currentDateTime
. A maximumTimeSpan
usually indicates a functionally infinite timeout (making this a "one-time" circuit breaker).