Esempio n. 1
0
 public void From(Routes.UrlRoute urlRoute)
 {
     Url               = urlRoute.Url;
     HashPattern       = urlRoute.HashPattern;
     this.PluginConfig = new PluginConfig(urlRoute.Pluginer);
     foreach (var server in urlRoute.Servers)
     {
         Servers.Add(new RouteServer {
             Url = server.Agent.Uri.ToString(), Weight = server.Weight, MaxRps = server.MaxRPS
         });
     }
 }
Esempio n. 2
0
 public void From(Routes.UrlRoute urlRoute)
 {
     Url         = urlRoute.Url;
     HashPattern = urlRoute.HashPattern;
     foreach (var filter in urlRoute.FilterNames)
     {
         Filters.Add(filter);
     }
     foreach (var server in urlRoute.Servers)
     {
         Servers.Add(new RouteServer {
             Url = server.Agent.Uri.ToString(), Weight = server.Weight, MaxRps = server.MaxRPS
         });
     }
 }
Esempio n. 3
0
        public void Execute(HttpRequest request, HttpResponse response)
        {
            var agent = UrlRoute.GetServerAgent(request);

            if (agent == null)
            {
                if (request.Server.EnableLog(LogType.Info))
                {
                    request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server unavailable");
                }

                Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(
                    request, response, UrlRoute.Gateway, $"The {Url} url route server unavailable", Gateway.URL_NODE_SERVER_UNAVAILABLE);
                UrlRoute.Gateway.ProcessError(Gateway.URL_NODE_SERVER_UNAVAILABLE, request);
                UrlRoute.Gateway.OnResponseError(erea);
            }
            else
            {
                if (UrlRoute.Pluginer.AgentRequesting(request, response, agent.Agent, UrlRoute))
                {
                    agent.Increment();
                    if (agent.ValidateRPS())
                    {
                        agent.Agent.Execute(request, response, agent, UrlRoute);
                    }
                    else
                    {
                        string error = $"Unable to reach {agent.Agent.Uri} HTTP request, exceeding maximum number of RPS";
                        Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(request, response,
                                                                                               UrlRoute.Gateway, error, Gateway.SERVER_MAX_OF_RPS);
                        UrlRoute.Gateway.OnResponseError(erea);
                        UrlRoute.Gateway.ProcessError(Gateway.SERVER_MAX_OF_RPS, request);
                        if (request.Server.EnableLog(LogType.Info))
                        {
                            request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server exceeding maximum number of RPS");
                        }
                    }
                }
                else
                {
                    if (request.Server.EnableLog(LogType.Info))
                    {
                        request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server exceeding cancel");
                    }
                }
            }
        }
Esempio n. 4
0
 public UrlRoute NewOrGet(string url, string hashPattern = null, bool apiLoader = true)
 {
     if (url == "*")
     {
         Default.BuildHashPattern(hashPattern);
         return(Default);
     }
     if (!mUrlRoutes.TryGetValue(url, out UrlRoute item))
     {
         item            = new UrlRoute(Gateway, url);
         item.ApiLoader  = apiLoader;
         mUrlRoutes[url] = item;
         UpdateUrlTable();
     }
     item.ApiLoader = apiLoader;
     item.BuildHashPattern(hashPattern);
     return(item);
 }
Esempio n. 5
0
 public void From(Routes.UrlRoute urlRoute)
 {
     Url         = urlRoute.Url;
     Remark      = urlRoute.Remark;
     HashPattern = urlRoute.HashPattern;
     MaxRps      = urlRoute.MaxRps;
     TimeOut     = urlRoute.TimeOut;
     AccessControlAllowHeaders = urlRoute.AccessControlAllowHeaders;
     AccessControlAllowMethods = urlRoute.AccessControlAllowMethods;
     AccessControlAllowOrigin  = urlRoute.AccessControlAllowOrigin;
     AccessControlMaxAge       = urlRoute.AccessControlMaxAge;
     Vary = urlRoute.Vary;
     AccessControlAllowCredentials = urlRoute.AccessControlAllowCredentials;
     this.PluginConfig             = new PluginConfig(urlRoute.Pluginer);
     foreach (var server in urlRoute.Servers)
     {
         Servers.Add(new RouteServer {
             Url = server.Agent.Uri.ToString(), Weight = server.Weight, MaxRps = server.MaxRPS, Standby = server.Standby
         });
     }
 }
Esempio n. 6
0
 public RouteCenter(Gateway gateway)
 {
     Gateway = gateway;
     Default = new UrlRoute(gateway, "*");
 }
