示例#1
0
        private static void SetCountryFromEndpoint(ServerInfo server, IPEndPoint externalEndpoint)
        {
            Task.Run(() =>
            {
                if (DateTime.UtcNow - _lastCountryRequestTime < TimeSpan.FromSeconds(1.5))
                {
                    return;
                }

                _lastCountryRequestTime = DateTime.UtcNow;
                if (EndpointCountries.TryGet(externalEndpoint, out var countryCode))
                {
                    server.Country = countryCode;
                }
                else
                {
                    server.Country = IpApi.GetCountry(externalEndpoint);
                    if (string.IsNullOrEmpty(server.Country))
                    {
                        server.Country = IpLocate.GetCountry(externalEndpoint);
                    }

                    if (!string.IsNullOrEmpty(server.Country))
                    {
                        EndpointCountries.TryAdd(externalEndpoint, server.Country);
                    }
                }
            });
        }
        private void SetCountryFromEndpoint(ServerInfo server, IPEndPoint externalEndpoint)
        {
            Task.Run(async() =>
            {
                if (_refreshingCountryCode)
                {
                    return;
                }

                _refreshingCountryCode = true;
                try
                {
                    if (DateTime.UtcNow - _lastCountryRequestTime < MaxCountryRequestTimeMs)
                    {
                        Thread.Sleep(MaxCountryRequestTimeMs);
                    }

                    _lastCountryRequestTime = DateTime.UtcNow;
                    if (EndpointCountries.TryGet(externalEndpoint, out var countryCode))
                    {
                        server.Country = countryCode;
                    }
                    else
                    {
                        server.Country = await IpApi.GetCountry(externalEndpoint);
                        if (string.IsNullOrEmpty(server.Country))
                        {
                            server.Country = await IpLocate.GetCountry(externalEndpoint);
                        }

                        if (!string.IsNullOrEmpty(server.Country))
                        {
                            EndpointCountries.TryAdd(externalEndpoint, server.Country);
                        }
                    }
                }
                finally
                {
                    _refreshingCountryCode = false;
                }
            });
        }