ServiceThrottleModule.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Framework.Monitoring;
  38. using OpenSim.Region.Framework.Scenes;
  39. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  40. namespace OpenSim.Region.CoreModules.Framework
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GridServiceThrottleModule")]
  43. public class ServiceThrottleModule : ISharedRegionModule, IServiceThrottleModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private readonly List<Scene> m_scenes = new List<Scene>();
  48. private System.Timers.Timer m_timer = new System.Timers.Timer();
  49. private Queue<Action> m_RequestQueue = new Queue<Action>();
  50. private Dictionary<string, List<string>> m_Pending = new Dictionary<string, List<string>>();
  51. private int m_Interval;
  52. #region ISharedRegionModule
  53. public void Initialise(IConfigSource config)
  54. {
  55. m_Interval = Util.GetConfigVarFromSections<int>(config, "Interval", new string[] { "ServiceThrottle" }, 5000);
  56. m_timer = new System.Timers.Timer();
  57. m_timer.AutoReset = false;
  58. m_timer.Enabled = true;
  59. m_timer.Interval = 15000; // 15 secs at first
  60. m_timer.Elapsed += ProcessQueue;
  61. m_timer.Start();
  62. //WorkManager.StartThread(
  63. // ProcessQueue,
  64. // "GridServiceRequestThread",
  65. // ThreadPriority.BelowNormal,
  66. // true,
  67. // false);
  68. }
  69. public void AddRegion(Scene scene)
  70. {
  71. lock (m_scenes)
  72. {
  73. m_scenes.Add(scene);
  74. scene.RegisterModuleInterface<IServiceThrottleModule>(this);
  75. scene.EventManager.OnNewClient += OnNewClient;
  76. scene.EventManager.OnMakeRootAgent += OnMakeRootAgent;
  77. }
  78. }
  79. public void RegionLoaded(Scene scene)
  80. {
  81. }
  82. public void RemoveRegion(Scene scene)
  83. {
  84. lock (m_scenes)
  85. {
  86. m_scenes.Remove(scene);
  87. scene.EventManager.OnNewClient -= OnNewClient;
  88. }
  89. }
  90. public void PostInitialise()
  91. {
  92. }
  93. public void Close()
  94. {
  95. }
  96. public string Name
  97. {
  98. get { return "ServiceThrottleModule"; }
  99. }
  100. public Type ReplaceableInterface
  101. {
  102. get { return null; }
  103. }
  104. #endregion ISharedRegionMOdule
  105. #region Events
  106. void OnNewClient(IClientAPI client)
  107. {
  108. client.OnRegionHandleRequest += OnRegionHandleRequest;
  109. }
  110. void OnMakeRootAgent(ScenePresence obj)
  111. {
  112. lock (m_timer)
  113. {
  114. if (!m_timer.Enabled)
  115. {
  116. m_timer.Interval = m_Interval;
  117. m_timer.Enabled = true;
  118. m_timer.Start();
  119. }
  120. }
  121. }
  122. public void OnRegionHandleRequest(IClientAPI client, UUID regionID)
  123. {
  124. //m_log.DebugFormat("[SERVICE THROTTLE]: RegionHandleRequest {0}", regionID);
  125. ulong handle = 0;
  126. if (IsLocalRegionHandle(regionID, out handle))
  127. {
  128. client.SendRegionHandle(regionID, handle);
  129. return;
  130. }
  131. Action action = delegate
  132. {
  133. GridRegion r = m_scenes[0].GridService.GetRegionByUUID(UUID.Zero, regionID);
  134. if (r != null && r.RegionHandle != 0)
  135. client.SendRegionHandle(regionID, r.RegionHandle);
  136. };
  137. Enqueue("region", regionID.ToString(), action);
  138. }
  139. #endregion Events
  140. #region IServiceThrottleModule
  141. public void Enqueue(string category, string itemid, Action continuation)
  142. {
  143. lock (m_RequestQueue)
  144. {
  145. if (m_Pending.ContainsKey(category))
  146. {
  147. if (m_Pending[category].Contains(itemid))
  148. // Don't enqueue, it's already pending
  149. return;
  150. }
  151. else
  152. m_Pending.Add(category, new List<string>());
  153. m_Pending[category].Add(itemid);
  154. m_RequestQueue.Enqueue(delegate
  155. {
  156. lock (m_RequestQueue)
  157. m_Pending[category].Remove(itemid);
  158. continuation();
  159. });
  160. }
  161. }
  162. #endregion IServiceThrottleModule
  163. #region Process Continuation Queue
  164. private void ProcessQueue(object sender, System.Timers.ElapsedEventArgs e)
  165. {
  166. //m_log.DebugFormat("[YYY]: Process queue with {0} continuations", m_RequestQueue.Count);
  167. while (m_RequestQueue.Count > 0)
  168. {
  169. Action continuation = null;
  170. lock (m_RequestQueue)
  171. continuation = m_RequestQueue.Dequeue();
  172. if (continuation != null)
  173. continuation();
  174. }
  175. if (AreThereRootAgents())
  176. {
  177. lock (m_timer)
  178. {
  179. m_timer.Interval = 1000; // 1 sec
  180. m_timer.Enabled = true;
  181. m_timer.Start();
  182. }
  183. }
  184. else
  185. lock (m_timer)
  186. m_timer.Enabled = false;
  187. }
  188. #endregion Process Continuation Queue
  189. #region Misc
  190. private bool IsLocalRegionHandle(UUID regionID, out ulong regionHandle)
  191. {
  192. regionHandle = 0;
  193. foreach (Scene s in m_scenes)
  194. if (s.RegionInfo.RegionID == regionID)
  195. {
  196. regionHandle = s.RegionInfo.RegionHandle;
  197. return true;
  198. }
  199. return false;
  200. }
  201. private bool AreThereRootAgents()
  202. {
  203. foreach (Scene s in m_scenes)
  204. {
  205. foreach (ScenePresence sp in s.GetScenePresences())
  206. if (!sp.IsChildAgent)
  207. return true;
  208. }
  209. return false;
  210. }
  211. #endregion Misc
  212. }
  213. }