2 // System.Web.QueueManager
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003,2004 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;
34 using System.Threading;
35 using System.Web.Configuration;
41 // keep the defaults in sync with the ones in HttpRuntimeSection.cs
44 int queueLimit = 5000;
47 Exception initialException;
49 public QueueManager ()
55 HttpRuntimeSection config;
57 config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");
59 HttpRuntimeConfig config;
61 config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
63 minFree = config.MinFreeThreads;
64 minLocalFree = config.MinLocalRequestFreeThreads;
65 queueLimit = config.AppRequestQueueLimit;
66 } catch (Exception e) {
71 queue = new Queue (queueLimit);
72 } catch (Exception e) {
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 ());
83 if (initialException == null && ex != null)
84 initialException = ex;
87 public bool HasException {
88 get { return initialException != null; }
91 public Exception InitialException {
92 get { return initialException; }
95 bool CanExecuteRequest (HttpWorkerRequest req)
101 return true; // The J2EE app server manages the thread pool
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);
110 public HttpWorkerRequest GetNextRequest (HttpWorkerRequest req)
112 if (!CanExecuteRequest (req)) {
113 if (!disposing && req != null) {
122 HttpWorkerRequest result;
125 if (result != null) {
136 void Queue (HttpWorkerRequest wr)
138 if (queue.Count < queueLimit) {
143 HttpRuntime.FinishUnavailable (wr);
146 HttpWorkerRequest Dequeue ()
149 return (HttpWorkerRequest) queue.Dequeue ();
154 public void Dispose ()
160 HttpWorkerRequest wr;
161 while ((wr = GetNextRequest (null)) != null)
162 HttpRuntime.FinishUnavailable (wr);