Esempio n. 1
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            // do not validated against empty string here.

            if (null == Match)
            {
                throw new ArgumentNullException("string match (left)");
            }


            if (null == MatchTo)
            {
                throw new ArgumentNullException("string match to(right)");
            }


            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }


            return(MatchString(Match, MatchTo, MatchType));
        }
Esempio n. 2
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }

            var LeftResults = null == Left ? true : await Left.MatchAsync(routingContext, sAddress, Context, Body);

            // short circut
            if (!LeftResults)
            {
                return(false);
            }

            var rightResults = null == Right ? true : await Right.MatchAsync(routingContext, sAddress, Context, Body);

            // short circut
            if (!rightResults)
            {
                return(false);
            }


            return(true);
        }
Esempio n. 3
0
 protected virtual void OnTelemetryResolve(RoutingContextBase routingContext,
                                           double totalms)
 {
     mMinClicker.Click(new RouterClickBase()
     {
         ClickType = RouterClickBase.Resolve_ClickType, Value = totalms
     });
 }
Esempio n. 4
0
 protected virtual void OnTelemetryExecute(RoutingContextBase routingContext,
                                           RoutingResultBase routingResults,
                                           double totalms)
 {
     mMinClicker.Click(new RouterClickBase()
     {
         ClickType = RouterClickBase.Execute_ClickType, Value = totalms
     });
 }
Esempio n. 5
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }

            routingContext.SourceBody = NewBody;
            return(true);
        }
Esempio n. 6
0
        protected virtual async Task <RoutingContextBase> ResolveAsync(RoutingContextBase re,
                                                                       string sAddress,
                                                                       IDictionary <string, object> Context,
                                                                       Stream Body)
        {
            bool bMatched = false;

            while (0 != Interlocked.CompareExchange(ref Changing, 1, 0))
            {
                locker.SpinOnce();
            }

            try
            {
                // can not do matching on empty list

                if (0 == mMatchList.Count)
                {
                    throw new InvalidOperationException("Resolver can not work empty matchers!");
                }

                // try to match
                foreach (var matcher in mMatchList.Values)
                {
                    bMatched = await matcher.MatchAsync(re, sAddress, Context, Body);

                    if (bMatched)
                    {
                        break;
                    }
                }
            }
            finally
            {
                Changing = 0;
            }

            if (!bMatched)
            {
                return(null);
            }

            return((RoutingContextBase)re);
        }
Esempio n. 7
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (string.IsNullOrEmpty(ExpectedHostAddress))
            {
                throw new ArgumentNullException("ExpectedHostAddress");
            }


            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }


            return(MatchString(sAddress, ExpectedHostAddress, HostAddressMatchType));
        }
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (null == Predicate)
            {
                throw new ArgumentNullException("Predicate");
            }



            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }

            return(Predicate(KeyName, Context[KeyName]));
        }
Esempio n. 9
0
        public async virtual Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                    string sAddress,
                                                    IDictionary <string, object> Context,
                                                    Stream Body)
        {
            if (null == Next)
            {
                // only the last (linked list tail) matcher sets the id.
                // copy the matcherTreeId to context;
                routingContext.MatcherTreeId = this.MatcherTreeId;
            }
            else
            {
                if (null != Next)
                {
                    return(await Next.MatchAsync(routingContext, sAddress, Context, Body));
                }
            }

            return(true);
        }
Esempio n. 10
0
        public override async Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            // validate, null list is bad
            if (null == this.TargetHostAddressList)
            {
                throw new ArgumentNullException("TargetHostAddresses");
            }


            // a list that has a null or empty is also bad
            var nullAddresses = TargetHostAddressList.Where(s => string.IsNullOrEmpty(s));

            if (0 != nullAddresses.Count())
            {
                throw new ArgumentNullException("TargetHostAddresses contains null or empty host addresses");
            }



            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }

            if (Clear)
            {
                routingContext.TargetHostAddressList.Clear();
            }


            routingContext.TargetHostAddressList.AddRange(TargetHostAddressList);
            routingContext.RouteExecuteType = this.RoutingType;


            return(true);
        }
Esempio n. 11
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }


            foreach (var p in mPredicates)
            {
                var success = await p.MatchAsync(routingContext, sAddress, Context, Body);

                if (success)
                {
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 12
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (null == Predicate)
            {
                throw new ArgumentNullException("Predicate");
            }



            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }



            if (true == await Predicate.MatchAsync(routingContext, sAddress, Context, Body))
            {
                if (null != True)
                {
                    return(await True.MatchAsync(routingContext, sAddress, Context, Body));
                }
            }
            else
            {
                if (null != False)
                {
                    return(await False.MatchAsync(routingContext, sAddress, Context, Body));
                }
            }


            return(true);
        }
