게으른 타임 아웃 해상도 회로 차단기가 ArgumentOutOfRangeException을 던지고 있습니다

StackOverflow https://stackoverflow.com/questions/2043544

문제

Ayende가 게시되었습니다 수정 Davy Brion 's에게 회로 차단기, 그는 타임 아웃 해상도를 게으른 모델로 변경했습니다.

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();
}

하지만, 생성자가 실패 할 수 있습니다 a TimeSpan a의 최대 값을 빠르게 넘칠 수 있습니다 DateTime. 예를 들어, 회로 차단기의 시간 초과가 시간대의 최대 값 인 경우.

System.ArgumentOutOfRangeException이 잡혔습니다

Message = "추가되거나 빼는 값은 표현할 수없는 DateTime을 초래합니다."

...

at System.datetime.op_addition (datetime d, timespan t)

이 문제를 피하고 예상되는 행동을 어떻게 유지합니까?

도움이 되었습니까?

해결책

약간의 수학을하십시오

확인하는 경우를 결정하십시오 타임 아웃은 나머지 시간보다 큽니다 a의 최대 값 DateTime 그리고 현재 DateTime. 최대 TimeSpan 일반적으로 나타납니다 기능적으로 무한한 시간 초과 (이것을 "일회성"회로 차단기로 만듭니다).

public OpenState(CircuitBreaker outer)
    : base(outer)
{
    DateTime now = DateTime.UtcNow;
    expiry = outer.Timeout > (DateTime.MaxValue - now) 
        ? DateTime.MaxValue
        : now + outer.Timeout;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top