Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from symform/master
Browse files Browse the repository at this point in the history
Changes to improve handling more load
  • Loading branch information
grendello committed Aug 29, 2011
2 parents d8ccb7a + 1cbe4d8 commit d463279
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
9 changes: 9 additions & 0 deletions man/xsp.1.in
Expand Up @@ -54,6 +54,15 @@ requests. By default XSP listens on port 8080 and mod-mono-server has
no default. no default.
AppSettings key name: MonoServerPort AppSettings key name: MonoServerPort
.TP .TP
.I \-\-backlog N
The backlog of connections to set on the listener socket.
By default the value is set to 500.
.TP
.I \-\-minThreads N
The minimum number of threads the threadpool allocates. Increase this value
to better handle the sudden arrival of connections.
The default value is determined by the mono runtime.
.TP
.I \-\-filename file (mod-mono-server and fastcgi-mono-server) .I \-\-filename file (mod-mono-server and fastcgi-mono-server)
The unix socket file name to listen on. The unix socket file name to listen on.
Default value: /tmp/mod_mono_server (fastcgi-mono-server: /tmp/fastcgi-mono-server) Default value: /tmp/mod_mono_server (fastcgi-mono-server: /tmp/fastcgi-mono-server)
Expand Down
31 changes: 30 additions & 1 deletion src/Mono.WebServer.Apache/main.cs
Expand Up @@ -113,6 +113,7 @@ static void ShowHelp ()
Console.WriteLine (" Default value: 127.0.0.1"); Console.WriteLine (" Default value: 127.0.0.1");
Console.WriteLine (" AppSettings key name: MonoServerAddress"); Console.WriteLine (" AppSettings key name: MonoServerAddress");
Console.WriteLine (); Console.WriteLine ();
Console.WriteLine (" --backlog N: the listen backlog. Default value: 500");
Console.WriteLine (" --root rootdir: the server changes to this directory before"); Console.WriteLine (" --root rootdir: the server changes to this directory before");
Console.WriteLine (" anything else."); Console.WriteLine (" anything else.");
Console.WriteLine (" Default value: current directory."); Console.WriteLine (" Default value: current directory.");
Expand Down Expand Up @@ -151,6 +152,10 @@ static void ShowHelp ()
Console.WriteLine (" Default value: /:."); Console.WriteLine (" Default value: /:.");
Console.WriteLine (" AppSettings key name: MonoApplications"); Console.WriteLine (" AppSettings key name: MonoApplications");
Console.WriteLine (); Console.WriteLine ();
Console.WriteLine (" --minThreads N: the minimum number of threads the thread pool creates on startup.");
Console.WriteLine (" Increase this value to handle a sudden inflow of connections.");
Console.WriteLine (" Default value: (runtime default)");
Console.WriteLine (" --backlog N: the listen backlog. Default value: 500");
Console.WriteLine (" --terminate: gracefully terminates a running mod-mono-server instance."); Console.WriteLine (" --terminate: gracefully terminates a running mod-mono-server instance.");
Console.WriteLine (" All other options but --filename or --address and --port"); Console.WriteLine (" All other options but --filename or --address and --port");
Console.WriteLine (" are ignored if this option is provided."); Console.WriteLine (" are ignored if this option is provided.");
Expand Down Expand Up @@ -253,6 +258,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
settings.RootDir = ext_apphost.Path; settings.RootDir = ext_apphost.Path;


