question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

apiclient.discovery build slow in certain servers

See original GitHub issue

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you’ve tried the usual “quick fixes”:

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS type and version: Multiple linux distros
  • Python version: 2.7
  • pip version: 18.1
  • google-api-python-client version: 1.9.3

Steps to reproduce

  1. Run apiclient.discovery.build on different servers
  2. In some servers, the build process takes over 10 minutes just to return the client

Code example

from apiclient.discovery import build
build(service, version, credentials=credentials)

Stack trace

This is a sudden error that started to occur a few weeks ago. I have several servers where I run googleapis’ build method, but I’ve started to notice that in some of them, without any change from my end, the build process started to take a really long time and even timeout in some cases. I have started to use build_from_document instead of build, with a discovery document stored internally (even though the added code just to store the document seems too much for an error that is pretty inexplicable). However, there is another bottleneck in this process, which is the “credentials” part of the build function, which is taking a long time to complete as well and, since this is a google-maintained library which methods are used internally for the downstream code, I have not yet found a work around for it. I have updated httplib2 to the latest version and googleapis as well, but the slowness and timeout continued for the affected servers - it is important to notice that the “slow” servers have the exact same configuration as the “fast” servers - using the same GCP project and client tokens. This is happening regardless of the service (tested with bigquery, drive, analytics (v3 and v4), sheets, and google search console). Besides using build_from_document, I also raised the socket timeout (socket.setdefaulttimeout) before calling the build method to prevent timeout exceptions, but the slowness is a major issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
partheacommented, Mar 3, 2021

Hi @thassyo-kondado,

We’ve just released version 2.0 of google-api-python-client. One of the key features is that discovery.build() will retrieve discovery documents from the client library itself rather than fetching them over the internet. Please try version 2.0 and let us know if you’re still experiencing an issue. I’m going to close off this issue due to inactivity. If you’re still having trouble, please re-open it with more information.

0reactions
Logic-gatecommented, Oct 15, 2020

UPDATE I have solved this issue…spent way too long it. googleapis.com hangs on ipv6. The solution forces ipv4.

#Fix for ipv6 hanging on googleapis.com
#source https://www.xspdf.com/help/50044152.html
old_getaddrinfo = socket.getaddrinfo
def new_getaddrinfo(*args, **kwargs):
    responses = old_getaddrinfo(*args, **kwargs)
    return [response
            for response in responses
            if response[0] == socket.AF_INET]
socket.getaddrinfo = new_getaddrinfo

EOF

UPDATE: WRONG I am beginning to suspect an issue related SSL certificates and it’s handling in python 3.8. EOF

I am facing the same exact issue and I’ve noticed that the connection fails 4 times every-time before the connection is made. At first I thought it was an issue with my code, but while testing with httplib2– GET request to googleapis.com the same thing happens. I then tried requests…same thing. I tried curl and it worked. Mind you it’s not a network issue, I tried the same script on various devices on the same connection and same versions of modules all worked just fine. Plus, curl worked.

Would appreciate any feedback…

credentialsCheck::tokenFound::/home/nemo/.config/harbour-cargo/token.pickle
credentialsCheck::goto::testFunction
serviceBuild::starting
serviceBuild::serviceBuilding
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect: (www.googleapis.com, 443)
...
...
...
reply: 'HTTP/1.1 200 OK\r\n'
header: Expires: Wed, 14 Oct 2020 14:53:40 GMT
header: Date: Wed, 14 Oct 2020 14:48:40 GMT
header: Cache-Control: public, max-age=300, must-revalidate, no-transform
header: Vary: Origin
header: Vary: X-Origin
header: Content-Type: application/json; charset=UTF-8
header: Content-Encoding: gzip
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: Content-Security-Policy: frame-ancestors 'self'
header: X-XSS-Protection: 1; mode=block
header: Server: GSE
header: Alt-Svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
header: Transfer-Encoding: chunked
...
...
...
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect fail: (www.googleapis.com, 443)
connect: (www.googleapis.com, 443)
...
...
...
reply: 'HTTP/1.1 200 OK\r\n'
header: Expires: Wed, 14 Oct 2020 14:50:46 GMT
header: Date: Wed, 14 Oct 2020 14:50:46 GMT
header: Cache-Control: private, max-age=0, must-revalidate, no-transform
header: Vary: Origin
header: Vary: X-Origin
header: Content-Type: application/json; charset=UTF-8
header: Content-Encoding: gzip
header: X-Content-Type-Options: nosniff
header: X-Frame-Options: SAMEORIGIN
header: Content-Security-Policy: frame-ancestors 'self'
header: X-XSS-Protection: 1; mode=block
header: Server: GSE
header: Alt-Svc: h3-Q050=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
header: Transfer-Encoding: chunked
testFunction::resultsParse::items

Read more comments on GitHub >

github_iconTop Results From Across the Web

Google API client is really slow when building the drive v2 API
It is often quicker than initialising objects from scratch, especially for objects that require initialisation through some sort of network data ...
Read more >
Improve performance | Google Drive
This document covers some techniques you can use to improve the performance of your application. In some cases, examples from other APIs or...
Read more >
googleapiclient.discovery - Google APIs
Create a Resource for interacting with an API. Same as `build()`, but constructs the Resource object from a discovery document that is it...
Read more >
Improve performance for InstantSearch.js - Algolia
On this page · 1. Preparing the connection to Algolia · 2. Mitigate the impact of a slow network on your application ·...
Read more >
API performance tips | BigQuery - Google Cloud
If the field you select is (or is part of) an array, the server returns the selected portion of all elements in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found