コード例 #1
0
 //  try to cache the response, if it hasn't already been
 private void TryCache(Request req, Response res)
 {
     if (EnableOutputCache && req.CacheAllowed && req.Method != "POST")
     {
         OutputCache.TryAdd((req.AbsPath + req.QueryString), res);
     }
 }
コード例 #2
0
        private static void AddBenchRndCD()
        {
            var dict = new Concurrent.ConcurrentDictionary <int, string>();
            var cnt  = new Counter32();

            var benchmarkName = "======== Random Add Concurrent int->string 1M Ops/sec:";

            Action <int, int> act = (i, threadBias) =>
            {
                // get some random index in [0, 1000000]
                int randomIndex = GetRandomIndex(i, threadBias, 1000000);
                dict.TryAdd(randomIndex, "qq");

                // after making about 1000000 adds, start with a new table
                var c = cnt;
                c.Increment();
                if (Every8K(i) && c.Value > 1000000)
                {
                    if (Interlocked.CompareExchange(ref cnt, new Counter32(), c) == c)
                    {
                        dict = new Concurrent.ConcurrentDictionary <int, string>();
                    }
                }
            };

            RunBench(benchmarkName, act);
        }
コード例 #3
0
        //  handles the request on a separate ThreadPool thread.
        private void HandleRequestAsync(AsyncReceiveState asyncState)
        {
            //  TODO: first try pull the request from the request cache, otherwise parse it now
            //  convert the raw request bytes/string into an HttpRequest object for ease of use
            string  reqString = Text.Encoding.ASCII.GetString(asyncState.Buffer).Trim('\0');
            Request req;

            //  pull the request object from the request cache, or create it now
            if (RequestCache.ContainsKey(reqString))
            {
                req = RequestCache[reqString];
            }
            else
            {
                req = new Request(reqString, this, asyncState.Socket, asyncState.Site);
                RequestCache.TryAdd(reqString, req);
            }

            //  first try to serve the response from the output cache
            bool servedFromCache = false;

            //bool cacheAllowed = true;
            if (EnableOutputCache && req.CacheAllowed)
            {
                if (OutputCache.ContainsKey((req.AbsPath + req.QueryString)))
                {
                    Response res = OutputCache[(req.AbsPath + req.QueryString)];
                    SendResponse(req, res, asyncState.Socket);
                    servedFromCache = true;
                    DebugMessage(("Serving resource from cache: "
                                  + (req.AbsPath + ".")), DebugMessageType.UsageMessage, "HandleRequestAsync");
                }
            }

            //  response couldn't be served from cache, handle the request according to the site role
            if (servedFromCache)
            {
                //  if the current site is a reverse proxy/load balancer with upstream servers defined, forward the request to another server,
                //    otherwise handle the request by this server like normal
                if (asyncState.Site.Role == "Standard")
                {
                    //  raise an event to handle the request/response cycle (this can be overridden during implementation to allow for custom handling)
                    HandleRequest(new Tuple <object, object>(req, asyncState.Socket), null);
                }
                else if (asyncState.Site.Role == "Load Balancer")
                {
                    //  parse the upstream servers and select one using the defined algorithm
                    string[] upstreams = asyncState.Site.Upstream.Split(',');
                    Random   r         = new Random();
                    int      i         = r.Next(0, upstreams.Length);
                    //  forward the request to the selected upstream server
                    ProxyRequest(new Tuple <object, object, object>(req, upstreams[i], asyncState.Socket), null); //???
                }
            }
        }
コード例 #4
0
        private static void SingleThreadedSequentialAddWithGapsCD()
        {
            var dict = new Concurrent.ConcurrentDictionary <int, int>();

            for (var i = 0; i < 8; ++i)
            {
                Stopwatch sw  = Stopwatch.StartNew();
                var       key = (i + 1) * 50_000_000;

                for (var j = 0; j < 10_000_000; ++j)
                {
                    dict.TryAdd(key + j, j);
                }

                sw.Stop();
                System.Console.Write(sw.ElapsedMilliseconds + " ");

                GC.Collect();
            }

            System.Console.WriteLine();
        }