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 | { |
| 36 | private const int DEFAULT_MIN_THREADS = 10; |
| 37 | private const int DEFAULT_MAX_THREADS = 100; |
| 38 | private volatile bool stop = false; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 39 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 40 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 41 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 42 | new TTransportFactory(), new TTransportFactory(), |
| 43 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 44 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 45 | { |
| 46 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 47 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 48 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 49 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 50 | new TTransportFactory(), new TTransportFactory(), |
| 51 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 52 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, logDelegate) |
| 53 | { |
| 54 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 55 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 56 | public TThreadPoolServer(TProcessor processor, |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 57 | TServerTransport serverTransport, |
| 58 | TTransportFactory transportFactory, |
| 59 | TProtocolFactory protocolFactory) |
| 60 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
| 61 | transportFactory, transportFactory, |
| 62 | protocolFactory, protocolFactory, |
| 63 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 68 | TServerTransport serverTransport, |
| 69 | TTransportFactory transportFactory, |
| 70 | TProtocolFactory protocolFactory) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 71 | : this(processorFactory, serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 72 | transportFactory, transportFactory, |
| 73 | protocolFactory, protocolFactory, |
| 74 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 75 | { |
| 76 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 77 | |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 78 | public TThreadPoolServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 79 | TServerTransport serverTransport, |
| 80 | TTransportFactory inputTransportFactory, |
| 81 | TTransportFactory outputTransportFactory, |
| 82 | TProtocolFactory inputProtocolFactory, |
| 83 | TProtocolFactory outputProtocolFactory, |
| 84 | int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 85 | : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 86 | inputProtocolFactory, outputProtocolFactory, logDel) |
| 87 | { |
| 88 | lock (typeof(TThreadPoolServer)) |
| 89 | { |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame^] | 90 | if(maxThreadPoolThreads > 0) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 91 | { |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame^] | 92 | if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads)) |
| 93 | { |
| 94 | throw new Exception("Error: could not SetMaxThreads in ThreadPool"); |
| 95 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 96 | } |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame^] | 97 | if (minThreadPoolThreads > 0) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 98 | { |
Jens Geyer | 224c334 | 2017-05-06 22:38:43 +0200 | [diff] [blame^] | 99 | if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads)) |
| 100 | { |
| 101 | throw new Exception("Error: could not SetMinThreads in ThreadPool"); |
| 102 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 106 | |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 107 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 108 | /// <summary> |
| 109 | /// Use new ThreadPool thread for each new client connection |
| 110 | /// </summary> |
| 111 | public override void Serve() |
| 112 | { |
| 113 | try |
| 114 | { |
| 115 | serverTransport.Listen(); |
| 116 | } |
| 117 | catch (TTransportException ttx) |
| 118 | { |
| 119 | logDelegate("Error, could not listen on ServerTransport: " + ttx); |
| 120 | return; |
| 121 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 122 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 123 | //Fire the preServe server event when server is up but before any client connections |
| 124 | if (serverEventHandler != null) |
| 125 | serverEventHandler.preServe(); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 126 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 127 | while (!stop) |
| 128 | { |
| 129 | int failureCount = 0; |
| 130 | try |
| 131 | { |
| 132 | TTransport client = serverTransport.Accept(); |
| 133 | ThreadPool.QueueUserWorkItem(this.Execute, client); |
| 134 | } |
| 135 | catch (TTransportException ttx) |
| 136 | { |
Jens Geyer | 7d88208 | 2015-01-27 22:08:44 +0100 | [diff] [blame] | 137 | if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 138 | { |
| 139 | ++failureCount; |
| 140 | logDelegate(ttx.ToString()); |
| 141 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 142 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 145 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 146 | if (stop) |
| 147 | { |
| 148 | try |
| 149 | { |
| 150 | serverTransport.Close(); |
| 151 | } |
| 152 | catch (TTransportException ttx) |
| 153 | { |
| 154 | logDelegate("TServerTransport failed on close: " + ttx.Message); |
| 155 | } |
| 156 | stop = false; |
| 157 | } |
| 158 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 159 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 160 | /// <summary> |
| 161 | /// Loops on processing a client forever |
| 162 | /// threadContext will be a TTransport instance |
| 163 | /// </summary> |
| 164 | /// <param name="threadContext"></param> |
| 165 | private void Execute(Object threadContext) |
| 166 | { |
| 167 | TTransport client = (TTransport)threadContext; |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 168 | TProcessor processor = processorFactory.GetProcessor(client, this); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 169 | TTransport inputTransport = null; |
| 170 | TTransport outputTransport = null; |
| 171 | TProtocol inputProtocol = null; |
| 172 | TProtocol outputProtocol = null; |
| 173 | Object connectionContext = null; |
| 174 | try |
| 175 | { |
| 176 | inputTransport = inputTransportFactory.GetTransport(client); |
| 177 | outputTransport = outputTransportFactory.GetTransport(client); |
| 178 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 179 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 180 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 181 | //Recover event handler (if any) and fire createContext server event when a client connects |
| 182 | if (serverEventHandler != null) |
| 183 | connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); |
| 184 | |
| 185 | //Process client requests until client disconnects |
Jens Geyer | 7d88208 | 2015-01-27 22:08:44 +0100 | [diff] [blame] | 186 | while (!stop) |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 187 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 188 | if (!inputTransport.Peek()) |
Jens Geyer | eb8e5ad | 2014-09-29 21:50:15 +0200 | [diff] [blame] | 189 | break; |
| 190 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 191 | //Fire processContext server event |
| 192 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 193 | //That is to say it may be many minutes between the event firing and the client request |
| 194 | //actually arriving or the client may hang up without ever makeing a request. |
| 195 | if (serverEventHandler != null) |
| 196 | serverEventHandler.processContext(connectionContext, inputTransport); |
| 197 | //Process client request (blocks until transport is readable) |
| 198 | if (!processor.Process(inputProtocol, outputProtocol)) |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | catch (TTransportException) |
| 203 | { |
| 204 | //Usually a client disconnect, expected |
| 205 | } |
| 206 | catch (Exception x) |
| 207 | { |
| 208 | //Unexpected |
| 209 | logDelegate("Error: " + x); |
| 210 | } |
| 211 | |
| 212 | //Fire deleteContext server event after client disconnects |
| 213 | if (serverEventHandler != null) |
| 214 | serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); |
| 215 | |
| 216 | //Close transports |
| 217 | if (inputTransport != null) |
| 218 | inputTransport.Close(); |
| 219 | if (outputTransport != null) |
| 220 | outputTransport.Close(); |
| 221 | } |
| 222 | |
| 223 | public override void Stop() |
| 224 | { |
| 225 | stop = true; |
| 226 | serverTransport.Close(); |
| 227 | } |
| 228 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 229 | } |