2 // System.Web.QueueManager
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003-2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Diagnostics;
35 using System.Threading;
36 using System.Web.Configuration;
40 sealed class QueueManager
42 // keep the defaults in sync with the ones in HttpRuntimeSection.cs
45 int queueLimit = 5000;
48 Exception initialException;
49 PerformanceCounter requestsQueuedCounter;
51 public QueueManager ()
56 HttpRuntimeSection config;
58 config = (HttpRuntimeSection) WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime");
59 minFree = config.MinFreeThreads;
60 minLocalFree = config.MinLocalRequestFreeThreads;
61 queueLimit = config.AppRequestQueueLimit;
62 } catch (Exception e) {
67 queue = new Queue (queueLimit);
68 } catch (Exception e) {
72 StringBuilder sb = new StringBuilder ("Several exceptions occurred:\n");
73 sb.AppendFormat ("--- Exception Q1:\n{0}\n", ex.ToString ());
74 sb.AppendFormat ("--- Exception Q2:\n{0}\n", e.ToString ());
75 initialException = new Exception (sb.ToString ());
79 if (initialException == null && ex != null)
80 initialException = ex;
82 requestsQueuedCounter = new PerformanceCounter ("ASP.NET", "Requests Queued");
83 requestsQueuedCounter.RawValue = 0;
86 public bool HasException {
87 get { return initialException != null; }
90 public Exception InitialException {
91 get { return initialException; }
94 bool CanExecuteRequest (HttpWorkerRequest req)
100 return true; // The J2EE app server manages the thread pool
103 ThreadPool.GetAvailableThreads (out threads, out cports);
104 bool local = (req != null && req.GetLocalAddress () == "127.0.0.1");
105 return (threads > minFree) || (local && threads > minLocalFree);
109 public HttpWorkerRequest GetNextRequest (HttpWorkerRequest req)
111 if (!CanExecuteRequest (req)) {
112 if (!disposing && req != null) {
121 HttpWorkerRequest result;
124 if (result != null) {
135 void Queue (HttpWorkerRequest wr)
137 if (queue.Count < queueLimit) {
139 requestsQueuedCounter.Increment ();
143 HttpRuntime.FinishUnavailable (wr);
146 HttpWorkerRequest Dequeue ()
148 if (queue.Count > 0) {
149 HttpWorkerRequest request = (HttpWorkerRequest) queue.Dequeue ();
150 requestsQueuedCounter.Decrement ();
157 public void Dispose ()
163 HttpWorkerRequest wr;
164 while ((wr = GetNextRequest (null)) != null)
165 HttpRuntime.FinishUnavailable (wr);