Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
Todd Lipcon | 53ae9f3 | 2009-12-07 00:42:38 +0000 | [diff] [blame] | 18 | * |
| 19 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 24 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 25 | using System.Threading; |
| 26 | using Thrift.Protocol; |
| 27 | using Thrift.Transport; |
| 28 | |
| 29 | namespace Thrift.Server |
| 30 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 31 | /// <summary> |
| 32 | /// Server that uses C# built-in ThreadPool to spawn threads when handling requests |
| 33 | /// </summary> |
| 34 | public class TThreadPoolServer : TServer |
| 35 | { |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 36 | private const int DEFAULT_MIN_THREADS = -1; // use .NET ThreadPool defaults |
| 37 | private const int DEFAULT_MAX_THREADS = -1; // use .NET ThreadPool defaults |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 38 | private volatile bool stop = false; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 39 | |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 40 | public struct Configuration |
| 41 | { |
| 42 | public int MinWorkerThreads; |
| 43 | public int MaxWorkerThreads; |
| 44 | public int MinIOThreads; |
| 45 | public int MaxIOThreads; |
| 46 | |
| 47 | public Configuration(int min = DEFAULT_MIN_THREADS, int max = DEFAULT_MAX_THREADS) |
| 48 | { |
| 49 | MinWorkerThreads = min; |
| 50 | MaxWorkerThreads = max; |
| 51 | MinIOThreads = min; |
| 52 | MaxIOThreads = max; |
| 53 | } |
| 54 | |
| 55 | public Configuration(int minWork, int maxWork, int minIO, int maxIO) |
| 56 | { |
| 57 | MinWorkerThreads = minWork; |
| 58 | MaxWorkerThreads = maxWork; |
| 59 | MinIOThreads = minIO; |
| 60 | MaxIOThreads = maxIO; |
| 61 | } |
| 62 | } |
| 63 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 64 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 65 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 66 | new TTransportFactory(), new TTransportFactory(), |
| 67 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 68 | new Configuration(), DefaultLogDelegate) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 69 | { |
| 70 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 71 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 72 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 73 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 74 | new TTransportFactory(), new TTransportFactory(), |
| 75 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 76 | new Configuration(), logDelegate) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 77 | { |
| 78 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 79 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 80 | public TThreadPoolServer(TProcessor processor, |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 81 | TServerTransport serverTransport, |
| 82 | TTransportFactory transportFactory, |
| 83 | TProtocolFactory protocolFactory) |
| 84 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
| 85 | transportFactory, transportFactory, |
| 86 | protocolFactory, protocolFactory, |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 87 | new Configuration(), DefaultLogDelegate) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
| 91 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 92 | TServerTransport serverTransport, |
| 93 | TTransportFactory transportFactory, |
| 94 | TProtocolFactory protocolFactory) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 95 | : this(processorFactory, serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 96 | transportFactory, transportFactory, |
| 97 | protocolFactory, protocolFactory, |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 98 | new Configuration(), DefaultLogDelegate) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 99 | { |
| 100 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 101 | |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 102 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 103 | TServerTransport serverTransport, |
| 104 | TTransportFactory inputTransportFactory, |
| 105 | TTransportFactory outputTransportFactory, |
| 106 | TProtocolFactory inputProtocolFactory, |
| 107 | TProtocolFactory outputProtocolFactory, |
| 108 | int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel) |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 109 | : this(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
| 110 | inputProtocolFactory, outputProtocolFactory, |
| 111 | new Configuration(minThreadPoolThreads, maxThreadPoolThreads), |
| 112 | logDel) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | public TThreadPoolServer(TProcessorFactory processorFactory, |
| 117 | TServerTransport serverTransport, |
| 118 | TTransportFactory inputTransportFactory, |
| 119 | TTransportFactory outputTransportFactory, |
| 120 | TProtocolFactory inputProtocolFactory, |
| 121 | TProtocolFactory outputProtocolFactory, |
| 122 | Configuration threadConfig, |
| 123 | LogDelegate logDel) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 124 | : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 125 | inputProtocolFactory, outputProtocolFactory, logDel) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 126 | { |
| 127 | lock (typeof(TThreadPoolServer)) |
| 128 | { |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 129 | if ((threadConfig.MaxWorkerThreads > 0) || (threadConfig.MaxIOThreads > 0)) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 130 | { |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 131 | int work, comm; |
| 132 | ThreadPool.GetMaxThreads(out work, out comm); |
| 133 | if (threadConfig.MaxWorkerThreads > 0) |
| 134 | work = threadConfig.MaxWorkerThreads; |
| 135 | if (threadConfig.MaxIOThreads > 0) |
| 136 | comm = threadConfig.MaxIOThreads; |
| 137 | if (!ThreadPool.SetMaxThreads(work, comm)) |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame] | 138 | throw new Exception("Error: could not SetMaxThreads in ThreadPool"); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 139 | } |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 140 | |
| 141 | if ((threadConfig.MinWorkerThreads > 0) || (threadConfig.MinIOThreads > 0)) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 142 | { |
Jens Geyer | d1380d5 | 2017-05-12 22:49:57 +0200 | [diff] [blame] | 143 | int work, comm; |
| 144 | ThreadPool.GetMinThreads(out work, out comm); |
| 145 | if (threadConfig.MinWorkerThreads > 0) |
| 146 | work = threadConfig.MinWorkerThreads; |
| 147 | if (threadConfig.MinIOThreads > 0) |
| 148 | comm = threadConfig.MinIOThreads; |
| 149 | if (!ThreadPool.SetMinThreads(work, comm)) |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame] | 150 | throw new Exception("Error: could not SetMinThreads in ThreadPool"); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 154 | |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 155 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 156 | /// <summary> |
| 157 | /// Use new ThreadPool thread for each new client connection |
| 158 | /// </summary> |
| 159 | public override void Serve() |
| 160 | { |
| 161 | try |
| 162 | { |
| 163 | serverTransport.Listen(); |
| 164 | } |
| 165 | catch (TTransportException ttx) |
| 166 | { |
| 167 | logDelegate("Error, could not listen on ServerTransport: " + ttx); |
| 168 | return; |
| 169 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 170 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 171 | //Fire the preServe server event when server is up but before any client connections |
| 172 | if (serverEventHandler != null) |
| 173 | serverEventHandler.preServe(); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 174 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 175 | while (!stop) |
| 176 | { |
| 177 | int failureCount = 0; |
| 178 | try |
| 179 | { |
| 180 | TTransport client = serverTransport.Accept(); |
| 181 | ThreadPool.QueueUserWorkItem(this.Execute, client); |
| 182 | } |
| 183 | catch (TTransportException ttx) |
| 184 | { |
Jens Geyer | 7d88208 | 2015-01-27 22:08:44 +0100 | [diff] [blame] | 185 | if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 186 | { |
| 187 | ++failureCount; |
| 188 | logDelegate(ttx.ToString()); |
| 189 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 190 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 193 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 194 | if (stop) |
| 195 | { |
| 196 | try |
| 197 | { |
| 198 | serverTransport.Close(); |
| 199 | } |
| 200 | catch (TTransportException ttx) |
| 201 | { |
| 202 | logDelegate("TServerTransport failed on close: " + ttx.Message); |
| 203 | } |
| 204 | stop = false; |
| 205 | } |
| 206 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 207 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 208 | /// <summary> |
| 209 | /// Loops on processing a client forever |
| 210 | /// threadContext will be a TTransport instance |
| 211 | /// </summary> |
| 212 | /// <param name="threadContext"></param> |
| 213 | private void Execute(Object threadContext) |
| 214 | { |
| 215 | TTransport client = (TTransport)threadContext; |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 216 | TProcessor processor = processorFactory.GetProcessor(client, this); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 217 | TTransport inputTransport = null; |
| 218 | TTransport outputTransport = null; |
| 219 | TProtocol inputProtocol = null; |
| 220 | TProtocol outputProtocol = null; |
| 221 | Object connectionContext = null; |
| 222 | try |
| 223 | { |
| 224 | inputTransport = inputTransportFactory.GetTransport(client); |
| 225 | outputTransport = outputTransportFactory.GetTransport(client); |
| 226 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 227 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 228 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 229 | //Recover event handler (if any) and fire createContext server event when a client connects |
| 230 | if (serverEventHandler != null) |
| 231 | connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); |
| 232 | |
| 233 | //Process client requests until client disconnects |
Jens Geyer | 7d88208 | 2015-01-27 22:08:44 +0100 | [diff] [blame] | 234 | while (!stop) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 235 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 236 | if (!inputTransport.Peek()) |
Jens Geyer | eb8e5ad | 2014-09-29 21:50:15 +0200 | [diff] [blame] | 237 | break; |
| 238 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 239 | //Fire processContext server event |
| 240 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 241 | //That is to say it may be many minutes between the event firing and the client request |
| 242 | //actually arriving or the client may hang up without ever makeing a request. |
| 243 | if (serverEventHandler != null) |
| 244 | serverEventHandler.processContext(connectionContext, inputTransport); |
| 245 | //Process client request (blocks until transport is readable) |
| 246 | if (!processor.Process(inputProtocol, outputProtocol)) |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | catch (TTransportException) |
| 251 | { |
| 252 | //Usually a client disconnect, expected |
| 253 | } |
| 254 | catch (Exception x) |
| 255 | { |
| 256 | //Unexpected |
| 257 | logDelegate("Error: " + x); |
| 258 | } |
| 259 | |
| 260 | //Fire deleteContext server event after client disconnects |
| 261 | if (serverEventHandler != null) |
| 262 | serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); |
| 263 | |
| 264 | //Close transports |
| 265 | if (inputTransport != null) |
| 266 | inputTransport.Close(); |
| 267 | if (outputTransport != null) |
| 268 | outputTransport.Close(); |
| 269 | } |
| 270 | |
| 271 | public override void Stop() |
| 272 | { |
| 273 | stop = true; |
| 274 | serverTransport.Close(); |
| 275 | } |
| 276 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 277 | } |