PollServiceRequestManager.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 BlockingQueue<PollServiceHttpRequest> m_requests = new BlockingQueue<PollServiceHttpRequest>();
  46. private static List<PollServiceHttpRequest> m_longPollRequests = new List<PollServiceHttpRequest>();
  47. private uint m_WorkerThreadCount = 0;
  48. private Thread[] m_workerThreads;
  49. private Thread m_longPollThread;
  50. private bool m_running = true;
  51. private int slowCount = 0;
  52. private SmartThreadPool m_threadPool = new SmartThreadPool(20000, 12, 2);
  53. // private int m_timeout = 1000; // increase timeout 250; now use the event one
  54. public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout)
  55. {
  56. m_server = pSrv;
  57. m_WorkerThreadCount = pWorkerThreadCount;
  58. m_workerThreads = new Thread[m_WorkerThreadCount];
  59. }
  60. public void Start()
  61. {
  62. //startup worker threads
  63. for (uint i = 0; i < m_WorkerThreadCount; i++)
  64. {
  65. m_workerThreads[i]
  66. = Watchdog.StartThread(
  67. PoolWorkerJob,
  68. string.Format("PollServiceWorkerThread{0}:{1}", i, m_server.Port),
  69. ThreadPriority.Normal,
  70. false,
  71. false,
  72. null,
  73. int.MaxValue);
  74. }
  75. m_longPollThread = Watchdog.StartThread(
  76. this.CheckLongPollThreads,
  77. string.Format("LongPollServiceWatcherThread:{0}", m_server.Port),
  78. ThreadPriority.Normal,
  79. false,
  80. true,
  81. null,
  82. 1000 * 60 * 10);
  83. }
  84. private void ReQueueEvent(PollServiceHttpRequest req)
  85. {
  86. if (m_running)
  87. {
  88. // delay the enqueueing for 100ms. There's no need to have the event
  89. // actively on the queue
  90. Timer t = new Timer(self => {
  91. ((Timer)self).Dispose();
  92. m_requests.Enqueue(req);
  93. });
  94. t.Change(100, Timeout.Infinite);
  95. }
  96. }
  97. public void Enqueue(PollServiceHttpRequest req)
  98. {
  99. if (m_running)
  100. {
  101. if (req.PollServiceArgs.Type == PollServiceEventArgs.EventType.LongPoll)
  102. {
  103. lock (m_longPollRequests)
  104. m_longPollRequests.Add(req);
  105. }
  106. else
  107. m_requests.Enqueue(req);
  108. }
  109. }
  110. private void CheckLongPollThreads()
  111. {
  112. // The only purpose of this thread is to check the EQs for events.
  113. // If there are events, that thread will be placed in the "ready-to-serve" queue, m_requests.
  114. // If there are no events, that thread will be back to its "waiting" queue, m_longPollRequests.
  115. // All other types of tasks (Inventory handlers, http-in, etc) don't have the long-poll nature,
  116. // so if they aren't ready to be served by a worker thread (no events), they are placed
  117. // directly back in the "ready-to-serve" queue by the worker thread.
  118. while (m_running)
  119. {
  120. Thread.Sleep(1000);
  121. Watchdog.UpdateThread();
  122. List<PollServiceHttpRequest> not_ready = new List<PollServiceHttpRequest>();
  123. lock (m_longPollRequests)
  124. {
  125. if (m_longPollRequests.Count > 0 && m_running)
  126. {
  127. List<PollServiceHttpRequest> ready = m_longPollRequests.FindAll(req =>
  128. (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id) || // there are events in this EQ
  129. (Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms) // no events, but timeout
  130. );
  131. ready.ForEach(req =>
  132. {
  133. m_requests.Enqueue(req);
  134. m_longPollRequests.Remove(req);
  135. });
  136. }
  137. }
  138. }
  139. }
  140. public void Stop()
  141. {
  142. m_running = false;
  143. // m_timeout = -10000; // cause all to expire
  144. Thread.Sleep(1000); // let the world move
  145. foreach (Thread t in m_workerThreads)
  146. Watchdog.AbortThread(t.ManagedThreadId);
  147. PollServiceHttpRequest wreq;
  148. lock (m_longPollRequests)
  149. {
  150. if (m_longPollRequests.Count > 0 && m_running)
  151. m_longPollRequests.ForEach(req => m_requests.Enqueue(req));
  152. }
  153. while (m_requests.Count() > 0)
  154. {
  155. try
  156. {
  157. wreq = m_requests.Dequeue(0);
  158. wreq.DoHTTPGruntWork(
  159. m_server, wreq.PollServiceArgs.NoEvents(wreq.RequestID, wreq.PollServiceArgs.Id));
  160. }
  161. catch
  162. {
  163. }
  164. }
  165. m_longPollRequests.Clear();
  166. m_requests.Clear();
  167. }
  168. // work threads
  169. private void PoolWorkerJob()
  170. {
  171. while (m_running)
  172. {
  173. PollServiceHttpRequest req = m_requests.Dequeue(5000);
  174. //m_log.WarnFormat("[YYY]: Dequeued {0}", (req == null ? "null" : req.PollServiceArgs.Type.ToString()));
  175. Watchdog.UpdateThread();
  176. if (req != null)
  177. {
  178. try
  179. {
  180. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  181. {
  182. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id);
  183. if (responsedata == null)
  184. continue;
  185. if (req.PollServiceArgs.Type == PollServiceEventArgs.EventType.LongPoll) // This is the event queue
  186. {
  187. try
  188. {
  189. req.DoHTTPGruntWork(m_server, responsedata);
  190. }
  191. catch (ObjectDisposedException) // Browser aborted before we could read body, server closed the stream
  192. {
  193. // Ignore it, no need to reply
  194. }
  195. }
  196. else
  197. {
  198. m_threadPool.QueueWorkItem(x =>
  199. {
  200. try
  201. {
  202. req.DoHTTPGruntWork(m_server, responsedata);
  203. }
  204. catch (ObjectDisposedException) // Browser aborted before we could read body, server closed the stream
  205. {
  206. // Ignore it, no need to reply
  207. }
  208. return null;
  209. }, null);
  210. }
  211. }
  212. else
  213. {
  214. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  215. {
  216. req.DoHTTPGruntWork(
  217. m_server, req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
  218. }
  219. else
  220. {
  221. ReQueueEvent(req);
  222. }
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }