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