TcpConnectionPool.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpConnectionPool.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2002 (C) Lluis Sanchez Gual
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Threading;
  11. using System.IO;
  12. using System.Net.Sockets;
  13. namespace System.Runtime.Remoting.Channels.Tcp
  14. {
  15. // This is a pool of Tcp connections. Connections requested
  16. // by the TCP channel are pooled after their use, and can
  17. // be reused later. Connections are automaticaly closed
  18. // if not used after some time, specified in KeepAliveSeconds.
  19. // The number of allowed open connections can also be specified
  20. // in MaxOpenConnections. The limit is per host.
  21. // If a thread requests a connection and the limit has been
  22. // reached, the thread is suspended until one is released.
  23. internal class TcpConnectionPool
  24. {
  25. // Table of pools. There is a HostConnectionPool
  26. // instance for each host
  27. static Hashtable _pools = new Hashtable();
  28. static int _maxOpenConnections = 50;
  29. static int _keepAliveSeconds = 15;
  30. static Thread _poolThread;
  31. static TcpConnectionPool()
  32. {
  33. // This thread will close unused connections
  34. _poolThread = new Thread (new ThreadStart (ConnectionCollector));
  35. _poolThread.Start();
  36. _poolThread.IsBackground = true;
  37. }
  38. public static void Shutdown ()
  39. {
  40. // FIXME: this should not be needed when background threads work.
  41. if (_poolThread != null)
  42. _poolThread.Abort();
  43. }
  44. public static int MaxOpenConnections
  45. {
  46. get { return _maxOpenConnections; }
  47. set
  48. {
  49. if (value < 1) throw new RemotingException ("MaxOpenConnections must be greater than zero");
  50. _maxOpenConnections = value;
  51. }
  52. }
  53. public static int KeepAliveSeconds
  54. {
  55. get { return _keepAliveSeconds; }
  56. set { _keepAliveSeconds = value; }
  57. }
  58. public static TcpConnection GetConnection (string host, int port)
  59. {
  60. HostConnectionPool hostPool;
  61. lock (_pools)
  62. {
  63. string key = host + ":" + port;
  64. hostPool = (HostConnectionPool) _pools[key];
  65. if (hostPool == null)
  66. {
  67. hostPool = new HostConnectionPool(host, port);
  68. _pools[key] = hostPool;
  69. }
  70. }
  71. return hostPool.GetConnection();
  72. }
  73. private static void ConnectionCollector ()
  74. {
  75. while (true)
  76. {
  77. Thread.Sleep(3000);
  78. lock (_pools)
  79. {
  80. ICollection values = _pools.Values;
  81. foreach (HostConnectionPool pool in values)
  82. pool.PurgeConnections();
  83. }
  84. }
  85. }
  86. }
  87. internal class ReusableTcpClient : TcpClient
  88. {
  89. public ReusableTcpClient (string host, int port): base (host, port)
  90. {
  91. }
  92. public bool IsAlive
  93. {
  94. get
  95. {
  96. // This Poll will return true if there is data pending to
  97. // be read. It prob. means that a client object using this
  98. // connection got an exception and did not finish to read
  99. // the data. It can also mean that the connection has been
  100. // closed in the server. In both cases, the connection cannot
  101. // be reused.
  102. return !Client.Poll (0, SelectMode.SelectRead);
  103. }
  104. }
  105. }
  106. internal class TcpConnection
  107. {
  108. DateTime _controlTime;
  109. Stream _stream;
  110. ReusableTcpClient _client;
  111. HostConnectionPool _pool;
  112. byte[] _buffer;
  113. public TcpConnection (HostConnectionPool pool, ReusableTcpClient client)
  114. {
  115. _pool = pool;
  116. _client = client;
  117. _stream = client.GetStream();
  118. _controlTime = DateTime.Now;
  119. _buffer = new byte[TcpMessageIO.DefaultStreamBufferSize];
  120. }
  121. public Stream Stream
  122. {
  123. get { return _stream; }
  124. }
  125. public DateTime ControlTime
  126. {
  127. get { return _controlTime; }
  128. set { _controlTime = value; }
  129. }
  130. public bool IsAlive
  131. {
  132. get { return _client.IsAlive; }
  133. }
  134. // This is a "thread safe" buffer that can be used by
  135. // TcpClientTransportSink to read or send data to the stream.
  136. // The buffer is "thread safe" since only one thread can
  137. // use a connection at a given time.
  138. public byte[] Buffer
  139. {
  140. get { return _buffer; }
  141. }
  142. // Returns the connection to the pool
  143. public void Release()
  144. {
  145. _pool.ReleaseConnection (this);
  146. }
  147. public void Close()
  148. {
  149. _client.Close();
  150. }
  151. }
  152. internal class HostConnectionPool
  153. {
  154. ArrayList _pool = new ArrayList();
  155. int _activeConnections = 0;
  156. string _host;
  157. int _port;
  158. public HostConnectionPool (string host, int port)
  159. {
  160. _host = host;
  161. _port = port;
  162. }
  163. public TcpConnection GetConnection ()
  164. {
  165. lock (_pool)
  166. {
  167. TcpConnection connection = null;
  168. do
  169. {
  170. if (_pool.Count > 0)
  171. {
  172. // There are available connections
  173. connection = (TcpConnection)_pool[_pool.Count - 1];
  174. _pool.RemoveAt(_pool.Count - 1);
  175. if (!connection.IsAlive) {
  176. CancelConnection (connection);
  177. connection = null;
  178. continue;
  179. }
  180. }
  181. if (connection == null && _activeConnections < TcpConnectionPool.MaxOpenConnections)
  182. {
  183. // No connections available, but the max connections
  184. // has not been reached yet, so a new one can be created
  185. connection = CreateConnection();
  186. }
  187. // No available connections in the pool
  188. // Wait for somewone to release one.
  189. if (connection == null)
  190. {
  191. Monitor.Wait(_pool);
  192. }
  193. }
  194. while (connection == null);
  195. return connection;
  196. }
  197. }
  198. private TcpConnection CreateConnection()
  199. {
  200. try
  201. {
  202. ReusableTcpClient client = new ReusableTcpClient(_host, _port);
  203. TcpConnection entry = new TcpConnection(this, client);
  204. _activeConnections++;
  205. return entry;
  206. }
  207. catch (Exception ex)
  208. {
  209. throw new RemotingException (ex.Message);
  210. }
  211. }
  212. public void ReleaseConnection (TcpConnection entry)
  213. {
  214. lock (_pool)
  215. {
  216. entry.ControlTime = DateTime.Now; // Initialize timeout
  217. _pool.Add (entry);
  218. Monitor.Pulse (_pool);
  219. }
  220. }
  221. private void CancelConnection(TcpConnection entry)
  222. {
  223. try
  224. {
  225. entry.Stream.Close();
  226. _activeConnections--;
  227. }
  228. catch
  229. {
  230. }
  231. }
  232. public void PurgeConnections()
  233. {
  234. lock (_pool)
  235. {
  236. for (int n=0; n < _pool.Count; n++)
  237. {
  238. TcpConnection entry = (TcpConnection)_pool[n];
  239. if ( (DateTime.Now - entry.ControlTime).TotalSeconds > TcpConnectionPool.KeepAliveSeconds)
  240. {
  241. CancelConnection (entry);
  242. _pool.RemoveAt(n);
  243. n--;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. }