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 | { |
| 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; |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 38 | private volatile bool stop = false; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 39 | |
| 40 | public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport) |
| 41 | :this(processor, serverTransport, |
| 42 | new TTransportFactory(), new TTransportFactory(), |
| 43 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 44 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [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 | } |
| 55 | |
| 56 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [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, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 64 | DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 65 | { |
| 66 | } |
| 67 | |
| 68 | public TThreadPoolServer(TProcessor processor, |
| 69 | TServerTransport serverTransport, |
| 70 | TTransportFactory inputTransportFactory, |
| 71 | TTransportFactory outputTransportFactory, |
| 72 | TProtocolFactory inputProtocolFactory, |
| 73 | TProtocolFactory outputProtocolFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 74 | int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 75 | :base(processor, serverTransport, inputTransportFactory, outputTransportFactory, |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 76 | inputProtocolFactory, outputProtocolFactory, logDel) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 77 | { |
Jens Geyer | 4b0fede | 2014-05-28 22:31:23 +0200 | [diff] [blame] | 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 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 88 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 91 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [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 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 103 | logDelegate("Error, could not listen on ServerTransport: " + ttx); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 107 | while (!stop) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 108 | { |
| 109 | int failureCount = 0; |
| 110 | try |
| 111 | { |
| 112 | TTransport client = serverTransport.Accept(); |
| 113 | ThreadPool.QueueUserWorkItem(this.Execute, client); |
| 114 | } |
| 115 | catch (TTransportException ttx) |
| 116 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 117 | if (stop) |
| 118 | { |
| 119 | logDelegate("TThreadPoolServer was shutting down, caught " + ttx.GetType().Name); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | ++failureCount; |
| 124 | logDelegate(ttx.ToString()); |
| 125 | } |
| 126 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 129 | |
| 130 | if (stop) |
| 131 | { |
| 132 | try |
| 133 | { |
| 134 | serverTransport.Close(); |
| 135 | } |
| 136 | catch (TTransportException ttx) |
| 137 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 138 | logDelegate("TServerTransport failed on close: " + ttx.Message); |
David Reiss | dc815f5 | 2008-03-02 00:58:04 +0000 | [diff] [blame] | 139 | } |
| 140 | stop = false; |
| 141 | } |
| 142 | } |
| 143 | |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [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 | try |
| 157 | { |
| 158 | inputTransport = inputTransportFactory.GetTransport(client); |
| 159 | outputTransport = outputTransportFactory.GetTransport(client); |
| 160 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 161 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 162 | while (processor.Process(inputProtocol, outputProtocol)) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 163 | { |
| 164 | //keep processing requests until client disconnects |
| 165 | } |
| 166 | } |
David Reiss | cee1e08 | 2008-04-02 22:10:09 +0000 | [diff] [blame] | 167 | catch (TTransportException) |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 168 | { |
| 169 | // Assume the client died and continue silently |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 170 | //Console.WriteLine(ttx); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | catch (Exception x) |
| 174 | { |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 175 | logDelegate("Error: " + x); |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | if (inputTransport != null) |
| 179 | { |
| 180 | inputTransport.Close(); |
| 181 | } |
| 182 | if (outputTransport != null) |
| 183 | { |
| 184 | outputTransport.Close(); |
| 185 | } |
| 186 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 187 | |
| 188 | public override void Stop() |
| 189 | { |
| 190 | stop = true; |
| 191 | serverTransport.Close(); |
| 192 | } |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 193 | } |
| 194 | } |