Esempio n. 7
0
 internal bool OnAgentRequesting(HttpRequest request, HttpResponse response, Servers.ServerAgent server, Routes.UrlRoute urlRoute)
 {
     if (AgentRequesting != null)
     {
         EventAgentRequestingArgs e = new EventAgentRequestingArgs(request, response, this, server, urlRoute);
         e.Cancel = false;
         AgentRequesting?.Invoke(this, e);
         return(!e.Cancel);
     }
     return(true);
 }
Esempio n. 8
0
 public RouteCenter(Gateway gateway)
 {
     Gateway          = gateway;
     Default          = new UrlRoute(gateway, "*");
     mUrlStatisticsDB = new UrlStatisticsMemoryDB(gateway);
 }
Esempio n. 9
0
        public void Execute(HttpRequest request, HttpResponse response)
        {
            if (request.Server.EnableLog(LogType.Debug))
            {
                request.Server.Log(LogType.Debug, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s get urlroute agent!");
            }
            if (string.Compare(request.Method, "OPTIONS", true) == 0)
            {
                if (!string.IsNullOrEmpty(UrlRoute.AccessControlAllowOrigin))
                {
                    OptionsAttribute oa = new OptionsAttribute();
                    oa.AllowCredentials = UrlRoute.AccessControlAllowCredentials;
                    oa.AllowHeaders     = UrlRoute.AccessControlAllowHeaders;
                    oa.AllowMaxAge      = UrlRoute.AccessControlMaxAge > 0 ? UrlRoute.AccessControlMaxAge.ToString() : null;
                    oa.AllowMethods     = UrlRoute.AccessControlAllowMethods;
                    oa.AllowOrigin      = UrlRoute.AccessControlAllowOrigin;
                    oa.Vary             = UrlRoute.Vary;
                    response.Result(oa);
                    return;
                }
            }
            if (!UrlRoute.ValidateRPS())
            {
                string error = $"Unable to reach [{UrlRoute.Url} route {request.Url}] in  HTTP request, exceeding maximum number of rps limit";
                Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(request, response,
                                                                                       UrlRoute.Gateway, error, Gateway.SERVER_MAX_OF_RPS);
                UrlRoute.Gateway.OnResponseError(erea);
                if (request.Server.EnableLog(LogType.Info))
                {
                    request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server exceeding maximum number of rps limit");
                }
                return;
            }

            var agent = UrlRoute.GetServerAgent(request);

            if (agent == null)
            {
                if (request.Server.EnableLog(LogType.Info))
                {
                    request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server unavailable");
                }

                Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(
                    request, response, UrlRoute.Gateway, $"The {Url} url route server unavailable", Gateway.URL_NODE_SERVER_UNAVAILABLE);
                UrlRoute.Gateway.OnResponseError(erea);
            }
            else
            {
                if (request.Server.EnableLog(LogType.Debug))
                {
                    request.Server.Log(LogType.Debug, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s AgentRequesting event!");
                }
                if (UrlRoute.Pluginer.AgentRequesting(request, response, agent.Agent, UrlRoute))
                {
                    agent.Increment();
                    if (agent.ValidateRPS())
                    {
                        if (request.Server.EnableLog(LogType.Debug))
                        {
                            request.Server.Log(LogType.Debug, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s agent execute!");
                        }
                        agent.Agent.Execute(request, response, agent, UrlRoute);
                    }
                    else
                    {
                        string error = $"Unable to reach {agent.Agent.Uri} HTTP request, exceeding maximum number of rps limit";
                        Events.EventResponseErrorArgs erea = new Events.EventResponseErrorArgs(request, response,
                                                                                               UrlRoute.Gateway, error, Gateway.SERVER_MAX_OF_RPS);
                        UrlRoute.Gateway.OnResponseError(erea);
                        if (request.Server.EnableLog(LogType.Info))
                        {
                            request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server exceeding maximum number of rps limit");
                        }
                    }
                }
                else
                {
                    if (request.Server.EnableLog(LogType.Info))
                    {
                        request.Server.Log(LogType.Info, $"Gateway {request.RemoteIPAddress} {request.Method} {request.Url} request {UrlRoute.Url}'s route server exceeding cancel");
                    }
                }
            }
        }
Esempio n. 10
0
 public RouteCenter(Gateway gateway)
 {
     Gateway = gateway;
     Default = new UrlRoute(gateway, "*", string.Empty);
 }