0

I am making Google API request through application using RestClient library to get address.

Sample request code-

require 'rest-client'
require 'json'

gmaps_api_href = "https://maps.googleapis.com/maps/api/geocode/json?latlng=18.56227673,73.76804232&language=ar"
response = RestClient.get gmaps_api_href
result = JSON.parse(response)['results']

This request works fine on my local machine and it completes within 1-2secs. But on production instance it takes 20secs to finish one request.

Due to some security measures, we can not access production instance directly. So I am unable to find pin point for this delay. After doing trial and error, we found that

  1. If we make request using CURL, it takes 1 sec on server
  2. If we make request using Net::HTTP, it takes 20sec to complete same as we were observed for RestClient.
  3. If we make request using WebRequest in small .net app, that request complete within 1 secs.

Its difficult for me to get difference between above observations.

Please let me know why it is so? and what changes I have to do to make it work in my Rails App?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
kd12
  • 1,291
  • 1
  • 13
  • 22
  • 1
    Related questions suggest to me that might be a DNS issue on your server: http://stackoverflow.com/questions/29945204/nethttp-extremely-slow-responses-for-https-requests http://stackoverflow.com/questions/9939726/ruby-net-http-opening-connection-very-slow – Jordan Running Jan 19 '16 at 06:40
  • Thanks Jordan!! One more thing why WebRequest works fine on server? Is there any difference? – kd12 Jan 19 '16 at 06:42
  • There are certainly differences between Ruby's Net::HTTP and .NET's WebRequest, just as there are differences between Net::HTTP and cURL. What those differences are, though, I couldn't say. – Jordan Running Jan 19 '16 at 06:44
  • I did the changes accordingly but no output :-(. It is still taking 20 secs.. – kd12 Jan 20 '16 at 10:51

1 Answers1

0

Are you using a Google API key? Your example does not show use of an API key. if not, I'd guess you are getting rate-limited by Google. On your server, you've probably already deployed a version of this app, which made lots of requests to Google without an API key in the fairly recent past, and Google noticed and it's rate-limiting software may be slowing down your requests made from that server. While your local machine hasn't in the past made an enormous amount of requests to the google api, so is not being rate-limited by google's servers.

It's possible Google's rate-limiting is paying some attention (for now!) to User-Agent, and the different user-agent sent by Curl somehow evades Google's rate-limiting that was triggered by the requests sent by RestClient with it's User-Agent (and RestClient may use net-http under the hood, and have the same User-Agent as it).

While one would hope that if you were rate-limited you'd get a "429 Too Many Requests" error response instead of just a slow response, it's possible RestClient hides this from you (I haven't used RestClient), and I've also seen some unpredictable behavior from Google rate-limiting defenses, especially when not using an API key on a service that requires one for all but a few sample requests. I have seen things similar to what you report in that case.

My guess is you're being rate limited because you are not using an API key. Get and use an API key from Google. Google still has rate limits when you are using an API key, but they are clearly advertised (for free? 2500 per-day, and no more than 10 per second. more if you pay) and should give more clear and predictable error messages when exceeded. That's part of why Google requires the api key, so they can reliably rate-limit you in clear ways.

https://developers.google.com/maps/documentation/geocoding/usage-limits https://developers.google.com/maps/documentation/geocoding/intro#BYB

jrochkind
  • 22,799
  • 12
  • 59
  • 74
  • We have made subscription for extra usage limit. Here, usuage limit is not the problem. Request is taking huge delay than our testing environment. – kd12 Jan 20 '16 at 10:54
  • are you on a university/corporate 'enterprise' network? Could your local network firewalls and protections being rate-limiting you? – jrochkind Jan 20 '16 at 12:48