r9090 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r9089‎ | r9090 | r9091 >
Date:01:37, 18 May 2005
Author:vibber
Status:old
Tags:
Comment:
Switch from crappy Queue/Wait/Pulse thing to using the ThreadPool.
Modified paths:
  • /trunk/mwsearch/Daemon/Daemon.cs (modified) (history)
  • /trunk/mwsearch/Daemon/Worker.cs (modified) (history)
  • /trunk/mwsearch/Search.mds (modified) (history)

Diff [purge]

Index: trunk/mwsearch/Daemon/Daemon.cs
@@ -47,34 +47,22 @@
4848
4949 private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
5050
51 - private static Queue openConnections;
52 -
5351 public static void Main(string[] args) {
5452 Console.WriteLine(
5553 "MediaWiki Lucene search indexer - runtime search daemon.\n" +
56 - "Version 20050103, copyright 2004 Kate Turner.\n"
 54+ "Version 20050517, copyright 2004 Kate Turner.\n"
5755 );
58 - /*
5956 int i = 0;
60 - while (i < args.length - 1) {
61 - if (args[i].equals("-port"))
62 - port = Integer.valueOf(args[++i]).intValue();
63 - else if (args[i].equals("-configfile"))
64 - Configuration.setConfigFile(args[++i]);
65 - else if (args[i].equals("-mwversion")) {
66 - String vers = args[++i];
67 - if (vers.equals("old"))
68 - mw_version = MW_OLD;
69 - else if (vers.equals("new"))
70 - mw_version = MW_NEW;
71 - else {
72 - System.err.println("Unknown MediaWiki version " + vers);
73 - return;
74 - }
75 - } else break;
 57+ while (i < args.Length - 1) {
 58+ if (args[i].Equals("--port")) {
 59+ port = Int32.Parse(args[++i]);
 60+ } else if (args[i].Equals("--configfile")) {
 61+ Configuration.SetConfigFile(args[++i]);
 62+ } else {
 63+ break;
 64+ }
7665 ++i;
7766 }
78 - */
7967 config = Configuration.Open();
8068
8169 indexPath = config.GetString("mwsearch", "indexpath");
@@ -88,26 +76,13 @@
8977 }
9078 sock.Start();
9179
 80+ /*
9281 log.Debug("Blah blah debug");
9382 log.Info("Blah blah info");
9483 log.Error("Blah blah error");
9584 log.Fatal("Blah blah fatal");
 85+ */
9686
97 - Queue xxx = new Queue();
98 - openConnections = Queue.Synchronized(xxx);
99 -
100 - // start some worker threads
101 - int maxThreads = 25;
102 - IList threads = new ArrayList();
103 - for (int i = 0; i < maxThreads; i++) {
104 - log.Debug("Starting worker thread #" + i);
105 - Worker worker = new Worker(i);
106 - ThreadStart start = new ThreadStart(worker.Run);
107 - Thread thread = new Thread(start);
108 - threads.Add(thread);
109 - thread.Start();
110 - }
111 -
11287 // go!
11388 for (;;) {
11489 TcpClient client;
@@ -118,21 +93,10 @@
11994 log.Error("accept() error: " + e.Message);
12095 continue;
12196 }
122 - lock (openConnections) {
123 - log.Debug("Queueing connection...");
124 - openConnections.Enqueue(client);
125 - Monitor.Pulse(openConnections);
126 - }
 97+ Worker worker = new Worker(client.GetStream());
 98+ ThreadPool.QueueUserWorkItem(worker.Run);
12799 }
128100
129101 }
130 -
131 - public static TcpClient NextClient() {
132 - lock (openConnections) {
133 - Monitor.Wait(openConnections);
134 - TcpClient client = (TcpClient)openConnections.Dequeue();
135 - return client;
136 - }
137 - }
138102 }
139103 }
Index: trunk/mwsearch/Daemon/Worker.cs
@@ -52,8 +52,6 @@
5353 private static readonly Encoding utf8 = new UTF8Encoding();
5454
5555
56 - /** A socket for our client. */
57 - TcpClient client;
5856 /** The search term after urlencoding */
5957 string searchterm;
6058 /** Client input stream */
@@ -72,55 +70,46 @@
7371 bool headersSent;
7472
7573 // lucene special chars: + - && || ! ( ) { } [ ] ^ " ~ * ? : \.
 74+ /*
7675 static string[] specialChars = {
7776 "\\+", "-", "&&", "\\|\\|", "!", "\\(", "\\)", "\\{", "\\}", "\\[", "\\]",
7877 "\\^", "\"", "~", "\\*", "\\?", ":", "\\\\"
7978 //"\\(", "\\)"
8079 };
 80+ */
8181
82 - public Worker(TcpClient newclient) {
83 - this.client = newclient;
 82+ public Worker(Stream stream) {
 83+ istrm = new StreamReader(stream);
 84+ ostrm = new StreamWriter(stream);
8485 }
85 - public Worker(int i) {
86 - num = i;
 86+
 87+ public void Run(object par) {
 88+ Run();
8789 }
88 - int num;
8990
9091 public void Run() {
91 - Console.WriteLine("wtf" + num);
92 - log.Info("starting handler #" + num);
9392 //using (log4net.NDC.Push(client.Client.RemoteEndPoint)) {
94 - for (;;) {
95 - client = Daemon.NextClient();
96 - using (log4net.NDC.Push("thread" + num)) {
97 - log.Debug("Thread " + num + " accepted a client");
98 - headersSent = false;
 93+ headersSent = false;
 94+ try {
 95+ Handle();
 96+ log.Debug("request handled.");
 97+ } catch (IOException e) {
 98+ log.Error("net error: " + e.Message);
 99+ } catch (Exception e) {
 100+ log.Error(e);
 101+ } finally {
 102+ if (!headersSent) {
99103 try {
100 - Handle();
101 - log.Debug("request handled.");
102 - } catch (IOException e) {
103 - log.Error("net error: " + e.Message);
104 - } catch (Exception e) {
105 - log.Error(e);
106 - } finally {
107 - if (!headersSent) {
108 - try {
109 - SendHeaders(500, "Internal server error");
110 - } catch (IOException e) { }
111 - }
112 - // Make sure the client is closed out.
113 - try { ostrm.Close(); } catch { }
114 - try { istrm.Close(); } catch { }
115 - try { client.Close(); } catch { }
116 - }
 104+ SendHeaders(500, "Internal server error");
 105+ } catch (IOException e) { }
117106 }
 107+ // Make sure the client is closed out.
 108+ try { ostrm.Close(); } catch { }
 109+ try { istrm.Close(); } catch { }
118110 }
119111 }
120112
121113 private void Handle() {
122 - istrm = new StreamReader(client.GetStream());
123 - ostrm = new StreamWriter(client.GetStream());
124 -
125114 /* Simple HTTP protocol; accepts GET requests only.
126115 * URL path format is /operation/database/searchterm
127116 * The path should be URL-encoded UTF-8 (standard IRI).
Index: trunk/mwsearch/Search.mds
@@ -17,7 +17,7 @@
1818 <Entry build="False" name="Tests" />
1919 </Configuration>
2020 </Configurations>
21 - <StartMode startupentry="Benchmark" single="True">
 21+ <StartMode startupentry="Daemon" single="True">
2222 <Execute type="None" entry="Search" />
2323 <Execute type="None" entry="Updater" />
2424 <Execute type="Execute" entry="Daemon" />

Status & tagging log