"keep alive" equivalent?

80 views
Skip to first unread message

crozzer

unread,
Dec 19, 2008, 5:59:13 PM12/19/08
to AdWords API Forum
I've got a long-running job (a few hours) that iterates through all of
our 2.8 million criterion across 5 accounts in order to download
them. I intermittently run across an error like "the connections was
expected to be kept open but was closed by the server..." (I can get
the exact error next time it happens, if necessary).

I'm hoping someone might suggest a good "keep alive" type request I
could insert into my code to execute on occasion with the intent of
keeping the connection alive.

Also, if I try/catch an exception, could you suggest code for re-
establishing a connection, rather just bailing out completely?

We are on .NET 3.5 and sdwords api v13

Thanks!

AdWords API Advisor

unread,
Dec 22, 2008, 11:28:09 AM12/22/08
to AdWords API Forum
Hello,

It's certainly possible that Google's web servers impose some server-
side timeout on their connections, though I expect it's something
fairly high. (I'm afraid I don't know off the top of my head what it
is, and it's probably not something specific to the AdWords API but
rather our web infrastructure in general.) I don't think the correct
way of dealing with that is to "tickle" an active connection that is
waiting for a response with a new request in the hopes of preventing
the server from dropping the connection before it responds to the
original request. I also don't think that there is any provision in
the HTTP spec in general for using the same connection to send
multiple HTTP requests in parallel--unless I'm mistaken, HTTP keep-
alives refer to using the same connection for multiple requests one
after another in sequence.

So anyway, the way to approach this problem is to first make sure
that the timeout isn't being triggered on the client side--I know you
say the specific error message points to the server, but just double-
check that. Assuming it's a server-side timeout, restructuring your
requests so that you request smaller chunks of data at a time would
make sense, or alternatively switching to using the ReportService to
retrieve the data you're looking for, would make sense to me.

Also, and I'm kind of surprised I haven't seen any discussion of this
in this group yet, in 2009 we'll be rolling out a major new release of
the AdWords API. One of the features of this release will be support
for asynchronous method calls. In such a setup you wouldn't have to
keep your HTTP connection open waiting for the response from a large
request in the first place.

More details on the changes in store for the AdWords API in 2009 is
available on the blog at

http://adwordsapi.blogspot.com/2008/12/preview-of-2009-adwords-api-changes.html

Cheers,
-Jeff Posnick, AdWords API Team

crozzer

unread,
Dec 22, 2008, 6:39:15 PM12/22/08
to AdWords API Forum
Here is the actual error message.
I'm interpreting this to mean that Google is cutting me off mid-
request/response.
I'm going to wrap it in a retry and see how that goes.
But, if you have any suggestions for how to prevent this in the first
place, please let me know.
Thanks~
=============================
Server Error in '/' Application.
The underlying connection was closed: A connection that was expected
to be kept alive was closed by the server.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection
was closed: A connection that was expected to be kept alive was closed
by the server.

Source Error:

Line 137: [return: System.Xml.Serialization.XmlElementAttribute
("getAllCriteriaReturn")]
Line 138: public Criterion[] getAllCriteria(long adGroupId) {
Line 139: object[] results = this.Invoke("getAllCriteria",
new object[] {
Line 140: adGroupId});
Line 141: return ((Criterion[])(results[0]));


Source File: C:\inetpub\ppc-tools\lib\awapi_dotnet_lib_2.1.0\src
\v13\CriterionService.cs Line: 139

Stack Trace:

[WebException: The underlying connection was closed: A connection that
was expected to be kept alive was closed by the server.]
System.Web.Services.Protocols.WebClientProtocol.GetWebResponse
(WebRequest request) +263
System.Web.Services.Protocols.HttpWebClientProtocol.GetWebResponse
(WebRequest request) +4
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters) +172
com.google.api.adwords.v13.CriterionService.getAllCriteria(Int64
adGroupId) in C:\inetpub\ppc-tools\lib\awapi_dotnet_lib_2.1.0\src
\v13\CriterionService.cs:139
ja.api.google.adwords.MasterClient.getAllDB(Int64[] accounts) in c:
\inetpub\ppc-tools\App_Code\ja_google_api_adwords\MasterClient.cs:522
GetActiveKeywords.Page_Load(Object sender, EventArgs e) in c:
\inetpub\ppc-tools\GetActiveKeywords.aspx.cs:40
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,
Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object
sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +50
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+627


Version Information: Microsoft .NET Framework Version:2.0.50727.3053;
ASP.NET Version:2.0.50727.3053

Peer Jakobsen (AdWords API Guru)

unread,
Dec 23, 2008, 6:03:46 AM12/23/08
to AdWords API Forum
Hello,

We are also using .NET and have all our webservices reference file
inherit from a class that handle a couple of connection problems that
you will experience in .NET. Here is the code for that class


Imports System
Imports System.Net
Imports System.Web.Services
Imports System.Web.Services.Description
Imports System.Web.Services.Protocols

Public Class SoapHttpClientProtocolBase
Inherits SoapHttpClientProtocol

Protected Overrides Function GetWebRequest(ByVal Uri As Uri) As
WebRequest
' The following settings are put in place
' to help avoid connection issues
System.Net.ServicePointManager.Expect100Continue = False
Dim webRequest As HttpWebRequest = CType(MyBase.GetWebRequest
(Uri), HttpWebRequest)
webRequest.KeepAlive = False
Return webRequest
End Function

Protected Overloads Function Invoke(ByVal methodName As String,
ByVal parameters As Object()) As Object()
Dim MAX_CONNECTION_ATTEMPTS As Integer = 6

Dim connectionAttempts As Integer = 0
Dim connectionSucceeded As Boolean = False
Dim invokeReturn As Object() = Nothing

While ((connectionAttempts <= MAX_CONNECTION_ATTEMPTS) And
(Not connectionSucceeded))
Try
connectionAttempts += 1
invokeReturn = MyBase.Invoke(methodName, parameters)
connectionSucceeded = True
Catch ex As WebException
If connectionAttempts >= MAX_CONNECTION_ATTEMPTS Then
Throw ex
End If
End Try
End While
Return invokeReturn
End Function
End Class




You would then use this class as base class for you criterion service
reference class. Example:

Partial Public Class CriterionService
Inherits outletservices.SoapHttpClientProtocolBase

Thanks
Reply all
Reply to author
Forward
0 new messages