David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 1 | // |
| 2 | // TThreadPoolServer.cs |
| 3 | // |
| 4 | // Begin: Dec 3, 2007 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 5 | // Authors: |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 6 | // Will Palmeri <wpalmeri@imeem.com> |
| 7 | // |
| 8 | // Distributed under the Thrift Software License |
| 9 | // |
| 10 | // See accompanying file LICENSE or visit the Thrift site at: |
| 11 | // http://developers.facebook.com/thrift/using |
| 12 | using System; |
| 13 | using System.Collections.Generic; |
| 14 | using System.Text; |
| 15 | using System.Threading; |
| 16 | using Thrift.Protocol; |
| 17 | using Thrift.Transport; |
| 18 | |
| 19 | namespace Thrift.Server |
| 20 | { |
| 21 | /// <summary> |
| 22 | /// Server that uses C# built-in ThreadPool to spawn threads when handling requests |
| 23 | /// </summary> |
| 24 | public class TThreadPoolServer : TServer |
| 25 | { |
| 26 | private const int DEFAULT_MIN_THREADS = 10; |
| 27 | private const int DEFAULT_MAX_THREADS = 100; |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 28 | private volatile bool stop = false; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 29 | |
| 30 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport) |
| 31 | :this(processor, serverTransport, |
| 32 | new TTransportFactory(), new TTransportFactory(), |
| 33 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 34 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 38 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) |
| 39 | : this(processor, serverTransport, |
| 40 | new TTransportFactory(), new TTransportFactory(), |
| 41 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 42 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, logDelegate) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 47 | public TThreadPoolServer(TProcessor processor, |
| 48 | TServerTransport serverTransport, |
| 49 | TTransportFactory transportFactory, |
| 50 | TProtocolFactory protocolFactory) |
| 51 | :this(processor, serverTransport, |
| 52 | transportFactory, transportFactory, |
| 53 | protocolFactory, protocolFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 54 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
| 58 | public TThreadPoolServer(TProcessor processor, |
| 59 | TServerTransport serverTransport, |
| 60 | TTransportFactory inputTransportFactory, |
| 61 | TTransportFactory outputTransportFactory, |
| 62 | TProtocolFactory inputProtocolFactory, |
| 63 | TProtocolFactory outputProtocolFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 64 | int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 65 | :base(processor, serverTransport, inputTransportFactory, outputTransportFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 66 | inputProtocolFactory, outputProtocolFactory, logDel) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 67 | { |
| 68 | if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads)) |
| 69 | { |
| 70 | throw new Exception("Error: could not SetMinThreads in ThreadPool"); |
| 71 | } |
| 72 | if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads)) |
| 73 | { |
| 74 | throw new Exception("Error: could not SetMaxThreads in ThreadPool"); |
| 75 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Use new ThreadPool thread for each new client connection |
| 80 | /// </summary> |
| 81 | public override void Serve() |
| 82 | { |
| 83 | try |
| 84 | { |
| 85 | serverTransport.Listen(); |
| 86 | } |
| 87 | catch (TTransportException ttx) |
| 88 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 89 | logDelegate("Error, could not listen on ServerTransport: " + ttx); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 90 | return; |
| 91 | } |
| 92 | |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 93 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 94 | { |
| 95 | int failureCount = 0; |
| 96 | try |
| 97 | { |
| 98 | TTransport client = serverTransport.Accept(); |
| 99 | ThreadPool.QueueUserWorkItem(this.Execute, client); |
| 100 | } |
| 101 | catch (TTransportException ttx) |
| 102 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 103 | if (stop) |
| 104 | { |
| 105 | logDelegate("TThreadPoolServer was shutting down, caught " + ttx.GetType().Name); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | ++failureCount; |
| 110 | logDelegate(ttx.ToString()); |
| 111 | } |
| 112 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 115 | |
| 116 | if (stop) |
| 117 | { |
| 118 | try |
| 119 | { |
| 120 | serverTransport.Close(); |
| 121 | } |
| 122 | catch (TTransportException ttx) |
| 123 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 124 | logDelegate("TServerTransport failed on close: " + ttx.Message); |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 125 | } |
| 126 | stop = false; |
| 127 | } |
| 128 | } |
| 129 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 130 | /// <summary> |
| 131 | /// Loops on processing a client forever |
| 132 | /// threadContext will be a TTransport instance |
| 133 | /// </summary> |
| 134 | /// <param name="threadContext"></param> |
| 135 | private void Execute(Object threadContext) |
| 136 | { |
| 137 | TTransport client = (TTransport)threadContext; |
| 138 | TTransport inputTransport = null; |
| 139 | TTransport outputTransport = null; |
| 140 | TProtocol inputProtocol = null; |
| 141 | TProtocol outputProtocol = null; |
| 142 | try |
| 143 | { |
| 144 | inputTransport = inputTransportFactory.GetTransport(client); |
| 145 | outputTransport = outputTransportFactory.GetTransport(client); |
| 146 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 147 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 148 | while (processor.Process(inputProtocol, outputProtocol)) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 149 | { |
| 150 | //keep processing requests until client disconnects |
| 151 | } |
| 152 | } |
David Reiss | cee1e08 | 2008-04-02 22:10:09 +0000 | [diff] [blame] | 153 | catch (TTransportException) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 154 | { |
| 155 | // Assume the client died and continue silently |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 156 | //Console.WriteLine(ttx); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | catch (Exception x) |
| 160 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 161 | logDelegate("Error: " + x); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (inputTransport != null) |
| 165 | { |
| 166 | inputTransport.Close(); |
| 167 | } |
| 168 | if (outputTransport != null) |
| 169 | { |
| 170 | outputTransport.Close(); |
| 171 | } |
| 172 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame^] | 173 | |
| 174 | public override void Stop() |
| 175 | { |
| 176 | stop = true; |
| 177 | serverTransport.Close(); |
| 178 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 179 | } |
| 180 | } |