Exemple #1
0
        public T Pass <T>(Func <T> operation)
        {
            if (currentState == State.Opened)
            {
                // todo: set a default value for fail circuit.
                return(default(T));
            }

            PipelineData <T> data = null;

            if (currentState == State.HalfOpened)
            {
                halfOpenedHits++;
                if (halfOpenedHits % halfOpenedHitsWindows != 0)
                {
                    // todo: set a default value for fail circuit.
                    return(default(T));
                }

                // Allow pass through for every halfOpenedHitsWindows times of operation.
                data = Watcher.Evaluate <T>(operation);

                if (data.Success)
                {
                    // If half opened succeed at least HalfOpenedCounterThreshold of times, change circuit to Closed.
                    if ((halfOpenedHits / halfOpenedHitsWindows) >= HalfOpenedCounterThreshold)
                    {
                        currentState = State.Closed;
                    }

                    return(data.Value);
                }

                return(default(T));
            }

            data = Watcher.Evaluate <T>(operation);

            if (data.Success)
            {
                if (currentState != State.Closed)
                {
                    lock (this.syncLock)
                    {
                        currentState = State.Closed;
                    }
                }

                return(data.Value);
            }

            closedFailureCounter++;
            if (closedFailureCounter >= closedFailureThreshold)
            {
                lock (this.syncLock)
                {
                    currentState = State.Opened;
                }

                this.openedTimer.Start();
            }

            // todo: set a default value for fail circuit.
            return(default(T));
        }