diff options
author | Diva Canto | 2013-07-16 17:06:17 -0700 |
---|---|---|
committer | Diva Canto | 2013-07-16 17:06:17 -0700 |
commit | 99a600753e2013a5e5c4649da78bc5b871461c8e (patch) | |
tree | 7697a536b9da1f93bdae5d63e23e1b908d89f6f9 /OpenSim/Region/CoreModules/Framework/ServiceThrottle | |
parent | Eliminated the UserManagement/UserManagementModule throttle thread. Made the ... (diff) | |
download | opensim-SC-99a600753e2013a5e5c4649da78bc5b871461c8e.zip opensim-SC-99a600753e2013a5e5c4649da78bc5b871461c8e.tar.gz opensim-SC-99a600753e2013a5e5c4649da78bc5b871461c8e.tar.bz2 opensim-SC-99a600753e2013a5e5c4649da78bc5b871461c8e.tar.xz |
Changed the name to ServiceThrottle/ServiceThrottleModule in order to reflect its more generic nature.
Diffstat (limited to 'OpenSim/Region/CoreModules/Framework/ServiceThrottle')
-rw-r--r-- | OpenSim/Region/CoreModules/Framework/ServiceThrottle/ServiceThrottleModule.cs | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Framework/ServiceThrottle/ServiceThrottleModule.cs b/OpenSim/Region/CoreModules/Framework/ServiceThrottle/ServiceThrottleModule.cs new file mode 100644 index 0000000..d805fd3 --- /dev/null +++ b/OpenSim/Region/CoreModules/Framework/ServiceThrottle/ServiceThrottleModule.cs | |||
@@ -0,0 +1,237 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Threading; | ||
32 | using log4net; | ||
33 | using Mono.Addins; | ||
34 | using Nini.Config; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Framework; | ||
37 | using OpenSim.Region.Framework.Interfaces; | ||
38 | using OpenSim.Framework.Monitoring; | ||
39 | using OpenSim.Region.Framework.Scenes; | ||
40 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
41 | |||
42 | namespace OpenSim.Region.CoreModules.Framework | ||
43 | { | ||
44 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GridServiceThrottleModule")] | ||
45 | public class ServiceThrottleModule : ISharedRegionModule, IServiceThrottleModule | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger( | ||
48 | MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | private readonly List<Scene> m_scenes = new List<Scene>(); | ||
51 | private System.Timers.Timer m_timer = new System.Timers.Timer(); | ||
52 | |||
53 | //private OpenSim.Framework.BlockingQueue<GridRegionRequest> m_RequestQueue = new OpenSim.Framework.BlockingQueue<GridRegionRequest>(); | ||
54 | // private OpenSim.Framework.DoubleQueue<GridRegionRequest> m_RequestQueue = new OpenSim.Framework.DoubleQueue<GridRegionRequest>(); | ||
55 | //private Queue<GridRegionRequest> m_RequestQueue = new Queue<GridRegionRequest>(); | ||
56 | private Queue<Action> m_RequestQueue = new Queue<Action>(); | ||
57 | |||
58 | #region ISharedRegionModule | ||
59 | |||
60 | public void Initialise(IConfigSource config) | ||
61 | { | ||
62 | m_timer = new System.Timers.Timer(); | ||
63 | m_timer.AutoReset = false; | ||
64 | m_timer.Enabled = true; | ||
65 | m_timer.Interval = 15000; // 15 secs at first | ||
66 | m_timer.Elapsed += ProcessQueue; | ||
67 | m_timer.Start(); | ||
68 | |||
69 | //Watchdog.StartThread( | ||
70 | // ProcessQueue, | ||
71 | // "GridServiceRequestThread", | ||
72 | // ThreadPriority.BelowNormal, | ||
73 | // true, | ||
74 | // false); | ||
75 | } | ||
76 | |||
77 | public void AddRegion(Scene scene) | ||
78 | { | ||
79 | lock (m_scenes) | ||
80 | { | ||
81 | m_scenes.Add(scene); | ||
82 | scene.RegisterModuleInterface<IServiceThrottleModule>(this); | ||
83 | scene.EventManager.OnNewClient += OnNewClient; | ||
84 | scene.EventManager.OnMakeRootAgent += OnMakeRootAgent; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | public void RegionLoaded(Scene scene) | ||
89 | { | ||
90 | } | ||
91 | |||
92 | public void RemoveRegion(Scene scene) | ||
93 | { | ||
94 | lock (m_scenes) | ||
95 | { | ||
96 | m_scenes.Remove(scene); | ||
97 | scene.EventManager.OnNewClient -= OnNewClient; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | public void PostInitialise() | ||
102 | { | ||
103 | } | ||
104 | |||
105 | public void Close() | ||
106 | { | ||
107 | } | ||
108 | |||
109 | public string Name | ||
110 | { | ||
111 | get { return "ServiceThrottleModule"; } | ||
112 | } | ||
113 | |||
114 | public Type ReplaceableInterface | ||
115 | { | ||
116 | get { return null; } | ||
117 | } | ||
118 | |||
119 | #endregion ISharedRegionMOdule | ||
120 | |||
121 | #region Events | ||
122 | |||
123 | void OnNewClient(IClientAPI client) | ||
124 | { | ||
125 | client.OnRegionHandleRequest += OnRegionHandleRequest; | ||
126 | } | ||
127 | |||
128 | void OnMakeRootAgent(ScenePresence obj) | ||
129 | { | ||
130 | lock (m_timer) | ||
131 | { | ||
132 | if (!m_timer.Enabled) | ||
133 | { | ||
134 | m_timer.Interval = 1000; | ||
135 | m_timer.Enabled = true; | ||
136 | m_timer.Start(); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | public void OnRegionHandleRequest(IClientAPI client, UUID regionID) | ||
142 | { | ||
143 | //m_log.DebugFormat("[SERVICE THROTTLE]: RegionHandleRequest {0}", regionID); | ||
144 | ulong handle = 0; | ||
145 | if (IsLocalRegionHandle(regionID, out handle)) | ||
146 | { | ||
147 | client.SendRegionHandle(regionID, handle); | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | Action action = delegate | ||
152 | { | ||
153 | GridRegion r = m_scenes[0].GridService.GetRegionByUUID(UUID.Zero, regionID); | ||
154 | |||
155 | if (r != null && r.RegionHandle != 0) | ||
156 | client.SendRegionHandle(regionID, r.RegionHandle); | ||
157 | }; | ||
158 | |||
159 | lock (m_RequestQueue) | ||
160 | m_RequestQueue.Enqueue(action); | ||
161 | |||
162 | } | ||
163 | |||
164 | #endregion Events | ||
165 | |||
166 | #region IServiceThrottleModule | ||
167 | |||
168 | public void Enqueue(Action continuation) | ||
169 | { | ||
170 | m_RequestQueue.Enqueue(continuation); | ||
171 | } | ||
172 | |||
173 | #endregion IServiceThrottleModule | ||
174 | |||
175 | #region Process Continuation Queue | ||
176 | |||
177 | private void ProcessQueue(object sender, System.Timers.ElapsedEventArgs e) | ||
178 | { | ||
179 | m_log.DebugFormat("[YYY]: Process queue with {0} continuations", m_RequestQueue.Count); | ||
180 | |||
181 | while (m_RequestQueue.Count > 0) | ||
182 | { | ||
183 | Action continuation = null; | ||
184 | lock (m_RequestQueue) | ||
185 | continuation = m_RequestQueue.Dequeue(); | ||
186 | |||
187 | if (continuation != null) | ||
188 | continuation(); | ||
189 | } | ||
190 | |||
191 | if (AreThereRootAgents()) | ||
192 | { | ||
193 | lock (m_timer) | ||
194 | { | ||
195 | m_timer.Interval = 1000; // 1 sec | ||
196 | m_timer.Enabled = true; | ||
197 | m_timer.Start(); | ||
198 | } | ||
199 | } | ||
200 | else | ||
201 | lock (m_timer) | ||
202 | m_timer.Enabled = false; | ||
203 | |||
204 | } | ||
205 | |||
206 | #endregion Process Continuation Queue | ||
207 | |||
208 | #region Misc | ||
209 | |||
210 | private bool IsLocalRegionHandle(UUID regionID, out ulong regionHandle) | ||
211 | { | ||
212 | regionHandle = 0; | ||
213 | foreach (Scene s in m_scenes) | ||
214 | if (s.RegionInfo.RegionID == regionID) | ||
215 | { | ||
216 | regionHandle = s.RegionInfo.RegionHandle; | ||
217 | return true; | ||
218 | } | ||
219 | return false; | ||
220 | } | ||
221 | |||
222 | private bool AreThereRootAgents() | ||
223 | { | ||
224 | foreach (Scene s in m_scenes) | ||
225 | { | ||
226 | foreach (ScenePresence sp in s.GetScenePresences()) | ||
227 | if (!sp.IsChildAgent) | ||
228 | return true; | ||
229 | } | ||
230 | |||
231 | return false; | ||
232 | } | ||
233 | |||
234 | #endregion Misc | ||
235 | } | ||
236 | |||
237 | } | ||