/Source/WCFChannelManager/AutoSizePool.cs

https://bitbucket.org/lombardo/nuaguil.net · C# · 168 lines · 135 code · 20 blank · 13 comment · 8 complexity · 0f7eb230e413c7e86e6c7fc4aa2b4309 MD5 · raw file

  1. //Copyright 2009 Benny Michielsen
  2. //Licensed under the Apache License, Version 2.0 (the "License");
  3. //you may not use this file except in compliance with the License.
  4. //You may obtain a copy of the License at
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //Unless required by applicable law or agreed to in writing, software
  7. //distributed under the License is distributed on an "AS IS" BASIS,
  8. //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. //See the License for the specific language governing permissions and
  10. //limitations under the License.
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using Spring.Pool;
  16. namespace WCFChannelManager
  17. {
  18. /// <summary>
  19. /// A pool that automatically grows if more instances are requested than there are available in the queue.
  20. /// </summary>
  21. public class AutoSizePool
  22. : IObjectPool
  23. {
  24. protected List<object> activeObjects;
  25. protected List<object> passiveObjects;
  26. protected bool closed;
  27. private object sync;
  28. public AutoSizePool(IPoolableObjectFactory factory)
  29. {
  30. activeObjects = new List<object>();
  31. passiveObjects = new List<object>();
  32. sync = new object();
  33. PoolableObjectFactory = factory;
  34. }
  35. #region IObjectPool Members
  36. public void AddObject()
  37. {
  38. lock (sync)
  39. {
  40. passiveObjects.Add(PoolableObjectFactory.MakeObject());
  41. }
  42. }
  43. public object BorrowObject()
  44. {
  45. lock (sync)
  46. {
  47. if (closed)
  48. {
  49. throw new InvalidOperationException("The pool is closed.");
  50. }
  51. object useable = null;
  52. IEnumerator<object> objects = passiveObjects.GetEnumerator();
  53. List<object> destroyedObjects = new List<object>();
  54. while (objects.MoveNext() && useable == null)
  55. {
  56. PoolableObjectFactory.ActivateObject(objects.Current);
  57. if (PoolableObjectFactory.ValidateObject(objects.Current))
  58. {
  59. useable = objects.Current;
  60. }
  61. else
  62. {
  63. PoolableObjectFactory.DestroyObject(objects.Current);
  64. destroyedObjects.Add(objects.Current);
  65. }
  66. }
  67. passiveObjects.RemoveAll(o => destroyedObjects.Contains(o));
  68. if (useable == null)
  69. {
  70. useable = PoolableObjectFactory.MakeObject();
  71. PoolableObjectFactory.ActivateObject(useable);
  72. }
  73. activeObjects.Add(useable);
  74. return useable;
  75. }
  76. }
  77. public void Clear()
  78. {
  79. lock (sync)
  80. {
  81. RemoveAllPassiveObjects();
  82. }
  83. }
  84. public void Close()
  85. {
  86. lock (sync)
  87. {
  88. closed = true;
  89. RemoveAllPassiveObjects();
  90. RemoveAllActiveObjects();
  91. }
  92. }
  93. public int NumActive
  94. {
  95. get
  96. {
  97. lock (sync)
  98. {
  99. return activeObjects.Count;
  100. }
  101. }
  102. }
  103. public int NumIdle
  104. {
  105. get
  106. {
  107. lock (sync)
  108. {
  109. return passiveObjects.Count;
  110. }
  111. }
  112. }
  113. public IPoolableObjectFactory PoolableObjectFactory
  114. {
  115. get;
  116. set;
  117. }
  118. public void ReturnObject(object target)
  119. {
  120. lock (sync)
  121. {
  122. if (activeObjects.Contains(target))
  123. {
  124. activeObjects.Remove(target);
  125. PoolableObjectFactory.PassivateObject(target);
  126. passiveObjects.Add(target);
  127. }
  128. }
  129. }
  130. #endregion
  131. private void RemoveAllPassiveObjects()
  132. {
  133. ClearList(passiveObjects);
  134. }
  135. private void RemoveAllActiveObjects()
  136. {
  137. ClearList(activeObjects);
  138. }
  139. private void ClearList(List<object> list)
  140. {
  141. foreach (object o in list)
  142. {
  143. PoolableObjectFactory.DestroyObject(o);
  144. }
  145. list.Clear();
  146. }
  147. }
  148. }