Esempio n. 13
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (string.IsNullOrEmpty(KeyName))
            {
                throw new ArgumentNullException("KeyName");
            }


            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }



            // short circut, if comparison is exact check that the dictionary
            // contains exact key &  we are done.
            if (MatchType == StringMatchType.Exact)
            {
                return(Context.ContainsKey(KeyName));
            }



            foreach (var key in Context.Keys)
            {
                if (MatchString(key, KeyName, MatchType))
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 14
0
        public async override Task <bool> MatchAsync(RoutingContextBase routingContext,
                                                     string sAddress,
                                                     IDictionary <string, object> Context,
                                                     Stream Body)
        {
            if (null == Predicates)
            {
                throw new ArgumentNullException("Predicates");
            }

            var anyNull = Predicates.Where(p => null == p);

            if (0 != anyNull.Count())
            {
                throw new ArgumentNullException("one or more predicates ids null");
            }



            if (false == await base.MatchAsync(routingContext, sAddress, Context, Body))
            {
                return(false);
            }


            foreach (var p in Predicates)
            {
                var success = await p.MatchAsync(routingContext, sAddress, Context, Body);

                if (!success)
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 15
0
        protected virtual async Task <RER> ExecuteContextAsync(RoutingContextBase re, ContextExecuteStrategyBase chs)
        {
            RER       rer        = null;
            Stopwatch sw         = new Stopwatch();
            var       currentChs = chs;
            ContextExecuteModeBase ExecuteMode = new DoNotRetryMode();
            var TryCount = 1;
            var bSucess  = false;


            while (true)
            {
                try
                {
                    sw.Start();
                    rer = (RER)await re.ExecuteAsync(ExecuteMode);

                    sw.Stop();
                    bSucess = true;
                }
                catch (Exception e)
                {
                    if (null == currentChs)
                    {
                        throw;
                    }

                    TryCount++;

                    // circut breaker
                    if (TryCount > mMaxRetryCount)
                    {
                        throw new RouterMaxedRetryException(new AggregateException(e))
                              {
                                  RetryCount = (TryCount - 1), RoutingContext = re
                              }
                    }
                    ;


                    try
                    {
                        // execute current strategy;
                        ExecuteMode = await currentChs.ExecuteStrategyAsync(TryCount, re, new AggregateException(e));
                    }
                    catch (Exception StrategyExecuteFail)
                    {
                        // if we failed to execute strategy we just wrap the exception
                        // along with the orginal execution one.
                        throw new FailedToExecuteStrategyException(StrategyExecuteFail, e)
                              {
                                  TryCount = TryCount, RoutingContext = re
                              };
                    }

                    // move to next strategy
                    currentChs = currentChs.Next;


                    if (!ExecuteMode.ShouldRouterTryAgain)
                    {
                        throw new AggregateException(e);
                    }
                }

                finally
                {
                    OnTelemetryExecute(re, rer, sw.Elapsed.TotalMilliseconds);
                    Trace.WriteLine(string.Format("Execute in {0} Call Count {1} Routing Type:{2}",
                                                  sw.Elapsed.TotalMilliseconds, TryCount, re.RouteExecuteType), "Verbose");

                    sw.Reset(); // reset the clock
                }

                if (bSucess)
                {
                    break; // exit the loop
                }
            }


            return(rer);
        }
Esempio n. 16
0
        public override async Task <ContextExecuteModeBase> ExecuteStrategyAsync(int CallCount, RoutingContextBase re, AggregateException ae)
        {
            await Task.Delay(mDelayMs);

            return(new RetryMode());
        }
Esempio n. 17
0
        public override Task <ContextExecuteModeBase> ExecuteStrategyAsync(int CallCount, RoutingContextBase re, AggregateException ae)
        {
            var exps = (null == Exceptions) ? new List <Exception>() : Exceptions;

            if (0 == exps.Count())
            {
                return(Task.FromResult((ContextExecuteModeBase) new DoNotRetryMode()));
            }


            var aex = ae.Flatten();

            if (exps.Contains(aex.InnerException))
            {
                return(Task.FromResult((ContextExecuteModeBase) new RetryMode()));
            }

            return(Task.FromResult((ContextExecuteModeBase) new DoNotRetryMode()));
        }
Esempio n. 18
0
 public abstract Task <ContextExecuteModeBase> ExecuteStrategyAsync(int CallCount, RoutingContextBase re, AggregateException ae);