ServiceThrottleModule.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 JobEngine m_processorJobEngine;
  49. #region ISharedRegionModule
  50. public void Initialise(IConfigSource config)
  51. {
  52. m_processorJobEngine = new JobEngine(
  53. "ServiceThrottle","ServiceThrottle");
  54. m_processorJobEngine.RequestProcessTimeoutOnStop = 31000; // many webrequests have 30s expire
  55. m_processorJobEngine.Start();
  56. }
  57. public void AddRegion(Scene scene)
  58. {
  59. lock (m_scenes)
  60. {
  61. m_scenes.Add(scene);
  62. scene.RegisterModuleInterface<IServiceThrottleModule>(this);
  63. scene.EventManager.OnNewClient += OnNewClient;
  64. }
  65. }
  66. public void RegionLoaded(Scene scene)
  67. {
  68. }
  69. public void RemoveRegion(Scene scene)
  70. {
  71. lock (m_scenes)
  72. {
  73. m_scenes.Remove(scene);
  74. scene.EventManager.OnNewClient -= OnNewClient;
  75. }
  76. }
  77. public void PostInitialise()
  78. {
  79. }
  80. public void Close()
  81. {
  82. m_processorJobEngine.Stop();
  83. }
  84. public string Name
  85. {
  86. get { return "ServiceThrottleModule"; }
  87. }
  88. public Type ReplaceableInterface
  89. {
  90. get { return null; }
  91. }
  92. #endregion ISharedRegionMOdule
  93. #region Events
  94. void OnNewClient(IClientAPI client)
  95. {
  96. client.OnRegionHandleRequest += OnRegionHandleRequest;
  97. }
  98. public void OnRegionHandleRequest(IClientAPI client, UUID regionID)
  99. {
  100. //m_log.DebugFormat("[SERVICE THROTTLE]: RegionHandleRequest {0}", regionID);
  101. Action action = delegate
  102. {
  103. if(!client.IsActive)
  104. return;
  105. if(m_scenes.Count == 0)
  106. return;
  107. Scene baseScene = m_scenes[0];
  108. if(baseScene == null || baseScene.ShuttingDown)
  109. return;
  110. GridRegion r = baseScene.GridService.GetRegionByUUID(UUID.Zero, regionID);
  111. if(!client.IsActive)
  112. return;
  113. if (r != null && r.RegionHandle != 0)
  114. client.SendRegionHandle(regionID, r.RegionHandle);
  115. };
  116. m_processorJobEngine.QueueJob("regionHandle", action, regionID.ToString());
  117. }
  118. #endregion Events
  119. #region IServiceThrottleModule
  120. public void Enqueue(string category, string itemid, Action continuation)
  121. {
  122. m_processorJobEngine.QueueJob(category, continuation, itemid);
  123. }
  124. #endregion IServiceThrottleModule
  125. }
  126. }