Options options = 0; Options options = 0;
int backlog = 500;
int hash = 0; int hash = 0;
for (int i = 0; i < args.Length; i++){ for (int i = 0; i < args.Length; i++){
string a = args [i]; string a = args [i];
Expand All @@ -279,6 +285,15 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.Address, ref options); CheckAndSetOptions (a, Options.Address, ref options);
settings.IP = args [++i]; settings.IP = args [++i];
break; break;
case "--backlog":
string backlogstr = args [++i];
try {
backlog = Convert.ToInt32 (backlogstr);
} catch (Exception) {
Console.WriteLine ("The value given for backlog is not valid {0}", backlogstr);
return 1;
}
break;
case "--root": case "--root":
CheckAndSetOptions (a, Options.Root, ref options); CheckAndSetOptions (a, Options.Root, ref options);
settings.RootDir = args [++i]; settings.RootDir = args [++i];
Expand All @@ -294,6 +309,20 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
case "--appconfigdir": case "--appconfigdir":
CheckAndSetOptions (a, Options.AppConfigDir, ref options); CheckAndSetOptions (a, Options.AppConfigDir, ref options);
settings.AppConfigDir = args [++i]; settings.AppConfigDir = args [++i];
break;
case "--minThreads":
string mtstr = args [++i];
int minThreads = 0;
try {
minThreads = Convert.ToInt32 (mtstr);
} catch (Exception) {
Console.WriteLine ("The value given for minThreads is not valid {0}", mtstr);
return 1;
}

if (minThreads > 0)
ThreadPool.SetMinThreads(minThreads, minThreads);

break; break;
case "--nonstop": case "--nonstop":
settings.NonStop = true; settings.NonStop = true;
Expand Down Expand Up @@ -435,7 +464,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
Console.Error.WriteLine ("Root directory: {0}", settings.RootDir); Console.Error.WriteLine ("Root directory: {0}", settings.RootDir);


try { try {
if (server.Start (!settings.NonStop) == false) if (server.Start (!settings.NonStop, backlog) == false)
return 2; return 2;


if (!settings.NonStop) { if (!settings.NonStop) {
Expand Down
30 changes: 29 additions & 1 deletion src/Mono.WebServer.XSP/main.cs
Expand Up @@ -109,6 +109,10 @@ static void ShowHelp ()
Console.WriteLine (" Default value: 0.0.0.0"); Console.WriteLine (" Default value: 0.0.0.0");
Console.WriteLine (" AppSettings key name: MonoServerAddress"); Console.WriteLine (" AppSettings key name: MonoServerAddress");
Console.WriteLine (); Console.WriteLine ();
Console.WriteLine (" --minThreads N: the minimum number of threads the thread pool creates on startup.");
Console.WriteLine (" Increase this value to handle a sudden inflow of connections.");
Console.WriteLine (" Default value: (runtime default)");
Console.WriteLine (" --backlog N: the listen backlog. Default value: 500");
Console.WriteLine (" --https: enable SSL for the server"); Console.WriteLine (" --https: enable SSL for the server");
Console.WriteLine (" Default value: false."); Console.WriteLine (" Default value: false.");
Console.WriteLine (" AppSettings key name: "); Console.WriteLine (" AppSettings key name: ");
Expand Down Expand Up @@ -289,6 +293,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo


Options options = 0; Options options = 0;
int hash = 0; int hash = 0;
int backlog = 500;
for (int i = 0; i < args.Length; i++){ for (int i = 0; i < args.Length; i++){
string a = args [i]; string a = args [i];
int idx = (i + 1 < args.Length) ? i + 1 : i; int idx = (i + 1 < args.Length) ? i + 1 : i;
Expand Down Expand Up @@ -338,6 +343,15 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.Address, ref options); CheckAndSetOptions (a, Options.Address, ref options);
settings.IP = args [++i]; settings.IP = args [++i];
break; break;
case "--backlog":
string backlogstr = args [++i];
try {
backlog = Convert.ToInt32 (backlogstr);
} catch (Exception) {
Console.WriteLine ("The value given for backlog is not valid {0}", backlogstr);
return 1;
}
break;
case "--root": case "--root":
CheckAndSetOptions (a, Options.Root, ref options); CheckAndSetOptions (a, Options.Root, ref options);
settings.RootDir = args [++i]; settings.RootDir = args [++i];
Expand All @@ -353,6 +367,20 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
case "--appconfigdir": case "--appconfigdir":
CheckAndSetOptions (a, Options.AppConfigDir, ref options); CheckAndSetOptions (a, Options.AppConfigDir, ref options);
settings.AppConfigDir = args [++i]; settings.AppConfigDir = args [++i];
break;
case "--minThreads":
string mtstr = args [++i];
int minThreads = 0;
try {
minThreads = Convert.ToInt32 (mtstr);
} catch (Exception) {
Console.WriteLine ("The value given for minThreads is not valid {0}", mtstr);
return 1;
}

if (minThreads > 0)
ThreadPool.SetMinThreads(minThreads, minThreads);

break; break;
case "--nonstop": case "--nonstop":
settings.NonStop = true; settings.NonStop = true;
Expand Down Expand Up @@ -469,7 +497,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
} }


try { try {
if (server.Start (!settings.NonStop, settings.Exception) == false) if (server.Start (!settings.NonStop, settings.Exception, backlog) == false)
return 2; return 2;


if (!quiet) { if (!quiet) {
Expand Down
49 changes: 34 additions & 15 deletions src/Mono.WebServer/ApplicationServer.cs
Expand Up @@ -295,13 +295,13 @@ public void AddApplicationsFromCommandLine (string applications)
} }
} }


public bool Start (bool bgThread, Exception initialException) public bool Start (bool bgThread, Exception initialException, int backlog)
{ {
this.initialException = initialException; this.initialException = initialException;
return Start (bgThread); return Start (bgThread, backlog);
} }


public bool Start (bool bgThread) public bool Start (bool bgThread, int backlog)
{ {
if (started) if (started)
throw new InvalidOperationException ("The server is already started."); throw new InvalidOperationException ("The server is already started.");
Expand All @@ -319,8 +319,7 @@ public bool Start (bool bgThread)
} }


listen_socket = webSource.CreateSocket (); listen_socket = webSource.CreateSocket ();
listen_socket.Listen (500); listen_socket.Listen (backlog);
listen_socket.Blocking = false;
runner = new Thread (new ThreadStart (RunServer)); runner = new Thread (new ThreadStart (RunServer));
runner.IsBackground = bgThread; runner.IsBackground = bgThread;
runner.Start (); runner.Start ();
Expand Down Expand Up @@ -402,32 +401,52 @@ void RunServer ()


void OnAccept (object sender, EventArgs e) void OnAccept (object sender, EventArgs e)
{ {
Socket accepted = null;
SocketAsyncEventArgs args = (SocketAsyncEventArgs) e; SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
if (args.SocketError == SocketError.Success) { Socket accepted = args.AcceptSocket;
accepted = args.AcceptSocket; args.AcceptSocket = null;
args.AcceptSocket = null;
if (args.SocketError != SocketError.Success) {
CloseSocket(accepted);
accepted = null;
} }


try { try {
if (started) if (started)
listen_socket.AcceptAsync (args); listen_socket.AcceptAsync (args);
} catch (Exception ex) { } catch (Exception ex) {
if (accepted != null) { CloseSocket(accepted);
SendException (accepted, ex);
accepted.Close (); // not much we can do. fast fail by killing the process.
throw; Console.Error.WriteLine ("Unable to accept socket. Exiting the process. {0}", ex);
} Environment.Exit (1);
} }


if (accepted == null) if (accepted == null)
return; return;


accepted.Blocking = true;
SetSocketOptions (accepted); SetSocketOptions (accepted);
StartRequest (accepted, 0); StartRequest (accepted, 0);
} }


void CloseSocket(Socket socket)
{
if (socket == null)
return;

// attempt a quick RST of the connection
try {
socket.LingerState = new LingerOption (true, 0);
} catch {
// ignore
}

try {
socket.Close();
} catch {
// ignore
}
}

void SendException (Socket socket, Exception ex) void SendException (Socket socket, Exception ex)
{ {
StringBuilder sb = new StringBuilder (); StringBuilder sb = new StringBuilder ();
Expand Down

0 comments on commit d463279

Please sign in to comment.