Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary exponential backoff to _flush_writes #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions sheetsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import logging
import httplib2 # pip install httplib2
import time
import random
from datetime import datetime
import json

Expand Down Expand Up @@ -630,19 +632,28 @@ def _flush_writes(self):
if self._batch_request:
logger.info("_flush_writes: Writing %s cell writes",
len(self._batch_request))
try:
self.worksheet.update_cells(self._batch_request)
except Exception, e:
logger.exception("gdata API error. %s", e)
raise e

# Now check the response code.
#for entry in resp.entry:
# if entry.batch_status.code != '200':
# error = "gdata API error. %s - %s" % (entry.batch_status.code,
# entry.batch_status.reason)
# logger.error("Batch update failed: %s", error)
# raise Exception(error)
for i in xrange(0, 12):
try:
self.worksheet.update_cells(self._batch_request)

# If the update completed successfully, break out of the
# backoff loop.
break
except Exception, e:
gdata_log.error("gdata API error. %s", e)

if i <= 10:
# Seed the backoff wait time with a random value -
# probably unnecessary, but might fix collision issues
# if running multiple sync instances.
wait_duration = 2**i + random.random()
logger.info("Error updating cells, re-trying after " +
"%s second backoff." % str(wait_duration))
time.sleep(wait_duration)
else:
# Backoff failed after a long time, re-raise the error.
raise e

self._batch_request = []

Expand Down