blob: 7ddabf7df4234045a396a2c48dc06e59ec210f85 [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{
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 Reissdc815f52008-03-02 00:58:04 +000038 private volatile bool stop = false;
David Reiss7f42bcf2008-01-11 20:59:12 +000039
40 public TThreadPoolServer(TProcessor processor, TServerTransport serverTransport)
41 :this(processor, serverTransport,
42 new TTransportFactory(), new TTransportFactory(),
43 new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(),
David Reiss63191332009-01-06 19:49:22 +000044 DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000045 {
46 }
47
David Reiss63191332009-01-06 19:49:22 +000048 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 Reiss7f42bcf2008-01-11 20:59:12 +000057 public TThreadPoolServer(TProcessor processor,
58 TServerTransport serverTransport,
59 TTransportFactory transportFactory,
60 TProtocolFactory protocolFactory)
61 :this(processor, serverTransport,
62 transportFactory, transportFactory,
63 protocolFactory, protocolFactory,
David Reiss63191332009-01-06 19:49:22 +000064 DEFAULT_MIN_THREADS, DEFAULT_MAX_THREADS, DefaultLogDelegate)
David Reiss7f42bcf2008-01-11 20:59:12 +000065 {
66 }
67
68 public TThreadPoolServer(TProcessor processor,
69 TServerTransport serverTransport,
70 TTransportFactory inputTransportFactory,
71 TTransportFactory outputTransportFactory,
72 TProtocolFactory inputProtocolFactory,
73 TProtocolFactory outputProtocolFactory,
David Reiss63191332009-01-06 19:49:22 +000074 int minThreadPoolThreads, int maxThreadPoolThreads, LogDelegate logDel)
David Reiss7f42bcf2008-01-11 20:59:12 +000075 :base(processor, serverTransport, inputTransportFactory, outputTransportFactory,
David Reiss63191332009-01-06 19:49:22 +000076 inputProtocolFactory, outputProtocolFactory, logDel)
David Reiss7f42bcf2008-01-11 20:59:12 +000077 {
Jens Geyer4b0fede2014-05-28 22:31:23 +020078 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 Reiss7f42bcf2008-01-11 20:59:12 +000088 }
David Reiss7f42bcf2008-01-11 20:59:12 +000089 }
90
Roger Meierb1ec4cc2012-04-11 21:21:41 +000091
David Reiss7f42bcf2008-01-11 20:59:12 +000092 /// <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 Reiss63191332009-01-06 19:49:22 +0000103 logDelegate("Error, could not listen on ServerTransport: " + ttx);
David Reiss7f42bcf2008-01-11 20:59:12 +0000104 return;
105 }
106
David Reissdc815f52008-03-02 00:58:04 +0000107 while (!stop)
David Reiss7f42bcf2008-01-11 20:59:12 +0000108 {
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 Reiss63191332009-01-06 19:49:22 +0000117 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 Reiss7f42bcf2008-01-11 20:59:12 +0000127 }
128 }
David Reissdc815f52008-03-02 00:58:04 +0000129
130 if (stop)
131 {
132 try
133 {
134 serverTransport.Close();
135 }
136 catch (TTransportException ttx)
137 {
David Reiss63191332009-01-06 19:49:22 +0000138 logDelegate("TServerTransport failed on close: " + ttx.Message);
David Reissdc815f52008-03-02 00:58:04 +0000139 }
140 stop = false;
141 }
142 }
143
David Reiss7f42bcf2008-01-11 20:59:12 +0000144 /// <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 Reiss0c90f6f2008-02-06 22:18:40 +0000162 while (processor.Process(inputProtocol, outputProtocol))
David Reiss7f42bcf2008-01-11 20:59:12 +0000163 {
164 //keep processing requests until client disconnects
165 }
166 }
David Reisscee1e082008-04-02 22:10:09 +0000167 catch (TTransportException)
David Reiss7f42bcf2008-01-11 20:59:12 +0000168 {
169 // Assume the client died and continue silently
David Reiss63191332009-01-06 19:49:22 +0000170 //Console.WriteLine(ttx);
David Reiss7f42bcf2008-01-11 20:59:12 +0000171 }
172
173 catch (Exception x)
174 {
David Reiss63191332009-01-06 19:49:22 +0000175 logDelegate("Error: " + x);
David Reiss7f42bcf2008-01-11 20:59:12 +0000176 }
177
178 if (inputTransport != null)
179 {
180 inputTransport.Close();
181 }
182 if (outputTransport != null)
183 {
184 outputTransport.Close();
185 }
186 }
David Reiss63191332009-01-06 19:49:22 +0000187
188 public override void Stop()
189 {
190 stop = true;
191 serverTransport.Close();
192 }
David Reiss7f42bcf2008-01-11 20:59:12 +0000193 }
194}