cache endpoints (and other paths) dns requests for 5min, this delay should be acceptable in all cases ?

0.9.0-post-fixes
UbitUmarov 2017-05-29 07:11:13 +01:00
parent 0f0673552c
commit a317bba8cf
1 changed files with 25 additions and 5 deletions

View File

@ -991,6 +991,8 @@ namespace OpenSim.Framework
return output.ToString(); return output.ToString();
} }
static ExpiringCache<string,IPAddress> dnscache = new ExpiringCache<string, IPAddress>();
/// <summary> /// <summary>
/// Converts a URL to a IPAddress /// Converts a URL to a IPAddress
/// </summary> /// </summary>
@ -1008,15 +1010,20 @@ namespace OpenSim.Framework
/// <returns>An IP address, or null</returns> /// <returns>An IP address, or null</returns>
public static IPAddress GetHostFromDNS(string dnsAddress) public static IPAddress GetHostFromDNS(string dnsAddress)
{ {
// If it is already an IP, avoid possible broken mono from seeing it
if(String.IsNullOrWhiteSpace(dnsAddress)) if(String.IsNullOrWhiteSpace(dnsAddress))
return null; return null;
IPAddress ia = null; IPAddress ia = null;
if(dnscache.TryGetValue(dnsAddress, out ia) && ia != null)
return ia;
ia = null;
// If it is already an IP, don't let GetHostEntry see it
if (IPAddress.TryParse(dnsAddress, out ia) && ia != null) if (IPAddress.TryParse(dnsAddress, out ia) && ia != null)
{ {
if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any)) if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any))
return null; return null;
dnscache.AddOrUpdate(dnsAddress, ia, 300);
return ia; return ia;
} }
@ -1027,7 +1034,7 @@ namespace OpenSim.Framework
} }
catch // (SocketException e) catch // (SocketException e)
{ {
return null; return null;
} }
if(IPH == null || IPH.AddressList.Length == 0) if(IPH == null || IPH.AddressList.Length == 0)
@ -1045,6 +1052,8 @@ namespace OpenSim.Framework
break; break;
} }
} }
if(ia != null)
dnscache.AddOrUpdate(dnsAddress, ia, 300);
return ia; return ia;
} }
@ -1071,14 +1080,22 @@ namespace OpenSim.Framework
return null; return null;
IPAddress ia = null; IPAddress ia = null;
// If it is already an IP, avoid possible broken mono from seeing it if(dnscache.TryGetValue(hostname, out ia) && ia != null)
return getEndPoint(ia, port);
ia = null;
// If it is already an IP, don't let GetHostEntry see it
if (IPAddress.TryParse(hostname, out ia) && ia != null) if (IPAddress.TryParse(hostname, out ia) && ia != null)
{ {
if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any)) if (ia.Equals(IPAddress.Any) || ia.Equals(IPAddress.IPv6Any))
return null; return null;
dnscache.AddOrUpdate(hostname, ia, 300);
return getEndPoint(ia, port); return getEndPoint(ia, port);
} }
IPHostEntry IPH; IPHostEntry IPH;
try try
{ {
@ -1086,7 +1103,7 @@ namespace OpenSim.Framework
} }
catch // (SocketException e) catch // (SocketException e)
{ {
return null; return null;
} }
if(IPH == null || IPH.AddressList.Length == 0) if(IPH == null || IPH.AddressList.Length == 0)
@ -1105,6 +1122,9 @@ namespace OpenSim.Framework
} }
} }
if(ia != null)
dnscache.AddOrUpdate(hostname, ia, 300);
return getEndPoint(ia,port); return getEndPoint(ia,port);
} }