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. |
| 18 | */ |
| 19 | |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 20 | using System; |
| 21 | using System.Collections.Generic; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 22 | using System.Threading; |
David Reiss | d831a21 | 2009-02-13 03:09:52 +0000 | [diff] [blame] | 23 | using Thrift.Collections; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 24 | using Thrift.Protocol; |
| 25 | using Thrift.Transport; |
| 26 | |
| 27 | namespace Thrift.Server |
| 28 | { |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 29 | /// <summary> |
| 30 | /// Server that uses C# threads (as opposed to the ThreadPool) when handling requests |
| 31 | /// </summary> |
| 32 | public class TThreadedServer : TServer |
| 33 | { |
| 34 | private const int DEFAULT_MAX_THREADS = 100; |
| 35 | private volatile bool stop = false; |
| 36 | private readonly int maxThreads; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 37 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 38 | private Queue<TTransport> clientQueue; |
| 39 | private THashSet<Thread> clientThreads; |
| 40 | private object clientLock; |
| 41 | private Thread workerThread; |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 42 | |
Jens Geyer | c7cf379 | 2015-03-07 13:18:02 +0100 | [diff] [blame] | 43 | public int ClientThreadsCount { |
| 44 | get { return clientThreads.Count; } |
| 45 | } |
| 46 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 47 | public TThreadedServer(TProcessor processor, TServerTransport serverTransport) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 48 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 49 | new TTransportFactory(), new TTransportFactory(), |
| 50 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 51 | DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 52 | { |
| 53 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 54 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 55 | public TThreadedServer(TProcessor processor, TServerTransport serverTransport, LogDelegate logDelegate) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 56 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 57 | new TTransportFactory(), new TTransportFactory(), |
| 58 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 59 | DEFAULT_MAX_THREADS, logDelegate) |
| 60 | { |
| 61 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 62 | |
| 63 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 64 | public TThreadedServer(TProcessor processor, |
| 65 | TServerTransport serverTransport, |
| 66 | TTransportFactory transportFactory, |
| 67 | TProtocolFactory protocolFactory) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 68 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 69 | transportFactory, transportFactory, |
| 70 | protocolFactory, protocolFactory, |
| 71 | DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 72 | { |
| 73 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 74 | |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 75 | public TThreadedServer(TProcessorFactory processorFactory, |
| 76 | TServerTransport serverTransport, |
| 77 | TTransportFactory transportFactory, |
| 78 | TProtocolFactory protocolFactory) |
| 79 | : this(processorFactory, serverTransport, |
| 80 | transportFactory, transportFactory, |
| 81 | protocolFactory, protocolFactory, |
| 82 | DEFAULT_MAX_THREADS, DefaultLogDelegate) |
| 83 | { |
| 84 | } |
| 85 | public TThreadedServer(TProcessorFactory processorFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 86 | TServerTransport serverTransport, |
| 87 | TTransportFactory inputTransportFactory, |
| 88 | TTransportFactory outputTransportFactory, |
| 89 | TProtocolFactory inputProtocolFactory, |
| 90 | TProtocolFactory outputProtocolFactory, |
| 91 | int maxThreads, LogDelegate logDel) |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 92 | : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 93 | inputProtocolFactory, outputProtocolFactory, logDel) |
| 94 | { |
| 95 | this.maxThreads = maxThreads; |
| 96 | clientQueue = new Queue<TTransport>(); |
| 97 | clientLock = new object(); |
| 98 | clientThreads = new THashSet<Thread>(); |
| 99 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 100 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 101 | /// <summary> |
| 102 | /// Use new Thread for each new client connection. block until numConnections < maxThreads |
| 103 | /// </summary> |
| 104 | public override void Serve() |
| 105 | { |
| 106 | try |
| 107 | { |
| 108 | //start worker thread |
| 109 | workerThread = new Thread(new ThreadStart(Execute)); |
| 110 | workerThread.Start(); |
| 111 | serverTransport.Listen(); |
| 112 | } |
| 113 | catch (TTransportException ttx) |
| 114 | { |
| 115 | logDelegate("Error, could not listen on ServerTransport: " + ttx); |
| 116 | return; |
| 117 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 118 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 119 | //Fire the preServe server event when server is up but before any client connections |
| 120 | if (serverEventHandler != null) |
| 121 | serverEventHandler.preServe(); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 122 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 123 | while (!stop) |
| 124 | { |
| 125 | int failureCount = 0; |
| 126 | try |
| 127 | { |
| 128 | TTransport client = serverTransport.Accept(); |
| 129 | lock (clientLock) |
| 130 | { |
| 131 | clientQueue.Enqueue(client); |
| 132 | Monitor.Pulse(clientLock); |
| 133 | } |
| 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 | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 142 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +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("TServeTransport failed on close: " + ttx.Message); |
| 155 | } |
| 156 | stop = false; |
| 157 | } |
| 158 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +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() |
| 166 | { |
| 167 | while (!stop) |
| 168 | { |
| 169 | TTransport client; |
| 170 | Thread t; |
| 171 | lock (clientLock) |
| 172 | { |
| 173 | //don't dequeue if too many connections |
| 174 | while (clientThreads.Count >= maxThreads) |
| 175 | { |
| 176 | Monitor.Wait(clientLock); |
| 177 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 178 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 179 | while (clientQueue.Count == 0) |
| 180 | { |
| 181 | Monitor.Wait(clientLock); |
| 182 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 183 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 184 | client = clientQueue.Dequeue(); |
| 185 | t = new Thread(new ParameterizedThreadStart(ClientWorker)); |
| 186 | clientThreads.Add(t); |
| 187 | } |
| 188 | //start processing requests from client on new thread |
| 189 | t.Start(client); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private void ClientWorker(Object context) |
| 194 | { |
| 195 | TTransport client = (TTransport)context; |
Jonathan Heard | 2bfd7df | 2015-10-28 17:34:27 +0000 | [diff] [blame] | 196 | TProcessor processor = processorFactory.GetProcessor(client); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 197 | TTransport inputTransport = null; |
| 198 | TTransport outputTransport = null; |
| 199 | TProtocol inputProtocol = null; |
| 200 | TProtocol outputProtocol = null; |
| 201 | Object connectionContext = null; |
| 202 | try |
| 203 | { |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 204 | using (inputTransport = inputTransportFactory.GetTransport(client)) |
| 205 | { |
| 206 | using (outputTransport = outputTransportFactory.GetTransport(client)) |
| 207 | { |
| 208 | inputProtocol = inputProtocolFactory.GetProtocol(inputTransport); |
| 209 | outputProtocol = outputProtocolFactory.GetProtocol(outputTransport); |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 210 | |
| 211 | //Recover event handler (if any) and fire createContext server event when a client connects |
| 212 | if (serverEventHandler != null) |
| 213 | connectionContext = serverEventHandler.createContext(inputProtocol, outputProtocol); |
| 214 | |
| 215 | //Process client requests until client disconnects |
Jens Geyer | 7d88208 | 2015-01-27 22:08:44 +0100 | [diff] [blame] | 216 | while (!stop) |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 217 | { |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 218 | if (!inputTransport.Peek()) |
Jens Geyer | eb8e5ad | 2014-09-29 21:50:15 +0200 | [diff] [blame] | 219 | break; |
| 220 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 221 | //Fire processContext server event |
| 222 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 223 | //That is to say it may be many minutes between the event firing and the client request |
| 224 | //actually arriving or the client may hang up without ever makeing a request. |
| 225 | if (serverEventHandler != null) |
| 226 | serverEventHandler.processContext(connectionContext, inputTransport); |
| 227 | //Process client request (blocks until transport is readable) |
| 228 | if (!processor.Process(inputProtocol, outputProtocol)) |
| 229 | break; |
Roger Meier | b1ec4cc | 2012-04-11 21:21:41 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 233 | } |
| 234 | catch (TTransportException) |
| 235 | { |
| 236 | //Usually a client disconnect, expected |
| 237 | } |
| 238 | catch (Exception x) |
| 239 | { |
| 240 | //Unexpected |
| 241 | logDelegate("Error: " + x); |
| 242 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 243 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 244 | //Fire deleteContext server event after client disconnects |
| 245 | if (serverEventHandler != null) |
| 246 | serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol); |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 247 | |
Randy Abernethy | 0e86f1f | 2014-07-13 09:50:19 -0700 | [diff] [blame] | 248 | lock (clientLock) |
| 249 | { |
| 250 | clientThreads.Remove(Thread.CurrentThread); |
| 251 | Monitor.Pulse(clientLock); |
| 252 | } |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | public override void Stop() |
| 257 | { |
| 258 | stop = true; |
| 259 | serverTransport.Close(); |
| 260 | //clean up all the threads myself |
| 261 | workerThread.Abort(); |
| 262 | foreach (Thread t in clientThreads) |
| 263 | { |
| 264 | t.Abort(); |
| 265 | } |
| 266 | } |
| 267 | } |
David Reiss | 6319133 | 2009-01-06 19:49:22 +0000 | [diff] [blame] | 268 | } |