Make git-cookie-authdaemon more robust.

Once the daemon has backgrounded itself, retry fetching the cookie if
there's an error, rather than just allowing the daemon to crash. Also,
throttle the minimum interval between new attempts to fetch the cookie,
as otherwise if the server returns a cookie that expires in less than
REFRESH seconds, the script will keep trying to refresh the cookie as
fast as it possibly can until it gets a new one, which triggers DoS
prevention limits.

Failures on the initial cookie fetch are not retried, as the daemon is
still in the foreground at that point and hopefully either a human or a
logging process is watching it.
diff --git a/git-cookie-authdaemon b/git-cookie-authdaemon
index f947d44..101f90b 100755
--- a/git-cookie-authdaemon
+++ b/git-cookie-authdaemon
@@ -17,6 +17,7 @@
 import urllib2
 
 REFRESH = 300  # seconds remaining when starting refresh
+RETRY_INTERVAL = 5 # seconds between retrying a failed refresh
 
 A = 'http://metadata/0.1/meta-data/service-accounts/default/acquire'
 S = 'https://www.googleapis.com/auth/gerritcodereview'
@@ -37,11 +38,18 @@
     'http.cookiefile', COOKIE_JAR
   ])
 
-def acquire_token():
-  return json.load(urllib2.urlopen(A + '?scopes=' + S))
+def acquire_token(retry):
+  while True:
+    try:
+      with urllib2.urlopen(A + '?scopes=' + S) as token:
+        return json.load(token)
+    except urllib2.URLError:
+      if not retry:
+        raise
+    time.sleep(RETRY_INTERVAL)
 
-def update_cookie():
-  token = acquire_token()
+def update_cookie(retry):
+  token = acquire_token(retry)
   expires = token['expiresAt']
 
   tmp_jar = COOKIE_JAR + '.lock'
@@ -78,14 +86,15 @@
   expires = expires - REFRESH
   while True:
     now = time.time()
+    expires = max(expires, now + RETRY_INTERVAL)
     while now < expires:
       time.sleep(expires - now)
       now = time.time()
-    expires = update_cookie() - REFRESH
+    expires = update_cookie(retry=True) - REFRESH
 
 def main():
   configure_git()
-  expires = update_cookie()
+  expires = update_cookie(retry=False)
 
   if os.fork() > 0:
     sys.exit(0)