1
0

PollServiceRequestManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ConcurrentQueue<PollServiceHttpRequest> m_retryRequests = new ConcurrentQueue<PollServiceHttpRequest>();
  42. private int m_WorkerThreadCount = 0;
  43. private ObjectJobEngine m_workerPool;
  44. private Thread m_retrysThread;
  45. private bool m_running = false;
  46. public PollServiceRequestManager(
  47. bool performResponsesAsync, uint pWorkerThreadCount, int pTimeout)
  48. {
  49. m_WorkerThreadCount = (int)pWorkerThreadCount;
  50. }
  51. public void Start()
  52. {
  53. if(m_running)
  54. return;
  55. m_running = true;
  56. m_workerPool = new ObjectJobEngine(PoolWorkerJob, "PollServiceWorker", 4000, m_WorkerThreadCount);
  57. m_retrysThread = WorkManager.StartThread(
  58. CheckRetries,
  59. string.Format("PollServiceWatcherThread"),
  60. ThreadPriority.Normal,
  61. true,
  62. true,
  63. null,
  64. 1000 * 60 * 10);
  65. }
  66. private void ReQueueEvent(PollServiceHttpRequest req)
  67. {
  68. if (m_running)
  69. m_retryRequests.Enqueue(req);
  70. }
  71. public void Enqueue(PollServiceHttpRequest req)
  72. {
  73. if(m_running)
  74. m_workerPool.Enqueue(req);
  75. }
  76. private void CheckRetries()
  77. {
  78. PollServiceHttpRequest preq;
  79. while (m_running)
  80. {
  81. Thread.Sleep(100);
  82. Watchdog.UpdateThread();
  83. while (m_running && m_retryRequests.TryDequeue(out preq))
  84. m_workerPool.Enqueue(preq);
  85. }
  86. }
  87. public void Stop()
  88. {
  89. if(!m_running)
  90. return;
  91. m_running = false;
  92. Thread.Sleep(100); // let the world move
  93. try
  94. {
  95. while (m_retryRequests.TryDequeue(out PollServiceHttpRequest req))
  96. req.DoHTTPstop();
  97. }
  98. catch
  99. {
  100. }
  101. int count = 10;
  102. while(-- count > 0 && m_workerPool.Count > 0)
  103. Thread.Sleep(100);
  104. m_workerPool.Dispose();
  105. m_workerPool = null;
  106. }
  107. // work threads
  108. private void PoolWorkerJob(object o)
  109. {
  110. PollServiceHttpRequest req = o as PollServiceHttpRequest;
  111. if(req == null)
  112. return;
  113. try
  114. {
  115. if (!req.Request.Context.CanSend())
  116. {
  117. req.PollServiceArgs.Drop(req.RequestID, req.PollServiceArgs.Id);
  118. return;
  119. }
  120. if(!m_running)
  121. {
  122. req.DoHTTPstop();
  123. return;
  124. }
  125. if (req.Request.Context.IsSending())
  126. {
  127. ReQueueEvent(req);
  128. return;
  129. }
  130. if (req.PollServiceArgs.HasEvents(req.RequestID, req.PollServiceArgs.Id))
  131. {
  132. try
  133. {
  134. Hashtable responsedata = req.PollServiceArgs.GetEvents(req.RequestID, req.PollServiceArgs.Id);
  135. req.DoHTTPGruntWork(responsedata);
  136. }
  137. catch { }
  138. }
  139. else
  140. {
  141. if ((Environment.TickCount - req.RequestTime) > req.PollServiceArgs.TimeOutms)
  142. {
  143. try
  144. {
  145. req.DoHTTPGruntWork(req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id));
  146. }
  147. catch { }
  148. }
  149. else
  150. {
  151. ReQueueEvent(req);
  152. }
  153. }
  154. }
  155. catch (Exception e)
  156. {
  157. m_log.ErrorFormat("Exception in poll service thread: " + e.ToString());
  158. }
  159. }
  160. }
  161. }