PollServiceRequestManager.cs 5.9 KB

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