PollServiceRequestManager.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 BlockingCollection<PollServiceHttpRequest> m_requests = new BlockingCollection<PollServiceHttpRequest>();
  42. private ConcurrentQueue<PollServiceHttpRequest> m_retryRequests = new ConcurrentQueue<PollServiceHttpRequest>();
  43. private uint m_WorkerThreadCount = 0;
  44. private Thread[] m_workerThreads;
  45. private Thread m_retrysThread;
  46. private bool m_running = false;
  47. public PollServiceRequestManager(
  48. bool performResponsesAsync, uint pWorkerThreadCount, int pTimeout)
  49. {
  50. m_WorkerThreadCount = pWorkerThreadCount;
  51. m_workerThreads = new Thread[m_WorkerThreadCount];
  52. }
  53. public void Start()
  54. {
  55. if(m_running)
  56. return;
  57. m_running = true;
  58. //startup worker threads
  59. for (uint i = 0; i < m_WorkerThreadCount; i++)
  60. {
  61. m_workerThreads[i]
  62. = WorkManager.StartThread(
  63. PoolWorkerJob,
  64. string.Format("PollServiceWorkerThread {0}", i),
  65. ThreadPriority.Normal,
  66. true,
  67. false,
  68. null,
  69. int.MaxValue);
  70. }
  71. m_retrysThread = WorkManager.StartThread(
  72. this.CheckRetries,
  73. string.Format("PollServiceWatcherThread"),
  74. ThreadPriority.Normal,
  75. true,
  76. true,
  77. null,
  78. 1000 * 60 * 10);
  79. }
  80. private void ReQueueEvent(PollServiceHttpRequest req)
  81. {
  82. if (m_running)
  83. m_retryRequests.Enqueue(req);
  84. }
  85. public void Enqueue(PollServiceHttpRequest req)
  86. {
  87. if(m_running)
  88. m_requests.Add(req);
  89. }
  90. private void CheckRetries()
  91. {
  92. PollServiceHttpRequest preq;
  93. while (m_running)
  94. {
  95. Thread.Sleep(100);
  96. Watchdog.UpdateThread();
  97. while (m_running && m_retryRequests.TryDequeue(out preq))
  98. m_requests.Add(preq);
  99. }
  100. }
  101. public void Stop()
  102. {
  103. if(!m_running)
  104. return;
  105. m_running = false;
  106. Thread.Sleep(100); // let the world move
  107. foreach (Thread t in m_workerThreads)
  108. Watchdog.AbortThread(t.ManagedThreadId);
  109. PollServiceHttpRequest req;
  110. try
  111. {
  112. while (m_retryRequests.TryDequeue(out req))
  113. req.DoHTTPstop();
  114. }
  115. catch
  116. {
  117. }
  118. try
  119. {
  120. while(m_requests.TryTake(out req, 0))
  121. req.DoHTTPstop();
  122. }
  123. catch
  124. {
  125. }
  126. m_requests.Dispose();
  127. }
  128. // work threads
  129. private void PoolWorkerJob()
  130. {
  131. PollServiceHttpRequest req;
  132. while (m_running)
  133. {
  134. try
  135. {
  136. req = null;
  137. if (!m_requests.TryTake(out req, 4500) || req == null)
  138. {
  139. Watchdog.UpdateThread();
  140. continue;
  141. }
  142. Watchdog.UpdateThread();
  143. if (!req.Request.Context.CanSend())
  144. {
  145. req.PollServiceArgs.Drop(req.RequestID, req.PollServiceArgs.Id);
  146. continue;
  147. }
  148. if (req.Request.Context.IsSending())
  149. {
  150. ReQueueEvent(req);
  151. continue;
  152. }
  153. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  154. {
  155. try
  156. {
  157. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id);
  158. req.DoHTTPGruntWork(responsedata);
  159. }
  160. catch { }
  161. }
  162. else
  163. {
  164. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  165. {
  166. try
  167. {
  168. req.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
  169. }
  170. catch { }
  171. }
  172. else
  173. {
  174. ReQueueEvent(req);
  175. }
  176. }
  177. }
  178. catch (ThreadAbortException)
  179. {
  180. Thread.ResetAbort();
  181. // Shouldn't set this to 'false', the normal shutdown should cause things to exit
  182. // but robust is still not normal neither is mono
  183. m_running = false;
  184. }
  185. catch (Exception e)
  186. {
  187. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  188. }
  189. }
  190. }
  191. }
  192. }