| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Dispatcher |
| | | 9 | | { |
| | | 10 | | public sealed class ServiceThrottle |
| | | 11 | | { |
| | | 12 | | internal const int DefaultMaxConcurrentCalls = 16; |
| | | 13 | | internal const int DefaultMaxConcurrentSessions = 100; |
| | 0 | 14 | | internal static int DefaultMaxConcurrentCallsCpuCount = DefaultMaxConcurrentCalls * Environment.ProcessorCount; |
| | 0 | 15 | | internal static int DefaultMaxConcurrentSessionsCpuCount = DefaultMaxConcurrentSessions * Environment.ProcessorC |
| | | 16 | | |
| | | 17 | | private FlowThrottle _calls; |
| | | 18 | | private FlowThrottle _sessions; |
| | | 19 | | private QuotaThrottle _dynamic; |
| | | 20 | | private FlowThrottle _instanceContexts; |
| | | 21 | | |
| | | 22 | | private readonly ServiceHostBase _host; |
| | | 23 | | |
| | 0 | 24 | | internal ServiceThrottle(ServiceHostBase host) |
| | | 25 | | { |
| | 0 | 26 | | if (!((host != null))) |
| | | 27 | | { |
| | | 28 | | Fx.Assert("ServiceThrottle.ServiceThrottle: (host != null)"); |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(host)); |
| | | 30 | | } |
| | 0 | 31 | | _host = host; |
| | 0 | 32 | | MaxConcurrentCalls = DefaultMaxConcurrentCallsCpuCount; |
| | 0 | 33 | | MaxConcurrentSessions = DefaultMaxConcurrentSessionsCpuCount; |
| | | 34 | | |
| | 0 | 35 | | IsActive = true; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | private FlowThrottle Calls |
| | | 39 | | { |
| | | 40 | | get |
| | | 41 | | { |
| | 0 | 42 | | if (_calls == null) |
| | | 43 | | { |
| | 0 | 44 | | lock (ThisLock) |
| | | 45 | | { |
| | 0 | 46 | | if (_calls == null) |
| | | 47 | | { |
| | 0 | 48 | | FlowThrottle callsFt = new FlowThrottle(DefaultMaxConcurrentCallsCpuCount, |
| | 0 | 49 | | MaxConcurrentCallsPropertyName, MaxConcurrentCallsConfigName); |
| | | 50 | | |
| | 0 | 51 | | callsFt.SetRatio(RatioCallsToken); |
| | | 52 | | |
| | 0 | 53 | | _calls = callsFt; |
| | | 54 | | } |
| | 0 | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | return _calls; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | private FlowThrottle Sessions |
| | | 63 | | { |
| | | 64 | | get |
| | | 65 | | { |
| | 0 | 66 | | if (_sessions == null) |
| | | 67 | | { |
| | 0 | 68 | | lock (ThisLock) |
| | | 69 | | { |
| | 0 | 70 | | if (_sessions == null) |
| | | 71 | | { |
| | 0 | 72 | | FlowThrottle sessionsFt = new FlowThrottle(DefaultMaxConcurrentSessionsCpuCount, |
| | 0 | 73 | | MaxConcurrentSessionsPropertyName, MaxConcurrentSessionsConfigName); |
| | | 74 | | |
| | 0 | 75 | | sessionsFt.SetRatio(RatioSessionsToken); |
| | | 76 | | |
| | 0 | 77 | | _sessions = sessionsFt; |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | return _sessions; |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private QuotaThrottle Dynamic |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 0 | 90 | | if (_dynamic == null) |
| | | 91 | | { |
| | 0 | 92 | | lock (ThisLock) |
| | | 93 | | { |
| | 0 | 94 | | if (_dynamic == null) |
| | | 95 | | { |
| | 0 | 96 | | QuotaThrottle dynamicQt = new QuotaThrottle(new object()) |
| | 0 | 97 | | { |
| | 0 | 98 | | Owner = "ServiceHost" |
| | 0 | 99 | | }; |
| | | 100 | | |
| | 0 | 101 | | _dynamic = dynamicQt; |
| | | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | UpdateIsActive(); |
| | 0 | 107 | | return _dynamic; |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | internal int ManualFlowControlLimit |
| | | 112 | | { |
| | 0 | 113 | | get { return Dynamic.Limit; } |
| | 0 | 114 | | set { Dynamic.SetLimit(value); } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private const string MaxConcurrentCallsPropertyName = "MaxConcurrentCalls"; |
| | | 118 | | private const string MaxConcurrentCallsConfigName = "maxConcurrentCalls"; |
| | | 119 | | public int MaxConcurrentCalls |
| | | 120 | | { |
| | 0 | 121 | | get { return Calls.Capacity; } |
| | | 122 | | set |
| | | 123 | | { |
| | 0 | 124 | | ThrowIfClosedOrOpened(MaxConcurrentCallsPropertyName); |
| | 0 | 125 | | Calls.Capacity = value; |
| | 0 | 126 | | UpdateIsActive(); |
| | | 127 | | //if (null != this.servicePerformanceCounters) |
| | | 128 | | //{ |
| | | 129 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.Calls |
| | | 130 | | //} |
| | 0 | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private const string MaxConcurrentSessionsPropertyName = "MaxConcurrentSessions"; |
| | | 135 | | private const string MaxConcurrentSessionsConfigName = "maxConcurrentSessions"; |
| | | 136 | | public int MaxConcurrentSessions |
| | | 137 | | { |
| | 0 | 138 | | get { return Sessions.Capacity; } |
| | | 139 | | set |
| | | 140 | | { |
| | 0 | 141 | | ThrowIfClosedOrOpened(MaxConcurrentSessionsPropertyName); |
| | 0 | 142 | | Sessions.Capacity = value; |
| | 0 | 143 | | UpdateIsActive(); |
| | | 144 | | //if (null != this.servicePerformanceCounters) |
| | | 145 | | //{ |
| | | 146 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.Sessi |
| | | 147 | | //} |
| | 0 | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | private const string MaxConcurrentInstancesPropertyName = "MaxConcurrentInstances"; |
| | | 152 | | private const string MaxConcurrentInstancesConfigName = "maxConcurrentInstances"; |
| | | 153 | | public int MaxConcurrentInstances |
| | | 154 | | { |
| | 0 | 155 | | get { return InstanceContexts.Capacity; } |
| | | 156 | | set |
| | | 157 | | { |
| | 0 | 158 | | ThrowIfClosedOrOpened(MaxConcurrentInstancesPropertyName); |
| | 0 | 159 | | InstanceContexts.Capacity = value; |
| | 0 | 160 | | UpdateIsActive(); |
| | | 161 | | //if (null != this.servicePerformanceCounters) |
| | | 162 | | //{ |
| | | 163 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.Insta |
| | | 164 | | //} |
| | 0 | 165 | | } |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | private FlowThrottle InstanceContexts |
| | | 169 | | { |
| | | 170 | | get |
| | | 171 | | { |
| | 0 | 172 | | if (_instanceContexts == null) |
| | | 173 | | { |
| | 0 | 174 | | lock (ThisLock) |
| | | 175 | | { |
| | 0 | 176 | | if (_instanceContexts == null) |
| | | 177 | | { |
| | 0 | 178 | | FlowThrottle instanceContextsFt = new FlowThrottle(int.MaxValue, |
| | 0 | 179 | | MaxConcurrentInstancesPropertyName, MaxConcurrentIn |
| | 0 | 180 | | instanceContextsFt.SetRatio(RatioInstancesToken); |
| | | 181 | | |
| | | 182 | | //if (this.servicePerformanceCounters != null) |
| | | 183 | | //{ |
| | | 184 | | // InitializeInstancePerfCounterSettings(instanceContextsFt); |
| | | 185 | | //} |
| | | 186 | | |
| | 0 | 187 | | _instanceContexts = instanceContextsFt; |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | return _instanceContexts; |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | internal bool IsActive { get; private set; } |
| | | 197 | | |
| | 0 | 198 | | internal object ThisLock { get; } = new object(); |
| | | 199 | | |
| | | 200 | | //internal void SetServicePerformanceCounters(ServicePerformanceCountersBase counters) |
| | | 201 | | //{ |
| | | 202 | | // this.servicePerformanceCounters = counters; |
| | | 203 | | // //instance throttle is created through the behavior, set the perf counter callbacks if initialized |
| | | 204 | | // if (_instanceContexts != null) |
| | | 205 | | // { |
| | | 206 | | // InitializeInstancePerfCounterSettings(_instanceContexts); |
| | | 207 | | // } |
| | | 208 | | |
| | | 209 | | // //this.calls and this.sessions throttles are created by the constructor. Set the perf counter callbacks |
| | | 210 | | // InitializeCallsPerfCounterSettings(); |
| | | 211 | | // InitializeSessionsPerfCounterSettings(); |
| | | 212 | | //} |
| | | 213 | | |
| | | 214 | | //void InitializeInstancePerfCounterSettings(FlowThrottle instanceContextsFt) |
| | | 215 | | //{ |
| | | 216 | | // Fx.Assert(instanceContextsFt != null, "Expect instanceContext to be initialized"); |
| | | 217 | | // Fx.Assert(this.servicePerformanceCounters != null, "expect servicePerformanceCounters to be set"); |
| | | 218 | | // instanceContextsFt.SetAcquired(AcquiredInstancesToken); |
| | | 219 | | // instanceContextsFt.SetReleased(ReleasedInstancesToken); |
| | | 220 | | // instanceContextsFt.SetRatio(RatioInstancesToken); |
| | | 221 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.InstancesPerc |
| | | 222 | | //} |
| | | 223 | | |
| | | 224 | | //void InitializeCallsPerfCounterSettings() |
| | | 225 | | //{ |
| | | 226 | | // Fx.Assert(_calls != null, "Expect calls to be initialized"); |
| | | 227 | | // Fx.Assert(this.servicePerformanceCounters != null, "expect servicePerformanceCounters to be set"); |
| | | 228 | | // _calls.SetAcquired(AcquiredCallsToken); |
| | | 229 | | // _calls.SetReleased(ReleasedCallsToken); |
| | | 230 | | // _calls.SetRatio(RatioCallsToken); |
| | | 231 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.CallsPercentM |
| | | 232 | | //} |
| | | 233 | | |
| | | 234 | | //void InitializeSessionsPerfCounterSettings() |
| | | 235 | | //{ |
| | | 236 | | // Fx.Assert(_sessions != null, "Expect sessions to be initialized"); |
| | | 237 | | // Fx.Assert(this.servicePerformanceCounters != null, "expect servicePerformanceCounters to be set"); |
| | | 238 | | // _sessions.SetAcquired(AcquiredSessionsToken); |
| | | 239 | | // _sessions.SetReleased(ReleasedSessionsToken); |
| | | 240 | | // _sessions.SetRatio(RatioSessionsToken); |
| | | 241 | | // this.servicePerformanceCounters.SetThrottleBase((int)ServicePerformanceCounters.PerfCounters.SessionsPerce |
| | | 242 | | //} |
| | | 243 | | |
| | | 244 | | private async Task PrivateAcquireCallAsync() |
| | | 245 | | { |
| | 0 | 246 | | if (_calls != null) |
| | | 247 | | { |
| | 0 | 248 | | await _calls.AcquireAsync(); |
| | | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | //bool PrivateAcquireSessionListenerHandler(ListenerHandler listener) |
| | | 253 | | //{ |
| | | 254 | | // if ((_sessions != null) && (listener.Channel != null) && (listener.Channel.Throttle == null)) |
| | | 255 | | // { |
| | | 256 | | // listener.Channel.Throttle = this; |
| | | 257 | | // return _sessions.Acquire(listener); |
| | | 258 | | // } |
| | | 259 | | // else |
| | | 260 | | // { |
| | | 261 | | // return true; |
| | | 262 | | // } |
| | | 263 | | //} |
| | | 264 | | |
| | | 265 | | private async Task PrivateAcquireSessionAsync() |
| | | 266 | | { |
| | 0 | 267 | | if (_sessions != null) |
| | | 268 | | { |
| | 0 | 269 | | await _sessions.AcquireAsync(); |
| | | 270 | | } |
| | 0 | 271 | | } |
| | | 272 | | |
| | | 273 | | private async Task PrivateAcquireDynamicAsync() |
| | | 274 | | { |
| | 0 | 275 | | if (_dynamic != null) |
| | | 276 | | { |
| | 0 | 277 | | await _dynamic.AcquireAsync(); |
| | | 278 | | } |
| | 0 | 279 | | } |
| | | 280 | | |
| | | 281 | | private async Task PrivateAcquireInstanceContextAsync(ChannelHandler channel) |
| | | 282 | | { |
| | 0 | 283 | | if ((_instanceContexts != null) && (channel.InstanceContext == null)) |
| | | 284 | | { |
| | 0 | 285 | | channel.InstanceContextServiceThrottle = this; |
| | 0 | 286 | | await _instanceContexts.AcquireAsync(); |
| | | 287 | | } |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | internal Task AcquireCallAsync() |
| | | 291 | | { |
| | 0 | 292 | | return PrivateAcquireCallAsync(); |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | internal async Task AcquireInstanceContextAndDynamicAsync(ChannelHandler channel, bool acquireInstanceContextThr |
| | | 296 | | { |
| | | 297 | | // TODO: Lock removed. This code looks like it should be safe to execute without the lock. Need to verify. |
| | 0 | 298 | | if (acquireInstanceContextThrottle) |
| | | 299 | | { |
| | 0 | 300 | | await PrivateAcquireInstanceContextAsync(channel); |
| | | 301 | | } |
| | | 302 | | |
| | 0 | 303 | | await PrivateAcquireDynamicAsync(); |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | internal Task AcquireSessionAsync() |
| | | 307 | | { |
| | 0 | 308 | | return PrivateAcquireSessionAsync(); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | internal void DeactivateChannel() |
| | | 312 | | { |
| | 0 | 313 | | if (IsActive) |
| | | 314 | | { |
| | 0 | 315 | | if (_sessions != null) |
| | | 316 | | { |
| | 0 | 317 | | _sessions.Release(); |
| | | 318 | | } |
| | | 319 | | } |
| | 0 | 320 | | } |
| | | 321 | | |
| | | 322 | | internal void DeactivateCall() |
| | | 323 | | { |
| | 0 | 324 | | if (IsActive) |
| | | 325 | | { |
| | 0 | 326 | | if (_calls != null) |
| | | 327 | | { |
| | 0 | 328 | | _calls.Release(); |
| | | 329 | | } |
| | | 330 | | } |
| | 0 | 331 | | } |
| | | 332 | | |
| | | 333 | | internal void DeactivateInstanceContext() |
| | | 334 | | { |
| | 0 | 335 | | if (IsActive) |
| | | 336 | | { |
| | 0 | 337 | | if (_instanceContexts != null) |
| | | 338 | | { |
| | 0 | 339 | | _instanceContexts.Release(); |
| | | 340 | | } |
| | | 341 | | } |
| | 0 | 342 | | } |
| | | 343 | | |
| | | 344 | | internal int IncrementManualFlowControlLimit(int incrementBy) |
| | | 345 | | { |
| | 0 | 346 | | return Dynamic.IncrementLimit(incrementBy); |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | private void ThrowIfClosedOrOpened(string memberName) |
| | | 350 | | { |
| | 0 | 351 | | if (_host.State == CommunicationState.Opened) |
| | | 352 | | { |
| | 0 | 353 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFx |
| | | 354 | | } |
| | | 355 | | else |
| | | 356 | | { |
| | 0 | 357 | | _host.ThrowIfClosedOrOpened(); |
| | | 358 | | } |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | private void UpdateIsActive() |
| | | 362 | | { |
| | 0 | 363 | | IsActive = ((_dynamic != null) || |
| | 0 | 364 | | ((_calls != null) && (_calls.Capacity != int.MaxValue)) || |
| | 0 | 365 | | ((_sessions != null) && (_sessions.Capacity != int.MaxValue)) || |
| | 0 | 366 | | ((_instanceContexts != null) && (_instanceContexts.Capacity != int.MaxValue))); |
| | 0 | 367 | | } |
| | | 368 | | |
| | | 369 | | //internal void AcquiredCallsToken() |
| | | 370 | | //{ |
| | | 371 | | // this.servicePerformanceCounters.IncrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Call |
| | | 372 | | //} |
| | | 373 | | |
| | | 374 | | //internal void ReleasedCallsToken() |
| | | 375 | | //{ |
| | | 376 | | // this.servicePerformanceCounters.DecrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Call |
| | | 377 | | //} |
| | | 378 | | |
| | | 379 | | internal void RatioCallsToken(int count) |
| | | 380 | | { |
| | | 381 | | //if (TD.ConcurrentCallsRatioIsEnabled()) |
| | | 382 | | //{ |
| | | 383 | | // TD.ConcurrentCallsRatio(count, MaxConcurrentCalls); |
| | | 384 | | //} |
| | 0 | 385 | | } |
| | | 386 | | |
| | | 387 | | //internal void AcquiredInstancesToken() |
| | | 388 | | //{ |
| | | 389 | | // this.servicePerformanceCounters.IncrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Inst |
| | | 390 | | //} |
| | | 391 | | |
| | | 392 | | //internal void ReleasedInstancesToken() |
| | | 393 | | //{ |
| | | 394 | | // this.servicePerformanceCounters.DecrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Inst |
| | | 395 | | //} |
| | | 396 | | |
| | | 397 | | internal void RatioInstancesToken(int count) |
| | | 398 | | { |
| | | 399 | | //if (TD.ConcurrentInstancesRatioIsEnabled()) |
| | | 400 | | //{ |
| | | 401 | | // TD.ConcurrentInstancesRatio(count, MaxConcurrentInstances); |
| | | 402 | | //} |
| | 0 | 403 | | } |
| | | 404 | | |
| | | 405 | | //internal void AcquiredSessionsToken() |
| | | 406 | | //{ |
| | | 407 | | // this.servicePerformanceCounters.IncrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Sess |
| | | 408 | | //} |
| | | 409 | | |
| | | 410 | | //internal void ReleasedSessionsToken() |
| | | 411 | | //{ |
| | | 412 | | // this.servicePerformanceCounters.DecrementThrottlePercent((int)ServicePerformanceCounters.PerfCounters.Sess |
| | | 413 | | //} |
| | | 414 | | |
| | | 415 | | internal void RatioSessionsToken(int count) |
| | | 416 | | { |
| | | 417 | | //if (TD.ConcurrentSessionsRatioIsEnabled()) |
| | | 418 | | //{ |
| | | 419 | | // TD.ConcurrentSessionsRatio(count, MaxConcurrentSessions); |
| | | 420 | | //} |
| | 0 | 421 | | } |
| | | 422 | | } |
| | | 423 | | } |