Search code examples
google-app-engineconnection-timeout

Google App Engine connection request timeout error


I am working on a GAE web app which shows movie related data.
To get the movie data I am using API from OMDB (http://www.omdbapi.com/) .Below is the code snippet I use to connect to the API.
When i run it locally it works perfectly fine, but doesn't work when deployed on GAE. It throws connection timeout exception, i tried increasing connection timeout period but that didn't work.

String URLstr = "http://www.omdbapi.com/?t="+URLEncoder.encode(Request,"utf-8");

        URL url=null;

        URLConnection uc = null;
        BufferedReader bf = null;
        try {
            url= new URL(URLstr);
            uc = url.openConnection();
            uc.setConnectTimeout(15* 1000);
            bf = new BufferedReader(new InputStreamReader(uc.getInputStream()));

        } catch (IOException e) {
            throw new IllegalArgumentException(e.getMessage());
        }

Is my code incorrect? are there some restrictions with GAE that i missed?


Solution

  • Your code looks correct. I am having the exact same issue with OMDB API and Google App Engine as of a few weeks ago. I reached out to Brian who runs OMDB API regarding this and I think it had to do with the App Engine IP range being blocked because of abuse a few weeks ago.

    I created the following webapp to figure out what external IP address the url fetch from my app was showing up as to the OMDB servers. I deployed the following to GAE to get the public IP.

    import webapp2
    import logging
    
    from google.appengine.api import urlfetch
    
    class ifconfig(webapp2.RequestHandler):
        def get(self):
            url="http://ipecho.net/plain"
            urlfetch.set_default_fetch_deadline(60)
            result = urlfetch.fetch(url)
            logging.debug("I think my external IP is %s " % result.content)
            self.response.write(result.content)
    
    app = webapp2.WSGIApplication([
        ('/ifconfig', ifconfig)
    ])
    

    In Google App Engine, I went to the instances tab and shutdown the instance, and checked what external IP the new instance had. I did this several times, and in my case it seemed like the external IPs were all coming from 107.178.195.0/24, so I provided this information to OMDB API.

    I guess this was in the banned IP block, and Brian was able to unblock that range. This fixed my issue and requests to the API started working again.

    This possibly might have resolved the issue for you as well, but if it didn't, you might want to figure out what your public IP is and reach out to Brian to see if it's in an IP range that's being blocked