blob: f0c7fe468f64ed42c74683a51c358327fa05d86c [file] [log] [blame]
Kevin Clarkab4460d2009-03-20 02:28:41 +00001/**
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 Lipcon53ae9f32009-12-07 00:42:38 +000018 *
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 Clarkab4460d2009-03-20 02:28:41 +000022 */
23
David Reiss7f42bcf2008-01-11 20:59:12 +000024using System;
David Reiss7f42bcf2008-01-11 20:59:12 +000025using System.Threading;
26using Thrift.Protocol;
27using Thrift.Transport;
28
29namespace Thrift.Server
30{
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070031 /// <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 Reiss7f42bcf2008-01-11 20:59:12 +000039
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070040 public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000041 : this(new TSingletonProcessorFactory(processor), serverTransport,
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070042 new TTransportFactory(), new TTransportFactory(),
43 new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
44 DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
45 {
46 }
David Reiss7f42bcf2008-01-11 20:59:12 +000047
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070048 public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000049 : this(new TSingletonProcessorFactory(processor), serverTransport,
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070050 new TTransportFactory(), new TTransportFactory(),
51 new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
52 DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, logDelegate)
53 {
54 }
David Reiss63191332009-01-06 19:49:22 +000055
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070056 public TThreadPoolServer(TProcessor processor,
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000057 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 Abernethy0e86f1f2014-07-13 09:50:19 -070068 TServerTransport serverTransport,
69 TTransportFactory transportFactory,
70 TProtocolFactory protocolFactory)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000071 : this(processorFactory, serverTransport,
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070072 transportFactory, transportFactory,
73 protocolFactory, protocolFactory,
74 DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
75 {
76 }
David Reiss7f42bcf2008-01-11 20:59:12 +000077
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000078 public TThreadPoolServer(TProcessorFactory processorFactory,
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070079 TServerTransport serverTransport,
80 TTransportFactory inputTransportFactory,
81 TTransportFactory outputTransportFactory,
82 TProtocolFactory inputProtocolFactory,
83 TProtocolFactory outputProtocolFactory,
84 int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel)
Jonathan Heard2bfd7df2015-10-28 17:34:27 +000085 : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory,
Randy Abernethy0e86f1f2014-07-13 09:50:19 -070086 inputProtocolFactory, outputProtocolFactory, logDel)
87 {
88 lock (typeof(TThreadPoolServer))
89 {
90 if (!ThreadPool.SetMaxThreads(maxThreadPoolThreads, maxThreadPoolThreads))
91 {
92 throw new Exception("Error: could not SetMaxThreads in ThreadPool");
93 }
94 if (!ThreadPool.SetMinThreads(minThreadPoolThreads, minThreadPoolThreads))
95 {
96 throw new Exception("Error: could not SetMinThreads in ThreadPool");
97 }
98 }
99 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000100
Roger Meierb1ec4cc2012-04-11 21:21:41 +0000101
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700102 /// <summary>
103 /// Use new ThreadPool thread for each new client connection
104 /// </summary>
105 public override void Serve()
106 {
107 try
108 {
109 serverTransport.Listen();
110 }
111 catch (TTransportException ttx)
112 {
113 logDelegate("Error, could not listen on ServerTransport: " + ttx);
114 return;
115 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000116
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700117 //Fire the preServe server event when server is up but before any client connections
118 if (serverEventHandler != null)
119 serverEventHandler.preServe();
David Reiss63191332009-01-06 19:49:22 +0000120
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700121 while (!stop)
122 {
123 int failureCount = 0;
124 try
125 {
126 TTransport client = serverTransport.Accept();
127 ThreadPool.QueueUserWorkItem(this.Execute, client);
128 }
129 catch (TTransportException ttx)
130 {
Jens Geyer7d882082015-01-27 22:08:44 +0100131 if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted)
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700132 {
133 ++failureCount;
134 logDelegate(ttx.ToString());
135 }
David Reissdc815f52008-03-02 00:58:04 +0000136
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700137 }
138 }
David Reissdc815f52008-03-02 00:58:04 +0000139
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700140 if (stop)
141 {
142 try
143 {
144 serverTransport.Close();
145 }
146 catch (TTransportException ttx)
147 {
148 logDelegate("TServerTransport failed on close: " + ttx.Message);
149 }
150 stop = false;
151 }
152 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000153
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700154 /// <summary>
155 /// Loops on processing a client forever
156 /// threadContext will be a TTransport instance
157 /// </summary>
158 /// <param name="threadContext"></param>
159 private void Execute(Object threadContext)
160 {
161 TTransport client = (TTransport)threadContext;
Jonathan Heard2bfd7df2015-10-28 17:34:27 +0000162 TProcessor processor = processorFactory.GetProcessor(client, this);
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700163 TTransport inputTransport = null;
164 TTransport outputTransport = null;
165 TProtocol inputProtocol = null;
166 TProtocol outputProtocol = null;
167 Object connectionContext = null;
168 try
169 {
170 inputTransport = inputTransportFactory.GetTransport(client);
171 outputTransport = outputTransportFactory.GetTransport(client);
172 inputProtocol = inputProtocolFactory.GetProtocol(inputTransport);
173 outputProtocol = outputProtocolFactory.GetProtocol(outputTransport);
David Reiss63191332009-01-06 19:49:22 +0000174
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700175 //Recover event handler (if any) and fire createContext server event when a client connects
176 if (serverEventHandler != null)
177 connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol);
178
179 //Process client requests until client disconnects
Jens Geyer7d882082015-01-27 22:08:44 +0100180 while (!stop)
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700181 {
Jens Geyerd5436f52014-10-03 19:50:38 +0200182 if (!inputTransport.Peek())
Jens Geyereb8e5ad2014-09-29 21:50:15 +0200183 break;
184
Randy Abernethy0e86f1f2014-07-13 09:50:19 -0700185 //Fire processContext server event
186 //N.B. This is the pattern implemented in C++ and the event fires provisionally.
187 //That is to say it may be many minutes between the event firing and the client request
188 //actually arriving or the client may hang up without ever makeing a request.
189 if (serverEventHandler != null)
190 serverEventHandler.processContext(connectionContext, inputTransport);
191 //Process client request (blocks until transport is readable)
192 if (!processor.Process(inputProtocol, outputProtocol))
193 break;
194 }
195 }
196 catch (TTransportException)
197 {
198 //Usually a client disconnect, expected
199 }
200 catch (Exception x)
201 {
202 //Unexpected
203 logDelegate("Error: " + x);
204 }
205
206 //Fire deleteContext server event after client disconnects
207 if (serverEventHandler != null)
208 serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
209
210 //Close transports
211 if (inputTransport != null)
212 inputTransport.Close();
213 if (outputTransport != null)
214 outputTransport.Close();
215 }
216
217 public override void Stop()
218 {
219 stop = true;
220 serverTransport.Close();
221 }
222 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000223}