예제 #1
0
        /// <summary>
        /// Send a http request with special header to loop back address to by pass the cache
        /// </summary>
        /// <returns></returns>
        protected override async Task <IPhoenixState> FireAsync()
        {
            try
            {
                var sw = Stopwatch.StartNew();
                using (var client = GetHttpClient())
                {
                    client.Timeout = TimeSpan.FromSeconds(_cacheItem.MaxAge - _cacheItem.Age + Math.Max(_cacheItem.StaleWhileRevalidate, _cacheItem.StaleIfError));
                    _clonedRequestMessage.Headers.CacheControl.Extensions.Add(new NameValueHeaderValue(WebApiExtensions.__cacheControl_flatwhite_force_refresh, "true"));
                    _clonedRequestMessage.Properties.Clear();
                    var response = await client.SendAsync(_clonedRequestMessage, HttpCompletionOption.ResponseHeadersRead);

                    response.EnsureSuccessStatusCode();
                }

                sw.Stop();
                Global.Logger.Info($"{nameof(WebApiPhoenix)} updated key: \"{_cacheItem.Key}\", store: \"{_cacheItem.StoreId}\", request: {_clonedRequestMessage.RequestUri.PathAndQuery}, duration: {sw.ElapsedMilliseconds}ms");
                Retry(_info.GetRefreshTime());
                _phoenixState = new InActivePhoenix();
                return(_phoenixState);
            }
            catch (Exception ex)
            {
                Global.Logger.Error($"Error while refreshing key {_info.Key}, store \"{_info.StoreId}\". Will retry after 1 second.", ex);
                Retry(TimeSpan.FromSeconds(1));
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// Send a http request with special header to loop back address to by pass the cache
        /// </summary>
        /// <returns></returns>
        protected override async Task<IPhoenixState> FireAsync()
        {
            try
            {
                var sw = Stopwatch.StartNew();
                using (var client = GetHttpClient())
                {
                    //client.Timeout = TimeSpan.FromSeconds(_cacheItem.MaxAge - _cacheItem.Age + Math.Max(_cacheItem.StaleWhileRevalidate, _cacheItem.StaleIfError));
                    var cloneRequestMessage = CloneRequestMessage(_originalRequestMessage);
                    var response = await client.SendAsync(cloneRequestMessage, HttpCompletionOption.ResponseHeadersRead);
                    response.EnsureSuccessStatusCode();
                }

                sw.Stop();
                Global.Logger.Info($"{nameof(WebApiPhoenix)} updated key: \"{_cacheItem.Key}\", store: \"{_cacheItem.StoreId}\", request: {_originalRequestMessage.RequestUri.PathAndQuery}, duration: {sw.ElapsedMilliseconds}ms");
                Retry(_info.GetRefreshTime());
                _phoenixState = new InActivePhoenix();
                return _phoenixState;
            }
            catch (Exception ex)
            {
                Global.Logger.Error($"Error while refreshing key {_info.Key}, store \"{_info.StoreId}\". Will retry after 1 second.", ex);
                Retry(TimeSpan.FromSeconds(1));
                throw;
            }
        }
        public void Should_create_RaisingPhoenix()
        {
            var wait = new AutoResetEvent(false);
            Func<Task<IPhoenixState>> action = () =>
            {
                wait.Set();
                IPhoenixState phoenixState = new InActivePhoenix();
                return Task.FromResult(phoenixState);
            };

            var state = new InActivePhoenix();
            state.Reborn(action);
            Assert.IsTrue(wait.WaitOne(1000));
        }
예제 #4
0
        public void Should_create_RaisingPhoenix()
        {
            var wait = new AutoResetEvent(false);
            Func <Task <IPhoenixState> > action = () =>
            {
                wait.Set();
                IPhoenixState phoenixState = new InActivePhoenix();
                return(Task.FromResult(phoenixState));
            };

            var state = new InActivePhoenix();

            state.Reborn(action);
            Assert.IsTrue(wait.WaitOne(1000));
        }
예제 #5
0
        public void GetState_should_return_status()
        {
            var state = new InActivePhoenix();

            Assert.IsTrue(state.GetState().StartsWith("inactive for "));
        }
 public void GetState_should_return_status()
 {
     var state = new InActivePhoenix();
     Assert.IsTrue(state.GetState().StartsWith("inactive for "));
 }