2009-02-28 Gonzalo Paniagua Javier <gonzalo@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / QueueManager.cs
1 //
2 // System.Web.QueueManager
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003,2004 Novell, Inc (http://www.novell.com)
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Collections;
33 using System.Diagnostics;
34 using System.Text;
35 using System.Threading;
36 using System.Web.Configuration;
37
38 namespace System.Web
39 {
40         class QueueManager
41         {
42                 // keep the defaults in sync with the ones in HttpRuntimeSection.cs
43                 int minFree = 8;
44                 int minLocalFree = 4;
45                 int queueLimit = 5000;
46                 Queue queue;
47                 bool disposing;
48                 Exception initialException;
49                 PerformanceCounter requestsQueuedCounter;
50                 
51                 public QueueManager ()
52                 {
53                         Exception ex = null;
54                         
55                         try {
56 #if NET_2_0
57                                 HttpRuntimeSection config;
58
59                                 config = (HttpRuntimeSection) WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime");
60 #else
61                                 HttpRuntimeConfig config;
62
63                                 config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
64 #endif
65                                 minFree = config.MinFreeThreads;
66                                 minLocalFree = config.MinLocalRequestFreeThreads;
67                                 queueLimit = config.AppRequestQueueLimit;
68                         } catch (Exception e) {
69                                 ex = e;
70                         }
71
72                         try {
73                                 queue = new Queue (queueLimit);
74                         } catch (Exception e) {
75                                 if (ex == null) {
76                                         initialException = e;
77                                 } else {
78                                         StringBuilder sb = new StringBuilder ("Several exceptions occurred:\n");
79                                         sb.AppendFormat ("--- Exception Q1:\n{0}\n", ex.ToString ());
80                                         sb.AppendFormat ("--- Exception Q2:\n{0}\n", e.ToString ());
81                                         initialException = new Exception (sb.ToString ());
82                                 }
83                         }
84
85                         if (initialException == null && ex != null)
86                                 initialException = ex;
87
88                         requestsQueuedCounter = new PerformanceCounter ("ASP.NET", "Requests Queued");
89                         requestsQueuedCounter.RawValue = 0;
90                 }
91
92                 public bool HasException {
93                         get { return initialException != null; }
94                 }
95
96                 public Exception InitialException {
97                         get { return initialException; }
98                 }
99                 
100                 bool CanExecuteRequest (HttpWorkerRequest req)
101                 {
102                         if (disposing)
103                                 return false;
104                                 
105 #if TARGET_J2EE
106                         return true; // The J2EE app server manages the thread pool
107 #else
108                         int threads, cports;
109                         ThreadPool.GetAvailableThreads (out threads, out cports);
110                         bool local = (req != null && req.GetLocalAddress () == "127.0.0.1");
111                         return (threads > minFree) || (local && threads > minLocalFree);
112 #endif
113                 }
114
115                 public HttpWorkerRequest GetNextRequest (HttpWorkerRequest req)
116                 {
117                         if (!CanExecuteRequest (req)) {
118                                 if (!disposing && req != null) {
119                                         lock (queue) {
120                                                 Queue (req);
121                                         }
122                                 }
123
124                                 return null;
125                         }
126
127                         HttpWorkerRequest result;
128                         lock (queue) {
129                                 result = Dequeue ();
130                                 if (result != null) {
131                                         if (req != null)
132                                                 Queue (req);
133                                 } else {
134                                         result = req;
135                                 }
136                         }
137
138                         return result;
139                 }
140                 
141                 void Queue (HttpWorkerRequest wr)
142                 {
143                         if (queue.Count < queueLimit) {
144                                 queue.Enqueue (wr);
145                                 requestsQueuedCounter.Increment ();
146                                 return;
147                         }
148
149                         HttpRuntime.FinishUnavailable (wr);
150                 }
151
152                 HttpWorkerRequest Dequeue ()
153                 {
154                         if (queue.Count > 0) {
155                                 HttpWorkerRequest request = (HttpWorkerRequest) queue.Dequeue ();
156                                 requestsQueuedCounter.Decrement ();
157                                 return request;
158                         }
159
160                         return null;
161                 }
162
163                 public void Dispose ()
164                 {
165                         if (disposing)
166                                 return;
167
168                         disposing = true;
169                         HttpWorkerRequest wr;
170                         while ((wr = GetNextRequest (null)) != null)
171                                 HttpRuntime.FinishUnavailable (wr);
172
173                         queue = null;
174                 }
175         }
176 }
177