/// <summary>
        /// Downloads the most recent copy of the endpoint address file and
        /// parses it to a collection of address range objects.
        /// </summary>
        public static AWSPublicIpAddressRanges Load(IWebProxy proxy)
        {
            const int maxDownloadRetries = 3;

            var retries = 0;

            while (retries < maxDownloadRetries)
            {
                try
                {
                    var content = AWSSDKUtils.DownloadStringContent(IpAddressRangeEndpoint, proxy);
                    AWSPublicIpAddressRanges instance = Parse(content);

                    return(instance);
                }
                catch (Exception e)
                {
                    retries++;
                    if (retries == maxDownloadRetries)
                    {
                        throw new AmazonServiceException(
                                  string.Format(CultureInfo.InvariantCulture,
                                                "Error downloading public IP address ranges file from {0}.", IpAddressRangeEndpoint),
                                  e);
                    }
                }

                var delay = (int)(Math.Pow(4, retries) * 100);
                delay = Math.Min(delay, 30 * 1000);
                AWSSDKUtils.Sleep(delay);
            }

            return(null);
        }
Exemplo n.º 2
0
        private static List <string> GetItems(string relativeOrAbsolutePath, int tries, bool slurp)
        {
            var items = new List <string>();

            try
            {
                // if we are given a relative path, we assume the data we need exists under the
                // main metadata root
                var uri = relativeOrAbsolutePath.StartsWith(EC2_METADATA_SVC, StringComparison.Ordinal)
                            ? new Uri(relativeOrAbsolutePath)
                            : new Uri(EC2_METADATA_ROOT + relativeOrAbsolutePath);

                var content = AWSSDKUtils.DownloadStringContent(uri, TimeSpan.FromSeconds(5));
                using (var stream = new StringReader(content))
                {
                    if (slurp)
                    {
                        items.Add(stream.ReadToEnd());
                    }
                    else
                    {
                        string line;
                        do
                        {
                            line = stream.ReadLine();
                            if (line != null)
                            {
                                items.Add(line.Trim());
                            }
                        }while (line != null);
                    }
                }
            }
            catch (WebException wex)
            {
                var response = wex.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }

                if (tries <= 1)
                {
                    Logger.GetLogger(typeof(EC2InstanceMetadata)).Error(wex, "Unable to contact EC2 Metadata service.");
                    return(null);
                }

                PauseExponentially(tries);
                return(GetItems(relativeOrAbsolutePath, tries - 1, slurp));
            }

            return(items);
        }
        public static AWSPublicIpAddressRanges Load()
        {
            int num = 0;

            while (num < 3)
            {
                try
                {
                    return(Parse(AWSSDKUtils.DownloadStringContent(IpAddressRangeEndpoint)));
                }
                catch (Exception innerException)
                {
                    num++;
                    if (num == 3)
                    {
                        throw new AmazonServiceException(string.Format(CultureInfo.InvariantCulture, "Error downloading public IP address ranges file from {0}.", IpAddressRangeEndpoint), innerException);
                    }
                }
                AWSSDKUtils.Sleep(Math.Min((int)(Math.Pow(4.0, (double)num) * 100.0), 30000));
            }
            return(null);
        }