Files
mono/mcs/class/System.Web/System.Web/QueueManager.cs
T
Marek Habersack fbd041c942 2007-05-30 Marek Habersack <[email protected]>
* WebConfigurationManager.cs: if errors happen when opening the
        configuration file, mark the manager as unsafe to prevent further
        usage and avoid error loops.

2007-05-30  Marek Habersack  <[email protected]>

        * TraceManager.cs: Catch all the possible exceptions in the
        constructor - the object MUST be created successfully (even if
        there are errors).

        * HttpRequest.cs: do not attempt to apply url mappings if the
        configuration manager has errors.

        * HttpRuntime.cs: handle static constructor exceptions later on,
        during the first call to ReallyProcessRequest.

        * QueueManager.cs: provide defaults for the parameters to be read
        from the config file(s).
        Catch all the possible exceptions in the constructor - the object
        MUST be created successfully (even if there are errors).


svn path=/trunk/mcs/; revision=78208
2007-05-30 13:50:14 +00:00

169 lines
4.0 KiB
C#

//
// System.Web.QueueManager
//
// Authors:
// Gonzalo Paniagua Javier ([email protected])
//
// (C) 2003,2004 Novell, Inc (http://www.novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Text;
using System.Threading;
using System.Web.Configuration;
namespace System.Web
{
class QueueManager
{
// keep the defaults in sync with the ones in HttpRuntimeSection.cs
int minFree = 8;
int minLocalFree = 4;
int queueLimit = 5000;
Queue queue;
bool disposing;
Exception initialException;
public QueueManager ()
{
Exception ex = null;
try {
#if NET_2_0
HttpRuntimeSection config;
config = (HttpRuntimeSection) WebConfigurationManager.GetSection ("system.web/httpRuntime");
#else
HttpRuntimeConfig config;
config = (HttpRuntimeConfig) HttpContext.GetAppConfig ("system.web/httpRuntime");
#endif
minFree = config.MinFreeThreads;
minLocalFree = config.MinLocalRequestFreeThreads;
queueLimit = config.AppRequestQueueLimit;
} catch (Exception e) {
ex = e;
}
try {
queue = new Queue (queueLimit);
} catch (Exception e) {
if (ex == null) {
initialException = e;
} else {
StringBuilder sb = new StringBuilder ("Several exceptions occurred:\n");
sb.AppendFormat ("--- Exception Q1:\n{0}\n", ex.ToString ());
sb.AppendFormat ("--- Exception Q2:\n{0}\n", e.ToString ());
initialException = new Exception (sb.ToString ());
}
}
if (initialException == null && ex != null)
initialException = ex;
}
public bool HasException {
get { return initialException != null; }
}
public Exception InitialException {
get { return initialException; }
}
bool CanExecuteRequest (HttpWorkerRequest req)
{
if (disposing)
return false;
#if TARGET_J2EE
return true; // The J2EE app server manages the thread pool
#else
int threads, cports;
ThreadPool.GetAvailableThreads (out threads, out cports);
bool local = (req != null && req.GetLocalAddress () == "127.0.0.1");
return (threads > minFree) || (local && threads > minLocalFree);
#endif
}
public HttpWorkerRequest GetNextRequest (HttpWorkerRequest req)
{
if (!CanExecuteRequest (req)) {
if (!disposing && req != null) {
lock (queue) {
Queue (req);
}
}
return null;
}
HttpWorkerRequest result;
lock (queue) {
result = Dequeue ();
if (result != null) {
if (req != null)
Queue (req);
} else {
result = req;
}
}
return result;
}
void Queue (HttpWorkerRequest wr)
{
if (queue.Count < queueLimit) {
queue.Enqueue (wr);
return;
}
HttpRuntime.FinishUnavailable (wr);
}
HttpWorkerRequest Dequeue ()
{
if (queue.Count > 0)
return (HttpWorkerRequest) queue.Dequeue ();
return null;
}
public void Dispose ()
{
if (disposing)
return;
disposing = true;
HttpWorkerRequest wr;
while ((wr = GetNextRequest (null)) != null)
HttpRuntime.FinishUnavailable (wr);
queue = null;
}
}
}