private void CheckSetting(CircuitBreakerSetting setting) { if (setting == null) { throw new ArgumentNullException(nameof(setting)); } if (setting.ProtectAction == null) { throw new NullReferenceException("ProtectAction不能为null"); } if (setting.AllowFailInterval <= TimeSpan.Zero) { throw new ArgumentException("允许错误次数的时间间隔不能小于0"); } if (setting.AllowFailTimes <= 0) { throw new ArgumentException("在间隔时间内允许错误的次数不能小于0"); } if (setting.HalfOpenDuration <= TimeSpan.Zero) { throw new ArgumentException("打开状态下切换到半开状态的时间间隔不能小于0"); } if (setting.HalfOpenRequestLimit <= 0) { throw new ArgumentException("半开状态下允许的请求次数不能小于0"); } }
public CircuitBreaker(CircuitBreakerSetting setting) { CheckSetting(setting); _setting = setting; _closeState = new CloseState(this, new Tuple <TimeSpan, int>(_setting.AllowFailInterval, _setting.AllowFailTimes)); _openState = new OpenState(this, _setting.HalfOpenDuration); _halfOpenState = new HalfOpenState(this, _setting.HalfOpenRequestLimit); _lockObject = new object(); MoveToCloseState(); }