PollServiceRequestManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Threading;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenSim.Framework.Monitoring;
  33. using Amib.Threading;
  34. using System.Collections.Generic;
  35. using System.Collections.Concurrent;
  36. namespace OpenSim.Framework.Servers.HttpServer
  37. {
  38. public class PollServiceRequestManager
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private Dictionary<int, Queue<PollServiceHttpRequest>> m_bycontext;
  42. private BlockingCollection<PollServiceHttpRequest> m_requests = new BlockingCollection<PollServiceHttpRequest>();
  43. private ConcurrentQueue<PollServiceHttpRequest> m_retryRequests = new ConcurrentQueue<PollServiceHttpRequest>();
  44. private uint m_WorkerThreadCount = 0;
  45. private Thread[] m_workerThreads;
  46. private Thread m_retrysThread;
  47. private bool m_running = false;
  48. //private SmartThreadPool m_threadPool;
  49. public PollServiceRequestManager(
  50. bool performResponsesAsync, uint pWorkerThreadCount, int pTimeout)
  51. {
  52. m_WorkerThreadCount = pWorkerThreadCount;
  53. m_workerThreads = new Thread[m_WorkerThreadCount];
  54. m_bycontext = new Dictionary<int, Queue<PollServiceHttpRequest>>(256);
  55. /*
  56. STPStartInfo startInfo = new STPStartInfo();
  57. startInfo.IdleTimeout = 30000;
  58. startInfo.MaxWorkerThreads = 20;
  59. startInfo.MinWorkerThreads = 1;
  60. startInfo.ThreadPriority = ThreadPriority.Normal;
  61. startInfo.StartSuspended = true;
  62. startInfo.ThreadPoolName = "PoolService";
  63. m_threadPool = new SmartThreadPool(startInfo);
  64. */
  65. }
  66. public void Start()
  67. {
  68. if(m_running)
  69. return;
  70. m_running = true;
  71. //m_threadPool.Start();
  72. //startup worker threads
  73. for (uint i = 0; i < m_WorkerThreadCount; i++)
  74. {
  75. m_workerThreads[i]
  76. = WorkManager.StartThread(
  77. PoolWorkerJob,
  78. string.Format("PollServiceWorkerThread {0}", i),
  79. ThreadPriority.Normal,
  80. true,
  81. false,
  82. null,
  83. int.MaxValue);
  84. }
  85. m_retrysThread = WorkManager.StartThread(
  86. this.CheckRetries,
  87. string.Format("PollServiceWatcherThread"),
  88. ThreadPriority.Normal,
  89. true,
  90. true,
  91. null,
  92. 1000 * 60 * 10);
  93. }
  94. private void ReQueueEvent(PollServiceHttpRequest req)
  95. {
  96. if (m_running)
  97. m_retryRequests.Enqueue(req);
  98. }
  99. public void Enqueue(PollServiceHttpRequest req)
  100. {
  101. Queue<PollServiceHttpRequest> ctxQeueue;
  102. int rhash = req.contextHash;
  103. lock (m_bycontext)
  104. {
  105. if (m_bycontext.TryGetValue(rhash, out ctxQeueue))
  106. {
  107. ctxQeueue.Enqueue(req);
  108. }
  109. else
  110. {
  111. ctxQeueue = new Queue<PollServiceHttpRequest>();
  112. m_bycontext[rhash] = ctxQeueue;
  113. EnqueueInt(req);
  114. }
  115. }
  116. }
  117. public void byContextDequeue(PollServiceHttpRequest req)
  118. {
  119. Queue<PollServiceHttpRequest> ctxQeueue;
  120. int rhash = req.contextHash;
  121. lock (m_bycontext)
  122. {
  123. if (m_bycontext.TryGetValue(rhash, out ctxQeueue))
  124. {
  125. if (ctxQeueue.Count > 0)
  126. {
  127. PollServiceHttpRequest newreq = ctxQeueue.Dequeue();
  128. EnqueueInt(newreq);
  129. }
  130. else
  131. {
  132. m_bycontext.Remove(rhash);
  133. }
  134. }
  135. }
  136. }
  137. public void DropByContext(PollServiceHttpRequest req)
  138. {
  139. Queue<PollServiceHttpRequest> ctxQeueue;
  140. int rhash = req.contextHash;
  141. lock (m_bycontext)
  142. {
  143. if (m_bycontext.TryGetValue(rhash, out ctxQeueue))
  144. {
  145. ctxQeueue.Clear();
  146. m_bycontext.Remove(rhash);
  147. }
  148. }
  149. }
  150. public void EnqueueInt(PollServiceHttpRequest req)
  151. {
  152. if (m_running)
  153. m_requests.Add(req);
  154. }
  155. private void CheckRetries()
  156. {
  157. PollServiceHttpRequest preq;
  158. while (m_running)
  159. {
  160. Thread.Sleep(100);
  161. Watchdog.UpdateThread();
  162. while (m_running && m_retryRequests.TryDequeue(out preq))
  163. m_requests.Add(preq);
  164. }
  165. }
  166. public void Stop()
  167. {
  168. if(!m_running)
  169. return;
  170. m_running = false;
  171. Thread.Sleep(100); // let the world move
  172. foreach (Thread t in m_workerThreads)
  173. Watchdog.AbortThread(t.ManagedThreadId);
  174. //m_threadPool.Shutdown();
  175. // any entry in m_bycontext should have a active request on the other queues
  176. // so just delete contents to easy GC
  177. foreach (Queue<PollServiceHttpRequest> qu in m_bycontext.Values)
  178. qu.Clear();
  179. m_bycontext.Clear();
  180. PollServiceHttpRequest req;
  181. try
  182. {
  183. while(m_retryRequests.TryDequeue(out req))
  184. req.DoHTTPstop();
  185. }
  186. catch
  187. {
  188. }
  189. try
  190. {
  191. while(m_requests.TryTake(out req, 0))
  192. req.DoHTTPstop();
  193. }
  194. catch
  195. {
  196. }
  197. m_requests.Dispose();
  198. }
  199. // work threads
  200. private void PoolWorkerJob()
  201. {
  202. PollServiceHttpRequest req;
  203. while (m_running)
  204. {
  205. try
  206. {
  207. req = null;
  208. if (!m_requests.TryTake(out req, 4500) || req == null)
  209. {
  210. Watchdog.UpdateThread();
  211. continue;
  212. }
  213. Watchdog.UpdateThread();
  214. if (!req.HttpContext.CanSend())
  215. {
  216. req.PollServiceArgs.Drop(req.RequestID, req.PollServiceArgs.Id);
  217. byContextDequeue(req);
  218. continue;
  219. }
  220. if (req.HttpContext.IsSending())
  221. {
  222. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  223. {
  224. req.PollServiceArgs.Drop(req.RequestID, req.PollServiceArgs.Id);
  225. byContextDequeue(req);
  226. }
  227. else
  228. ReQueueEvent(req);
  229. continue;
  230. }
  231. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  232. {
  233. PollServiceHttpRequest nreq = req;
  234. //m_threadPool.QueueWorkItem(x =>
  235. //{
  236. try
  237. {
  238. Hashtable responsedata = nreq.PollServiceArgs.GetEvents(nreq.RequestID, nreq.PollServiceArgs.Id);
  239. nreq.DoHTTPGruntWork(responsedata);
  240. }
  241. catch (ObjectDisposedException) { }
  242. finally
  243. {
  244. byContextDequeue(nreq);
  245. nreq = null;
  246. }
  247. //return null;
  248. //}, null);
  249. }
  250. else
  251. {
  252. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  253. {
  254. PollServiceHttpRequest nreq = req;
  255. //m_threadPool.QueueWorkItem(x =>
  256. //{
  257. try
  258. {
  259. nreq.DoHTTPGruntWork(nreq.PollServiceArgs.NoEvents(nreq.RequestID, nreq.PollServiceArgs.Id));
  260. }
  261. catch (ObjectDisposedException) { }
  262. finally
  263. {
  264. byContextDequeue(nreq);
  265. nreq = null;
  266. }
  267. //return null;
  268. //}, null);
  269. }
  270. else
  271. {
  272. ReQueueEvent(req);
  273. }
  274. }
  275. }
  276. catch (ThreadAbortException)
  277. {
  278. Thread.ResetAbort();
  279. // Shouldn't set this to 'false', the normal shutdown should cause things to exit
  280. // but robust is still not normal neither is mono
  281. m_running = false;
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  286. }
  287. }
  288. }
  289. }
  290. }