PollServiceRequestManager.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 HttpServer;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Monitoring;
  35. using Amib.Threading;
  36. using System.IO;
  37. using System.Text;
  38. using System.Collections.Generic;
  39. namespace OpenSim.Framework.Servers.HttpServer
  40. {
  41. public class PollServiceRequestManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private readonly BaseHttpServer m_server;
  45. private Dictionary<PollServiceHttpRequest, Queue<PollServiceHttpRequest>> m_bycontext;
  46. private BlockingQueue<PollServiceHttpRequest> m_requests = new BlockingQueue<PollServiceHttpRequest>();
  47. private static Queue<PollServiceHttpRequest> m_slowRequests = new Queue<PollServiceHttpRequest>();
  48. private static Queue<PollServiceHttpRequest> m_retryRequests = new Queue<PollServiceHttpRequest>();
  49. private uint m_WorkerThreadCount = 0;
  50. private Thread[] m_workerThreads;
  51. private Thread m_retrysThread;
  52. private bool m_running = false;
  53. private int slowCount = 0;
  54. private SmartThreadPool m_threadPool;
  55. public PollServiceRequestManager(
  56. BaseHttpServer pSrv, bool performResponsesAsync, uint pWorkerThreadCount, int pTimeout)
  57. {
  58. m_server = pSrv;
  59. m_WorkerThreadCount = pWorkerThreadCount;
  60. m_workerThreads = new Thread[m_WorkerThreadCount];
  61. PollServiceHttpRequestComparer preqCp = new PollServiceHttpRequestComparer();
  62. m_bycontext = new Dictionary<PollServiceHttpRequest, Queue<PollServiceHttpRequest>>(preqCp);
  63. STPStartInfo startInfo = new STPStartInfo();
  64. startInfo.IdleTimeout = 30000;
  65. startInfo.MaxWorkerThreads = 15;
  66. startInfo.MinWorkerThreads = 1;
  67. startInfo.ThreadPriority = ThreadPriority.Normal;
  68. startInfo.StartSuspended = true;
  69. startInfo.ThreadPoolName = "PoolService";
  70. m_threadPool = new SmartThreadPool(startInfo);
  71. }
  72. public void Start()
  73. {
  74. m_running = true;
  75. m_threadPool.Start();
  76. //startup worker threads
  77. for (uint i = 0; i < m_WorkerThreadCount; i++)
  78. {
  79. m_workerThreads[i]
  80. = WorkManager.StartThread(
  81. PoolWorkerJob,
  82. string.Format("PollServiceWorkerThread {0}:{1}", i, m_server.Port),
  83. ThreadPriority.Normal,
  84. false,
  85. false,
  86. null,
  87. int.MaxValue);
  88. }
  89. m_retrysThread = WorkManager.StartThread(
  90. this.CheckRetries,
  91. string.Format("PollServiceWatcherThread:{0}", m_server.Port),
  92. ThreadPriority.Normal,
  93. false,
  94. true,
  95. null,
  96. 1000 * 60 * 10);
  97. }
  98. private void ReQueueEvent(PollServiceHttpRequest req)
  99. {
  100. if (m_running)
  101. {
  102. lock (m_retryRequests)
  103. m_retryRequests.Enqueue(req);
  104. }
  105. }
  106. public void Enqueue(PollServiceHttpRequest req)
  107. {
  108. lock (m_bycontext)
  109. {
  110. Queue<PollServiceHttpRequest> ctxQeueue;
  111. if (m_bycontext.TryGetValue(req, out ctxQeueue))
  112. {
  113. ctxQeueue.Enqueue(req);
  114. }
  115. else
  116. {
  117. ctxQeueue = new Queue<PollServiceHttpRequest>();
  118. m_bycontext[req] = ctxQeueue;
  119. EnqueueInt(req);
  120. }
  121. }
  122. }
  123. public void byContextDequeue(PollServiceHttpRequest req)
  124. {
  125. Queue<PollServiceHttpRequest> ctxQeueue;
  126. lock (m_bycontext)
  127. {
  128. if (m_bycontext.TryGetValue(req, out ctxQeueue))
  129. {
  130. if (ctxQeueue.Count > 0)
  131. {
  132. PollServiceHttpRequest newreq = ctxQeueue.Dequeue();
  133. EnqueueInt(newreq);
  134. }
  135. else
  136. {
  137. m_bycontext.Remove(req);
  138. }
  139. }
  140. }
  141. }
  142. public void EnqueueInt(PollServiceHttpRequest req)
  143. {
  144. if (m_running)
  145. {
  146. if (req.PollServiceArgs.Type != PollServiceEventArgs.EventType.LongPoll)
  147. {
  148. m_requests.Enqueue(req);
  149. }
  150. else
  151. {
  152. lock (m_slowRequests)
  153. m_slowRequests.Enqueue(req);
  154. }
  155. }
  156. }
  157. private void CheckRetries()
  158. {
  159. while (m_running)
  160. {
  161. Thread.Sleep(100); // let the world move .. back to faster rate
  162. Watchdog.UpdateThread();
  163. lock (m_retryRequests)
  164. {
  165. while (m_retryRequests.Count > 0 && m_running)
  166. m_requests.Enqueue(m_retryRequests.Dequeue());
  167. }
  168. slowCount++;
  169. if (slowCount >= 10)
  170. {
  171. slowCount = 0;
  172. lock (m_slowRequests)
  173. {
  174. while (m_slowRequests.Count > 0 && m_running)
  175. m_requests.Enqueue(m_slowRequests.Dequeue());
  176. }
  177. }
  178. }
  179. }
  180. public void Stop()
  181. {
  182. m_running = false;
  183. Thread.Sleep(1000); // let the world move
  184. foreach (Thread t in m_workerThreads)
  185. Watchdog.AbortThread(t.ManagedThreadId);
  186. // any entry in m_bycontext should have a active request on the other queues
  187. // so just delete contents to easy GC
  188. foreach (Queue<PollServiceHttpRequest> qu in m_bycontext.Values)
  189. qu.Clear();
  190. m_bycontext.Clear();
  191. try
  192. {
  193. foreach (PollServiceHttpRequest req in m_retryRequests)
  194. {
  195. req.DoHTTPstop(m_server);
  196. }
  197. }
  198. catch
  199. {
  200. }
  201. PollServiceHttpRequest wreq;
  202. m_retryRequests.Clear();
  203. lock (m_slowRequests)
  204. {
  205. while (m_slowRequests.Count > 0)
  206. m_requests.Enqueue(m_slowRequests.Dequeue());
  207. }
  208. while (m_requests.Count() > 0)
  209. {
  210. try
  211. {
  212. wreq = m_requests.Dequeue(0);
  213. wreq.DoHTTPstop(m_server);
  214. }
  215. catch
  216. {
  217. }
  218. }
  219. m_requests.Clear();
  220. }
  221. // work threads
  222. private void PoolWorkerJob()
  223. {
  224. while (m_running)
  225. {
  226. PollServiceHttpRequest req = m_requests.Dequeue(5000);
  227. Watchdog.UpdateThread();
  228. if (req != null)
  229. {
  230. try
  231. {
  232. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  233. {
  234. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id);
  235. if (req.PollServiceArgs.Type == PollServiceEventArgs.EventType.LongPoll) // This is the event queue
  236. {
  237. try
  238. {
  239. req.DoHTTPGruntWork(m_server, responsedata);
  240. byContextDequeue(req);
  241. }
  242. catch (ObjectDisposedException) // Browser aborted before we could read body, server closed the stream
  243. {
  244. // Ignore it, no need to reply
  245. }
  246. }
  247. else
  248. {
  249. m_threadPool.QueueWorkItem(x =>
  250. {
  251. try
  252. {
  253. req.DoHTTPGruntWork(m_server, responsedata);
  254. byContextDequeue(req);
  255. }
  256. catch (ObjectDisposedException) // Browser aborted before we could read body, server closed the stream
  257. {
  258. // Ignore it, no need to reply
  259. }
  260. return null;
  261. }, null);
  262. }
  263. }
  264. else
  265. {
  266. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  267. {
  268. req.DoHTTPGruntWork(m_server,
  269. req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
  270. byContextDequeue(req);
  271. }
  272. else
  273. {
  274. ReQueueEvent(req);
  275. }
  276. }
  277. }
  278. catch (Exception e)
  279. {
  280. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }