From 9e4bf3439cbe654d8ef531abb68a3f755dee7a55 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 22 Jan 2018 19:50:07 +0000 Subject: change throtle datasnapshot get --- .../DataSnapshot/DataRequestHandler.cs | 71 +++++++++++++++++++--- 1 file changed, 64 insertions(+), 7 deletions(-) (limited to 'OpenSim/Region/OptionalModules/DataSnapshot/DataRequestHandler.cs') diff --git a/OpenSim/Region/OptionalModules/DataSnapshot/DataRequestHandler.cs b/OpenSim/Region/OptionalModules/DataSnapshot/DataRequestHandler.cs index fb60e9e..817170f 100644 --- a/OpenSim/Region/OptionalModules/DataSnapshot/DataRequestHandler.cs +++ b/OpenSim/Region/OptionalModules/DataSnapshot/DataRequestHandler.cs @@ -45,6 +45,7 @@ namespace OpenSim.Region.DataSnapshot // private Scene m_scene = null; private DataSnapshotManager m_externalData = null; private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private ExpiringCache throotleGen = new ExpiringCache(); public DataRequestHandler(Scene scene, DataSnapshotManager externalData) { @@ -61,25 +62,81 @@ namespace OpenSim.Region.DataSnapshot } + private string GetClientString(Hashtable request) + { + string clientstring = ""; + if (!request.ContainsKey("headers")) + return clientstring; + + Hashtable requestinfo = (Hashtable)request["headers"]; + if (requestinfo.ContainsKey("x-forwarded-for")) + { + object str = requestinfo["x-forwarded-for"]; + if (str != null) + { + if (!string.IsNullOrEmpty(str.ToString())) + { + return str.ToString(); + } + } + } + if (!requestinfo.ContainsKey("remote_addr")) + return clientstring; + + object remote_addrobj = requestinfo["remote_addr"]; + if (remote_addrobj != null) + { + if (!string.IsNullOrEmpty(remote_addrobj.ToString())) + { + clientstring = remote_addrobj.ToString(); + } + } + + return clientstring; + } + public Hashtable OnGetSnapshot(Hashtable keysvals) { - string snapObj = (string)keysvals["region"]; - m_log.DebugFormat("[DATASNAPSHOT] Received collection request for {0}", snapObj); Hashtable reply = new Hashtable(); - int statuscode = 200; + string reqtag; + string snapObj = (string)keysvals["region"]; + if(string.IsNullOrWhiteSpace(snapObj)) + reqtag = GetClientString(keysvals); + else + reqtag = snapObj + GetClientString(keysvals); + + + if(!string.IsNullOrWhiteSpace(reqtag)) + { + if(throotleGen.Contains(reqtag)) + { + reply["str_response_string"] = "Please try your request again later"; + reply["int_response_code"] = 503; + reply["content_type"] = "text/plain"; + m_log.Debug("[DATASNAPSHOT] Collection request spam. reply try later"); + return reply; + } + + throotleGen.AddOrUpdate(reqtag, 0, 60); + } + + if(string.IsNullOrWhiteSpace(snapObj)) + m_log.DebugFormat("[DATASNAPSHOT] Received collection request for all"); + else + m_log.DebugFormat("[DATASNAPSHOT] Received collection request for {0}", snapObj); XmlDocument response = m_externalData.GetSnapshot(snapObj); if(response == null) { - reply["str_response_string"] = string.Empty; + reply["str_response_string"] = "Please try your request again later"; reply["int_response_code"] = 503; - reply["content_type"] = "text"; - m_log.Debug("[DATASNAPSHOT] Collection request reply try later"); + reply["content_type"] = "text/plain"; + m_log.Debug("[DATASNAPSHOT] Collection request spam. reply try later"); return reply; } reply["str_response_string"] = response.OuterXml; - reply["int_response_code"] = statuscode; + reply["int_response_code"] = 200; reply["content_type"] = "text/xml"; return reply; } -- cgit v1.1