Beispiel #1
0
        private void RestoreToAvailable()
        {
            bool isRestored = false;

            if (UnavailableException != null)
            {
                lock (UnavailableLock)
                {
                    if (UnavailableException != null)
                    {
                        UnavailableException = null;
                        UnavailableTime      = null;
                        isRestored           = true;
                    }
                }
            }

            if (isRestored)
            {
                lock (_allObjectsLock)
                    _allObjects.ForEach(a => a.LastGetTime = a.LastReturnTime = new DateTime(2000, 1, 1));

                Policy.OnAvailable();

                TestTrace.WriteLine($"【{Policy.Name}】Recovered", ConsoleColor.DarkGreen);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 后台定时检查可用性
        /// </summary>
        /// <param name="interval"></param>
        private void CheckAvailable(int interval)
        {
            new Thread(() =>
            {
                if (UnavailableException != null)
                {
                    TestTrace.WriteLine($"【{Policy.Name}】Next recovery time:{DateTime.Now.AddSeconds(interval)}", ConsoleColor.DarkYellow);
                }

                while (UnavailableException != null)
                {
                    if (running == false)
                    {
                        return;
                    }

                    Thread.CurrentThread.Join(TimeSpan.FromSeconds(interval));

                    if (running == false)
                    {
                        return;
                    }

                    try
                    {
                        var conn = GetFree(false);
                        if (conn == null)
                        {
                            throw new Exception($"CheckAvailable: Failed to get resource {this.Statistics}");
                        }

                        try
                        {
                            if (Policy.OnCheckAvailable(conn) == false)
                            {
                                throw new Exception("CheckAvailable: An exception needs to be thrown");
                            }
                            break;
                        }
                        finally
                        {
                            Return(conn);
                        }
                    }
                    catch (Exception ex)
                    {
                        TestTrace.WriteLine($"【{Policy.Name}】Next recovery time: {DateTime.Now.AddSeconds(interval)} ({ex.Message})", ConsoleColor.DarkYellow);
                    }
                }

                RestoreToAvailable();
            }).Start();
        }