ServiceThrottle.cs 16 KB

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