Permalink
Browse files

Merge pull request #3 from symform/master

Changes to improve handling more load
  • Loading branch information...
2 parents d8ccb7a + 1cbe4d8 commit d463279284470587d812bcde7941dcd92fd594c3 @grendello grendello committed Aug 29, 2011
Showing with 102 additions and 17 deletions.
  1. +9 −0 man/xsp.1.in
  2. +30 −1 src/Mono.WebServer.Apache/main.cs
  3. +29 −1 src/Mono.WebServer.XSP/main.cs
  4. +34 −15 src/Mono.WebServer/ApplicationServer.cs
View
@@ -54,6 +54,15 @@ requests. By default XSP listens on port 8080 and mod-mono-server has
no default.
AppSettings key name: MonoServerPort
.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)
The unix socket file name to listen on.
Default value: /tmp/mod_mono_server (fastcgi-mono-server: /tmp/fastcgi-mono-server)
@@ -113,6 +113,7 @@ static void ShowHelp ()
Console.WriteLine (" Default value: 127.0.0.1");
Console.WriteLine (" AppSettings key name: MonoServerAddress");
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 (" anything else.");
Console.WriteLine (" Default value: current directory.");
@@ -151,6 +152,10 @@ static void ShowHelp ()
Console.WriteLine (" Default value: /:.");
Console.WriteLine (" AppSettings key name: MonoApplications");
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 (" All other options but --filename or --address and --port");
Console.WriteLine (" are ignored if this option is provided.");
@@ -253,6 +258,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
settings.RootDir = ext_apphost.Path;
Options options = 0;
+ int backlog = 500;
int hash = 0;
for (int i = 0; i < args.Length; i++){
string a = args [i];
@@ -279,6 +285,15 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.Address, ref options);
settings.IP = args [++i];
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":
CheckAndSetOptions (a, Options.Root, ref options);
settings.RootDir = args [++i];
@@ -295,6 +310,20 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.AppConfigDir, ref options);
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;
case "--nonstop":
settings.NonStop = true;
break;
@@ -435,7 +464,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
Console.Error.WriteLine ("Root directory: {0}", settings.RootDir);
try {
- if (server.Start (!settings.NonStop) == false)
+ if (server.Start (!settings.NonStop, backlog) == false)
return 2;
if (!settings.NonStop) {
@@ -109,6 +109,10 @@ static void ShowHelp ()
Console.WriteLine (" Default value: 0.0.0.0");
Console.WriteLine (" AppSettings key name: MonoServerAddress");
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 (" Default value: false.");
Console.WriteLine (" AppSettings key name: ");
@@ -289,6 +293,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
Options options = 0;
int hash = 0;
+ int backlog = 500;
for (int i = 0; i < args.Length; i++){
string a = args [i];
int idx = (i + 1 < args.Length) ? i + 1 : i;
@@ -338,6 +343,15 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.Address, ref options);
settings.IP = args [++i];
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":
CheckAndSetOptions (a, Options.Root, ref options);
settings.RootDir = args [++i];
@@ -354,6 +368,20 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
CheckAndSetOptions (a, Options.AppConfigDir, ref options);
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;
case "--nonstop":
settings.NonStop = true;
break;
@@ -469,7 +497,7 @@ public int RealMain (string [] args, bool root, IApplicationHost ext_apphost, bo
}
try {
- if (server.Start (!settings.NonStop, settings.Exception) == false)
+ if (server.Start (!settings.NonStop, settings.Exception, backlog) == false)
return 2;
if (!quiet) {
@@ -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;
- return Start (bgThread);
+ return Start (bgThread, backlog);
}
- public bool Start (bool bgThread)
+ public bool Start (bool bgThread, int backlog)
{
if (started)
throw new InvalidOperationException ("The server is already started.");
@@ -319,8 +319,7 @@ public bool Start (bool bgThread)
}
listen_socket = webSource.CreateSocket ();
- listen_socket.Listen (500);
- listen_socket.Blocking = false;
+ listen_socket.Listen (backlog);
runner = new Thread (new ThreadStart (RunServer));
runner.IsBackground = bgThread;
runner.Start ();
@@ -402,32 +401,52 @@ void RunServer ()
void OnAccept (object sender, EventArgs e)
{
- Socket accepted = null;
SocketAsyncEventArgs args = (SocketAsyncEventArgs) e;
- if (args.SocketError == SocketError.Success) {
- accepted = args.AcceptSocket;
- args.AcceptSocket = null;
+ Socket accepted = args.AcceptSocket;
+ args.AcceptSocket = null;
+
+ if (args.SocketError != SocketError.Success) {
+ CloseSocket(accepted);
+ accepted = null;
}
try {
if (started)
listen_socket.AcceptAsync (args);
} catch (Exception ex) {
- if (accepted != null) {
- SendException (accepted, ex);
- accepted.Close ();
- throw;
- }
+ CloseSocket(accepted);
+
+ // not much we can do. fast fail by killing the process.
+ Console.Error.WriteLine ("Unable to accept socket. Exiting the process. {0}", ex);
+ Environment.Exit (1);
}
if (accepted == null)
return;
- accepted.Blocking = true;
SetSocketOptions (accepted);
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)
{
StringBuilder sb = new StringBuilder ();

0 comments on commit d463279

Please sign in to comment.