Jens Geyer | 3f1fd59 | 2021-10-28 22:31:12 +0200 | [diff] [blame] | 1 | /* |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 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 | * Contains some contributions under the Thrift Software License. |
| 20 | * Please see doc/old-thrift-license.txt in the Thrift distribution for |
| 21 | * details. |
| 22 | */ |
| 23 | |
| 24 | using System; |
| 25 | using System.Threading; |
| 26 | using Thrift.Protocol; |
| 27 | using Thrift.Transport; |
| 28 | using Thrift.Processor; |
| 29 | using System.Threading.Tasks; |
| 30 | using Microsoft.Extensions.Logging; |
| 31 | |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 32 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 33 | namespace Thrift.Server |
| 34 | { |
| 35 | /// <summary> |
| 36 | /// Server that uses C# built-in ThreadPool to spawn threads when handling requests. |
| 37 | /// </summary> |
| 38 | public class TThreadPoolAsyncServer : TServer |
| 39 | { |
| 40 | private const int DEFAULT_MIN_THREADS = -1; // use .NET ThreadPool defaults |
| 41 | private const int DEFAULT_MAX_THREADS = -1; // use .NET ThreadPool defaults |
| 42 | private volatile bool stop = false; |
| 43 | |
| 44 | private CancellationToken ServerCancellationToken; |
| 45 | |
| 46 | public struct Configuration |
| 47 | { |
| 48 | public int MinWorkerThreads; |
| 49 | public int MaxWorkerThreads; |
| 50 | public int MinIOThreads; |
| 51 | public int MaxIOThreads; |
| 52 | |
| 53 | public Configuration(int min = DEFAULT_MIN_THREADS, int max = DEFAULT_MAX_THREADS) |
| 54 | { |
| 55 | MinWorkerThreads = min; |
| 56 | MaxWorkerThreads = max; |
| 57 | MinIOThreads = min; |
| 58 | MaxIOThreads = max; |
| 59 | } |
| 60 | |
| 61 | public Configuration(int minWork, int maxWork, int minIO, int maxIO) |
| 62 | { |
| 63 | MinWorkerThreads = minWork; |
| 64 | MaxWorkerThreads = maxWork; |
| 65 | MinIOThreads = minIO; |
| 66 | MaxIOThreads = maxIO; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | public TThreadPoolAsyncServer(ITAsyncProcessor processor, TServerTransport serverTransport, ILogger logger = null) |
| 71 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
Kyle Smith | 7b94dd4 | 2019-03-23 17:26:56 +0100 | [diff] [blame] | 72 | null, null, // defaults to TTransportFactory() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 73 | new TBinaryProtocol.Factory(), new TBinaryProtocol.Factory(), |
| 74 | new Configuration(), logger) |
| 75 | { |
| 76 | } |
| 77 | |
| 78 | public TThreadPoolAsyncServer(ITAsyncProcessor processor, |
| 79 | TServerTransport serverTransport, |
| 80 | TTransportFactory transportFactory, |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 81 | TProtocolFactory protocolFactory) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 82 | : this(new TSingletonProcessorFactory(processor), serverTransport, |
| 83 | transportFactory, transportFactory, |
| 84 | protocolFactory, protocolFactory, |
| 85 | new Configuration()) |
| 86 | { |
| 87 | } |
| 88 | |
| 89 | public TThreadPoolAsyncServer(ITProcessorFactory processorFactory, |
| 90 | TServerTransport serverTransport, |
| 91 | TTransportFactory transportFactory, |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 92 | TProtocolFactory protocolFactory) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 93 | : this(processorFactory, serverTransport, |
| 94 | transportFactory, transportFactory, |
| 95 | protocolFactory, protocolFactory, |
| 96 | new Configuration()) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | public TThreadPoolAsyncServer(ITProcessorFactory processorFactory, |
| 101 | TServerTransport serverTransport, |
| 102 | TTransportFactory inputTransportFactory, |
| 103 | TTransportFactory outputTransportFactory, |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 104 | TProtocolFactory inputProtocolFactory, |
| 105 | TProtocolFactory outputProtocolFactory, |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame] | 106 | int minThreadPoolThreads, int maxThreadPoolThreads, ILogger logger = null) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 107 | : this(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
| 108 | inputProtocolFactory, outputProtocolFactory, |
| 109 | new Configuration(minThreadPoolThreads, maxThreadPoolThreads), |
| 110 | logger) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | public TThreadPoolAsyncServer(ITProcessorFactory processorFactory, |
| 115 | TServerTransport serverTransport, |
| 116 | TTransportFactory inputTransportFactory, |
| 117 | TTransportFactory outputTransportFactory, |
Jens Geyer | 421444f | 2019-03-20 22:13:25 +0100 | [diff] [blame] | 118 | TProtocolFactory inputProtocolFactory, |
| 119 | TProtocolFactory outputProtocolFactory, |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 120 | Configuration threadConfig, |
| 121 | ILogger logger = null) |
| 122 | : base(processorFactory, serverTransport, inputTransportFactory, outputTransportFactory, |
| 123 | inputProtocolFactory, outputProtocolFactory, logger) |
| 124 | { |
| 125 | lock (typeof(TThreadPoolAsyncServer)) |
| 126 | { |
| 127 | if ((threadConfig.MaxWorkerThreads > 0) || (threadConfig.MaxIOThreads > 0)) |
| 128 | { |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 129 | ThreadPool.GetMaxThreads(out int work, out int comm); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 130 | if (threadConfig.MaxWorkerThreads > 0) |
| 131 | work = threadConfig.MaxWorkerThreads; |
| 132 | if (threadConfig.MaxIOThreads > 0) |
| 133 | comm = threadConfig.MaxIOThreads; |
| 134 | if (!ThreadPool.SetMaxThreads(work, comm)) |
| 135 | throw new Exception("Error: could not SetMaxThreads in ThreadPool"); |
| 136 | } |
| 137 | |
| 138 | if ((threadConfig.MinWorkerThreads > 0) || (threadConfig.MinIOThreads > 0)) |
| 139 | { |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 140 | ThreadPool.GetMinThreads(out int work, out int comm); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 141 | if (threadConfig.MinWorkerThreads > 0) |
| 142 | work = threadConfig.MinWorkerThreads; |
| 143 | if (threadConfig.MinIOThreads > 0) |
| 144 | comm = threadConfig.MinIOThreads; |
| 145 | if (!ThreadPool.SetMinThreads(work, comm)) |
| 146 | throw new Exception("Error: could not SetMinThreads in ThreadPool"); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /// <summary> |
| 153 | /// Use new ThreadPool thread for each new client connection. |
| 154 | /// </summary> |
| 155 | public override async Task ServeAsync(CancellationToken cancellationToken) |
| 156 | { |
| 157 | ServerCancellationToken = cancellationToken; |
| 158 | try |
| 159 | { |
| 160 | try |
| 161 | { |
| 162 | ServerTransport.Listen(); |
| 163 | } |
| 164 | catch (TTransportException ttx) |
| 165 | { |
| 166 | LogError("Error, could not listen on ServerTransport: " + ttx); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | //Fire the preServe server event when server is up but before any client connections |
| 171 | if (ServerEventHandler != null) |
| 172 | await ServerEventHandler.PreServeAsync(cancellationToken); |
| 173 | |
Jens Geyer | d4e1eb9 | 2021-04-15 16:48:21 +0200 | [diff] [blame] | 174 | while (!(stop || ServerCancellationToken.IsCancellationRequested)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 175 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 176 | try |
| 177 | { |
| 178 | TTransport client = await ServerTransport.AcceptAsync(cancellationToken); |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 179 | _ = Task.Run(async () => await ExecuteAsync(client), cancellationToken); // intentionally ignoring retval |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 180 | } |
Jens Geyer | d4e1eb9 | 2021-04-15 16:48:21 +0200 | [diff] [blame] | 181 | catch (TaskCanceledException) |
| 182 | { |
| 183 | stop = true; |
| 184 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 185 | catch (TTransportException ttx) |
| 186 | { |
| 187 | if (!stop || ttx.Type != TTransportException.ExceptionType.Interrupted) |
| 188 | { |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 189 | LogError(ttx.ToString()); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (stop) |
| 196 | { |
| 197 | try |
| 198 | { |
| 199 | ServerTransport.Close(); |
| 200 | } |
| 201 | catch (TTransportException ttx) |
| 202 | { |
| 203 | LogError("TServerTransport failed on close: " + ttx.Message); |
| 204 | } |
| 205 | stop = false; |
| 206 | } |
| 207 | |
| 208 | } |
| 209 | finally |
| 210 | { |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 211 | ServerCancellationToken = default; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | /// <summary> |
| 216 | /// Loops on processing a client forever |
Jens Geyer | 3f1fd59 | 2021-10-28 22:31:12 +0200 | [diff] [blame] | 217 | /// client will be a TTransport instance |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 218 | /// </summary> |
Jens Geyer | 3f1fd59 | 2021-10-28 22:31:12 +0200 | [diff] [blame] | 219 | /// <param name="client"></param> |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 220 | private async Task ExecuteAsync(TTransport client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 221 | { |
| 222 | var cancellationToken = ServerCancellationToken; |
| 223 | |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 224 | using (client) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 225 | { |
| 226 | ITAsyncProcessor processor = ProcessorFactory.GetAsyncProcessor(client, this); |
| 227 | TTransport inputTransport = null; |
| 228 | TTransport outputTransport = null; |
| 229 | TProtocol inputProtocol = null; |
| 230 | TProtocol outputProtocol = null; |
| 231 | object connectionContext = null; |
| 232 | try |
| 233 | { |
| 234 | try |
| 235 | { |
| 236 | inputTransport = InputTransportFactory.GetTransport(client); |
| 237 | outputTransport = OutputTransportFactory.GetTransport(client); |
| 238 | inputProtocol = InputProtocolFactory.GetProtocol(inputTransport); |
| 239 | outputProtocol = OutputProtocolFactory.GetProtocol(outputTransport); |
| 240 | |
| 241 | //Recover event handler (if any) and fire createContext server event when a client connects |
| 242 | if (ServerEventHandler != null) |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 243 | connectionContext = await ServerEventHandler.CreateContextAsync(inputProtocol, outputProtocol, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 244 | |
| 245 | //Process client requests until client disconnects |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame] | 246 | while (!(stop || cancellationToken.IsCancellationRequested)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 247 | { |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame] | 248 | if (!await inputTransport.PeekAsync(cancellationToken)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 249 | break; |
| 250 | |
| 251 | //Fire processContext server event |
| 252 | //N.B. This is the pattern implemented in C++ and the event fires provisionally. |
| 253 | //That is to say it may be many minutes between the event firing and the client request |
| 254 | //actually arriving or the client may hang up without ever makeing a request. |
| 255 | if (ServerEventHandler != null) |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 256 | await ServerEventHandler.ProcessContextAsync(connectionContext, inputTransport, cancellationToken); |
Jens Geyer | 63d114d | 2021-05-25 23:42:35 +0200 | [diff] [blame] | 257 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 258 | //Process client request (blocks until transport is readable) |
Jens Geyer | ea1e8ff | 2021-11-13 23:21:02 +0100 | [diff] [blame] | 259 | if (!await processor.ProcessAsync(inputProtocol, outputProtocol, cancellationToken)) |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 260 | break; |
| 261 | } |
| 262 | } |
| 263 | catch (TTransportException) |
| 264 | { |
| 265 | //Usually a client disconnect, expected |
| 266 | } |
| 267 | catch (Exception x) |
| 268 | { |
| 269 | //Unexpected |
| 270 | LogError("Error: " + x); |
| 271 | } |
| 272 | |
| 273 | //Fire deleteContext server event after client disconnects |
| 274 | if (ServerEventHandler != null) |
phxnsharp | 9a4802a | 2021-05-21 23:36:30 +0200 | [diff] [blame] | 275 | await ServerEventHandler.DeleteContextAsync(connectionContext, inputProtocol, outputProtocol, cancellationToken); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 276 | |
| 277 | } |
| 278 | finally |
| 279 | { |
| 280 | //Close transports |
| 281 | inputTransport?.Close(); |
| 282 | outputTransport?.Close(); |
| 283 | |
| 284 | // disposable stuff should be disposed |
| 285 | inputProtocol?.Dispose(); |
| 286 | outputProtocol?.Dispose(); |
| 287 | inputTransport?.Dispose(); |
| 288 | outputTransport?.Dispose(); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | public override void Stop() |
| 294 | { |
| 295 | stop = true; |
| 296 | ServerTransport?.Close(); |
| 297 | } |
| 298 | } |
| 299 | } |