2008-01-23 Olivier Dufour <olivier.duff@gmail.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.Text;
34 using System.Threading;
35 using System.Web.Configuration;
36
37 namespace System.Web
38 {
39         class QueueManager
40         {
41                 // keep the defaults in sync with the ones in HttpRuntimeSection.cs
42                 int minFree = 8;
43                 int minLocalFree = 4;
44                 int queueLimit = 5000;
45                 Queue queue;
46                 bool disposing;
47                 Exception initialException;
48                 
49                 public QueueManager ()
50                 {
51                         Exception ex = null;
52                         
53                         try {
54 #if NET_2_0
55                                 HttpRuntimeSection config;
56
57                                 config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");
58 #else
59                                 HttpRuntimeConfig config;
60
61                                 config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
62 #endif
63                                 minFree = config.MinFreeThreads;
64                                 minLocalFree = config.MinLocalRequestFreeThreads;
65                                 queueLimit = config.AppRequestQueueLimit;
66                         } catch (Exception e) {
67                                 ex = e;
68                         }
69
70                         try {
71                                 queue = new Queue (queueLimit);
72                         } catch (Exception e) {
73                                 if (ex == null) {
74                                         initialException = e;
75                                 } else {
76                                         StringBuilder sb = new StringBuilder ("Several exceptions occurred:\n");
77                                         sb.AppendFormat ("--- Exception Q1:\n{0}\n", ex.ToString ());
78                                         sb.AppendFormat ("--- Exception Q2:\n{0}\n", e.ToString ());
79                                         initialException = new Exception (sb.ToString ());
80                                 }
81                         }
82
83                         if (initialException == null && ex != null)
84                                 initialException = ex;
85                 }
86
87                 public bool HasException {
88                         get { return initialException != null; }
89                 }
90
91                 public Exception InitialException {
92                         get { return initialException; }
93                 }
94                 
95                 bool CanExecuteRequest (HttpWorkerRequest req)
96                 {
97                         if (disposing)
98                                 return false;
99                                 
100 #if TARGET_J2EE
101                         return true; // The J2EE app server manages the thread pool
102 #else
103                         int threads, cports;
104                         ThreadPool.GetAvailableThreads (out threads, out cports);
105                         bool local = (req != null && req.GetLocalAddress () == "127.0.0.1");
106                         return (threads > minFree) || (local && threads > minLocalFree);
107 #endif
108                 }
109
110                 public HttpWorkerRequest GetNextRequest (HttpWorkerRequest req)
111                 {
112                         if (!CanExecuteRequest (req)) {
113                                 if (!disposing && req != null) {
114                                         lock (queue) {
115                                                 Queue (req);
116                                         }
117                                 }
118
119                                 return null;
120                         }
121
122                         HttpWorkerRequest result;
123                         lock (queue) {
124                                 result = Dequeue ();
125                                 if (result != null) {
126                                         if (req != null)
127                                                 Queue (req);
128                                 } else {
129                                         result = req;
130                                 }
131                         }
132
133                         return result;
134                 }
135                 
136                 void Queue (HttpWorkerRequest wr)
137                 {
138                         if (queue.Count < queueLimit) {
139                                 queue.Enqueue (wr);
140                                 return;
141                         }
142
143                         HttpRuntime.FinishUnavailable (wr);
144                 }
145
146                 HttpWorkerRequest Dequeue ()
147                 {
148                         if (queue.Count > 0)
149                                 return (HttpWorkerRequest) queue.Dequeue ();
150
151                         return null;
152                 }
153
154                 public void Dispose ()
155                 {
156                         if (disposing)
157                                 return;
158
159                         disposing = true;
160                         HttpWorkerRequest wr;
161                         while ((wr = GetNextRequest (null)) != null)
162                                 HttpRuntime.FinishUnavailable (wr);
163
164                         queue = null;
165                 }
166         }
167 }
168