Example #1
0
        public PipelineData <T> Evaluate <T>(Func <T> requestOperation)
        {
            var data = new PipelineData <T>();

            try
            {
                if (requestOperation == null)
                {
                    // todo handle this
                }

                data.Value   = requestOperation();
                data.Success = true;
            }
            catch (WebException)
            {
                // todo: implement logic to handle different error code
                data.Success = false;
            }

            return(data);
        }
Example #2
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));
        }