Index: trunk/mwsearch/Daemon/HttpHandler.cs |
— | — | @@ -45,6 +45,30 @@ |
46 | 46 | ostrm = new StreamWriter(stream); |
47 | 47 | } |
48 | 48 | |
| 49 | + private static int _openCount = 0; |
| 50 | + private static object _countLock = new object(); |
| 51 | + |
| 52 | + public static int OpenCount { |
| 53 | + get { |
| 54 | + lock (_countLock) { |
| 55 | + return _openCount; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private static void _Enter() { |
| 61 | + lock (_countLock) { |
| 62 | + _openCount++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private static void _Leave() { |
| 67 | + lock (_countLock) { |
| 68 | + _openCount--; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
49 | 73 | public void Run(object par) { |
50 | 74 | Run(); |
51 | 75 | } |
— | — | @@ -53,6 +77,7 @@ |
54 | 78 | //using (log4net.NDC.Push(client.Client.RemoteEndPoint)) { |
55 | 79 | headersSent = false; |
56 | 80 | try { |
| 81 | + _Enter(); |
57 | 82 | Handle(); |
58 | 83 | log.Debug("request handled."); |
59 | 84 | } catch (IOException e) { |
— | — | @@ -68,6 +93,7 @@ |
69 | 94 | // Make sure the client is closed out. |
70 | 95 | try { ostrm.Close(); } catch { } |
71 | 96 | try { istrm.Close(); } catch { } |
| 97 | + _Leave(); |
72 | 98 | } |
73 | 99 | } |
74 | 100 | |
Index: trunk/mwsearch/Daemon/Daemon.cs |
— | — | @@ -44,6 +44,7 @@ |
45 | 45 | private static Configuration config; |
46 | 46 | |
47 | 47 | private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
| 48 | + |
48 | 49 | |
49 | 50 | public static void Main(string[] args) { |
50 | 51 | Console.WriteLine( |
— | — | @@ -76,6 +77,11 @@ |
77 | 78 | } |
78 | 79 | sock.Start(); |
79 | 80 | |
| 81 | + int maxWorkers = 10; |
| 82 | + string max = config.GetString("Daemon", "maxworkers"); |
| 83 | + if (max != null) |
| 84 | + maxWorkers = int.Parse(max); |
| 85 | + |
80 | 86 | /* |
81 | 87 | log.Debug("Blah blah debug"); |
82 | 88 | log.Info("Blah blah info"); |
— | — | @@ -93,8 +99,13 @@ |
94 | 100 | log.Error("accept() error: " + e.Message); |
95 | 101 | continue; |
96 | 102 | } |
97 | | - Worker worker = new Worker(client.GetStream(), config); |
98 | | - ThreadPool.QueueUserWorkItem(worker.Run); |
| 103 | + |
| 104 | + if (Worker.OpenCount > maxWorkers) { |
| 105 | + log.Error("too many connections, skipping a request"); |
| 106 | + } else { |
| 107 | + Worker worker = new Worker(client.GetStream(), config); |
| 108 | + ThreadPool.QueueUserWorkItem(worker.Run); |
| 109 | + } |
99 | 110 | } |
100 | 111 | |
101 | 112 | } |
Index: trunk/mwsearch/Daemon/Worker.cs |
— | — | @@ -75,6 +75,7 @@ |
76 | 76 | this.config = config; |
77 | 77 | } |
78 | 78 | |
| 79 | + |
79 | 80 | protected override void DoStuff() { |
80 | 81 | if (uri.AbsolutePath == "/robots.txt") { |
81 | 82 | RobotsTxt(); |