[autotest] Allow suite scheduler to set time out for RPC.

When an RPC server is under heavy load, long RPC calls like
create_suite_job might time out. However the call might still
successfully create suite job even though the caller received an
socket.timeout exception and retry the call. That leads to multiple
suites be created.

This change allows the create_suite_job call to pass in a minimum value
of timeout, to reduce the flake of the RPC. The same change is applied
to get_jobs and get_hostnames call, which may take longer than the
default 6s timeout.

BUG=chromium:591538
TEST=local suite schedule run, unittest, local AFE

Change-Id: If7b888fe6ca80f5c7e705026e06b883cef0bfdc4
Reviewed-on: https://chromium-review.googlesource.com/330463
Commit-Ready: Dan Shi <dshi@chromium.org>
Tested-by: Dan Shi <dshi@google.com>
Reviewed-by: Simran Basi <sbasi@chromium.org>
diff --git a/frontend/afe/json_rpc/proxy.py b/frontend/afe/json_rpc/proxy.py
index 2c51727..1e1df4c 100644
--- a/frontend/afe/json_rpc/proxy.py
+++ b/frontend/afe/json_rpc/proxy.py
@@ -20,6 +20,7 @@
 """
 
 import os
+import socket
 import urllib2
 from autotest_lib.client.common_lib import error as exceptions
 
@@ -99,12 +100,21 @@
         return ServiceProxy(self.__serviceURL, name, self.__headers)
 
     def __call__(self, *args, **kwargs):
+        # Caller can pass in a minimum value of timeout to be used for urlopen
+        # call. Otherwise, the default socket timeout will be used.
+        min_rpc_timeout = kwargs.pop('min_rpc_timeout', None)
         postdata = json_encoder_class().encode({'method': self.__serviceName,
                                                 'params': args + (kwargs,),
                                                 'id': 'jsonrpc'})
         request = urllib2.Request(self.__serviceURL, data=postdata,
                                   headers=self.__headers)
-        respdata = urllib2.urlopen(request).read()
+        default_timeout = socket.getdefaulttimeout()
+        if not default_timeout:
+            # If default timeout is None, socket will never time out.
+            respdata = urllib2.urlopen(request).read()
+        else:
+            timeout = max(min_rpc_timeout, default_timeout)
+            respdata = urllib2.urlopen(request, timeout=timeout).read()
         try:
             resp = decoder.JSONDecoder().decode(respdata)
         except ValueError: