private bool UpdateCache()

in sdk/src/Core/Internal/Utils/HostEndPoint.cs [154:236]


		private bool UpdateCache()
		{
			if (!cacheLock.TryEnterUpgradeableReadLock(0))
			{
				// Another thread is already performing an update => bail and use potentially dirty cache
				return false;
			}
			try
			{
				// We hold a UpgradableReadLock so when may call IPCacheIsValid
				if (IPCacheIsValid() != CacheState.Invalid)
				{
					// Cache no longer invalid, i.e. another thread performed the update after us seeing it invalid
					// and before now.
					return false;
				}
				
				// We have confirmed that the cache still is invalid and needs updating.
				// We know that we are the only ones that may update it because we hold an UpgradeableReadLock
				// Only one thread may hold such lock at a time, see:
				// https://docs.microsoft.com/en-gb/dotnet/api/system.threading.readerwriterlockslim?view=netframework-4.7.2#remarks
				
				var ipEntries = Dns.GetHostAddresses(Host);
				var newIP = ipEntries.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);

				if (newIP == null)
				{
					_logger.InfoFormat(
						"IP cache invalid: DNS responded with no IP addresses for {1}. Cached IP address not updated!.",
						_ipCache, Host);
					return false;
				}
				// Upgrade our read lock to write mode
				cacheLock.EnterWriteLock();
				try
				{
					_timestampOfLastIPCacheUpdate = DateTime.Now;
					_ipCache = new IPEndPoint(newIP, Port);
					_logger.InfoFormat("IP cache invalid: updated ip cache for {0} to {1}.", Host, newIP);
					return true;
				}
				//Error catching for IPEndPoint creation
				catch (ArgumentNullException)
				{
					_logger.InfoFormat("IP cache invalid: resolved IP address for hostname {0} null. Cached IP address not updated!", Host);
				}
				catch (ArgumentOutOfRangeException)
				{
					_logger.InfoFormat("IP cache invalid: either the port {0} or IP address {1} is out of range. Cached IP address not updated!", Port, newIP);
				}
				finally
				{
					// Downgrade back to read mode
					cacheLock.ExitWriteLock();
				}
			}
			//Error catching for DNS resolve
			catch (ArgumentNullException)
			{
				_logger.InfoFormat(
					"IP cache invalid: failed to resolve DNS due to host being null. Cached IP address not updated!");
			}
			catch (ArgumentOutOfRangeException)
			{
				_logger.InfoFormat(
					"IP cache invalid: failed to resolve DNS due to host being longer than 255 characters. ({0}) Cached IP address not updated!",
					Host);
			}
			catch (SocketException)
			{
				_logger.InfoFormat("IP cache invalid: failed to resolve DNS. ({0}) Cached IP address not updated!", Host);
			}
			catch (ArgumentException)
			{
				_logger.InfoFormat("IP cache invalid: failed to update cache due to {0} not being a valid IP. Cached IP address not updated!", Host);
			}
			finally
			{
				cacheLock.ExitUpgradeableReadLock();
			}

			return false;
		}