Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New ReportUtilities fails to get 3 reports in a row #36

Closed
pieceofsummer opened this issue Nov 20, 2014 · 3 comments
Closed

New ReportUtilities fails to get 3 reports in a row #36

pieceofsummer opened this issue Nov 20, 2014 · 3 comments

Comments

@pieceofsummer
Copy link

Hello.

I've just updated my application to 18.6.0 (via NuGet, if that matters) and discovered a weird behavior of new ReportUtilities: it successfully gets two reports for the same client, but requesting a third one will just hang until timeout. Switching back to legacy ReportUtilities works fine.

Here's some test code.
It requests 3 exactly the same reports (for simplicity, reproduces for different reports too) in a row, showing the first two are returned in a few seconds, but 3rd one always timeouts after 100 seconds (default timeout in app.config). Increasing the timeout value doesn't help, just makes it stuck for more time.

using System;
using System.Xml;
using System.Diagnostics;
using Google.Api.Ads.Common.Lib;
using Google.Api.Ads.AdWords.Lib;
using Google.Api.Ads.AdWords.v201409;

namespace test_report
{
    class Program
    {
        static void Main(string[] args)
        {
            var adwords = new AdWordsUser();
            var config = (AdWordsAppConfig)adwords.Config;

            config.Email = "...";
            config.DeveloperToken = "...";
            config.OAuth2Mode = OAuth2Flow.APPLICATION;
            config.OAuth2RefreshToken = "...";
            config.OAuth2ClientId = "...";
            config.OAuth2ClientSecret = "...";
            config.ClientCustomerId = "...";

            const string ReportVersion = "v201409";
            DateTime startDate = new DateTime(2014, 10, 1), endDate = new DateTime(2014, 10, 31);

            for (int i = 1; i <= 3; i++)
            {
                Console.WriteLine("Report #{0}...", i);

                var defi = new ReportDefinition();
                defi.dateRangeType = ReportDefinitionDateRangeType.CUSTOM_DATE;
                defi.reportType = ReportDefinitionReportType.KEYWORDS_PERFORMANCE_REPORT;
                defi.reportName = "KEYWORDS_PERFORMANCE_REPORT";
                defi.downloadFormat = DownloadFormat.XML;
                defi.selector = new Selector()
                {
                    fields = new string[] {
                        "CampaignId", "CampaignName", "Id", "KeywordText",
                        "Date", "Impressions", "Clicks", "Cost", "AdNetworkType1"
                    },
                    dateRange = new DateRange
                    {
                        min = startDate.ToString("yyyyMMdd"),
                        max = endDate.ToString("yyyyMMdd")
                    }
                };

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                try
                {
                    // this works for first two reports and always fails for the 3rd one:
                    var util = new Google.Api.Ads.AdWords.Util.Reports.ReportUtilities(adwords, ReportVersion, defi);
                    var response = util.GetResponse();
                    var xml = new XmlDocument();
                    xml.Load(response.Stream);

                    // and this works okay:
                    //var util = new Google.Api.Ads.AdWords.Util.Reports.Legacy.ReportUtilities(adwords);
                    //util.ReportVersion = ReportVersion;
                    //var response = util.GetClientReport(defi);
                    //var xml = new XmlDocument();
                    //xml.LoadXml(response.Text);

                    stopwatch.Stop();
                    Console.WriteLine("Done in {0}", stopwatch.Elapsed);
                }
                catch(Exception ex)
                {
                    stopwatch.Stop();
                    Console.WriteLine("Error in {0}: {1}", stopwatch.Elapsed, ex.Message);
                }
            }
        }
    }
}

The output is:

Report #1...
Done in 00:00:04.3187831
Report #2...
Done in 00:00:02.8653650
Report #3...
Error in 00:01:40.0315389: The operation has timed out
@AnashOommen
Copy link
Member

Hi,

ReportResponse is a disposable object, which wraps the underlying WebRequest and WebResponse. Unless it is disposed, .NET won't open more connections. The magical number "3" comes from ServicePointManager.DefaultConnectionLimit Property, which is 2.

Try modifying your code to:

using (ReportResponse response = util.GetResponse()) {
   var xml = new XmlDocument();
   xml.Load(response.Stream);

   stopwatch.Stop();
   Console.WriteLine("Done in {0}", stopwatch.Elapsed);
}

I will update the migration guide appropriately.

@AnashOommen
Copy link
Member

Reopening this issue, since the code examples also needs to be updated.

@AnashOommen
Copy link
Member

Fixed in v18.7.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants