PollServiceRequestManager.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. namespace OpenSim.Framework.Servers.HttpServer
  35. {
  36. public class PollServiceRequestManager
  37. {
  38. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private readonly BaseHttpServer m_server;
  40. private static Queue m_requests = Queue.Synchronized(new Queue());
  41. private uint m_WorkerThreadCount = 0;
  42. private Thread[] m_workerThreads;
  43. private PollServiceWorkerThread[] m_PollServiceWorkerThreads;
  44. private Thread m_watcherThread;
  45. private bool m_running = true;
  46. public PollServiceRequestManager(BaseHttpServer pSrv, uint pWorkerThreadCount, int pTimeout)
  47. {
  48. m_server = pSrv;
  49. m_WorkerThreadCount = pWorkerThreadCount;
  50. m_workerThreads = new Thread[m_WorkerThreadCount];
  51. m_PollServiceWorkerThreads = new PollServiceWorkerThread[m_WorkerThreadCount];
  52. //startup worker threads
  53. for (uint i = 0; i < m_WorkerThreadCount; i++)
  54. {
  55. m_PollServiceWorkerThreads[i] = new PollServiceWorkerThread(m_server, pTimeout);
  56. m_PollServiceWorkerThreads[i].ReQueue += ReQueueEvent;
  57. m_workerThreads[i]
  58. = Watchdog.StartThread(
  59. m_PollServiceWorkerThreads[i].ThreadStart,
  60. String.Format("PollServiceWorkerThread{0}", i),
  61. ThreadPriority.Normal,
  62. false,
  63. int.MaxValue);
  64. }
  65. m_watcherThread
  66. = Watchdog.StartThread(
  67. this.ThreadStart,
  68. "PollServiceWatcherThread",
  69. ThreadPriority.Normal,
  70. false,
  71. 1000 * 60 * 10);
  72. }
  73. internal void ReQueueEvent(PollServiceHttpRequest req)
  74. {
  75. // Do accounting stuff here
  76. Enqueue(req);
  77. }
  78. public void Enqueue(PollServiceHttpRequest req)
  79. {
  80. lock (m_requests)
  81. m_requests.Enqueue(req);
  82. }
  83. public void ThreadStart()
  84. {
  85. while (m_running)
  86. {
  87. Watchdog.UpdateThread();
  88. ProcessQueuedRequests();
  89. Thread.Sleep(1000);
  90. }
  91. }
  92. private void ProcessQueuedRequests()
  93. {
  94. lock (m_requests)
  95. {
  96. if (m_requests.Count == 0)
  97. return;
  98. // m_log.DebugFormat("[POLL SERVICE REQUEST MANAGER]: Processing {0} requests", m_requests.Count);
  99. int reqperthread = (int) (m_requests.Count/m_WorkerThreadCount) + 1;
  100. // For Each WorkerThread
  101. for (int tc = 0; tc < m_WorkerThreadCount && m_requests.Count > 0; tc++)
  102. {
  103. //Loop over number of requests each thread handles.
  104. for (int i = 0; i < reqperthread && m_requests.Count > 0; i++)
  105. {
  106. try
  107. {
  108. m_PollServiceWorkerThreads[tc].Enqueue((PollServiceHttpRequest)m_requests.Dequeue());
  109. }
  110. catch (InvalidOperationException)
  111. {
  112. // The queue is empty, we did our calculations wrong!
  113. return;
  114. }
  115. }
  116. }
  117. }
  118. }
  119. ~PollServiceRequestManager()
  120. {
  121. foreach (object o in m_requests)
  122. {
  123. PollServiceHttpRequest req = (PollServiceHttpRequest) o;
  124. m_server.DoHTTPGruntWork(
  125. req.PollServiceArgs.NoEvents(req.RequestID, req.PollServiceArgs.Id),
  126. new OSHttpResponse(new HttpResponse(req.HttpContext, req.Request), req.HttpContext));
  127. }
  128. m_requests.Clear();
  129. foreach (Thread t in m_workerThreads)
  130. {
  131. t.Abort();
  132. }
  133. m_running = false;
  134. }
  135. }
  136. }