summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2013-12-19 21:52:12 -0400
committerKali Kaneko <kali@leap.se>2013-12-19 22:08:26 -0400
commit7a6d913fed8fc8332174c7ac5c8a4a5472a478c3 (patch)
treeec31ec578b431111b81b077ae070578d2b795dca
parentcb616b80c47a5c251f4c66808396d0ac1352a390 (diff)
Do not instantiate the synchronizer each time.
This has the nice effect of letting the persistent-connection reuse the existing connection, avoiding the ssl handshake overhead each time we try to synchronize. This can be traced by logging the instantiation of HttpClientBase in u1db.remote I *think* we should be fine with the timeouts as long as we keep the sync period along the 1 min we are doing now. For other cases, we should look into how to override the default timeout in httplib (used by u1db http_client).
-rw-r--r--client/changes/bug_reuse-http-connection2
-rw-r--r--client/src/leap/soledad/client/sqlcipher.py43
-rw-r--r--client/src/leap/soledad/client/target.py1
3 files changed, 36 insertions, 10 deletions
diff --git a/client/changes/bug_reuse-http-connection b/client/changes/bug_reuse-http-connection
new file mode 100644
index 00000000..c6cdd9b4
--- /dev/null
+++ b/client/changes/bug_reuse-http-connection
@@ -0,0 +1,2 @@
+ o Fix a bug in soledad.client.sqlcipher by which we were creating
+ a new connection for each sync.
diff --git a/client/src/leap/soledad/client/sqlcipher.py b/client/src/leap/soledad/client/sqlcipher.py
index c7aebbe6..5695bf82 100644
--- a/client/src/leap/soledad/client/sqlcipher.py
+++ b/client/src/leap/soledad/client/sqlcipher.py
@@ -49,10 +49,12 @@ import time
import string
import threading
-from u1db.backends import sqlite_backend
-from u1db import errors
from pysqlcipher import dbapi2
+from u1db.backends import sqlite_backend
+from u1db.sync import Synchronizer
from u1db import errors as u1db_errors
+
+from leap.soledad.client.target import SoledadSyncTarget
from leap.soledad.common.document import SoledadDocument
logger = logging.getLogger(__name__)
@@ -144,6 +146,7 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase):
_index_storage_value = 'expand referenced encrypted'
k_lock = threading.Lock()
+ _syncer = None
def __init__(self, sqlcipher_file, password, document_factory=None,
crypto=None, raw_key=False, cipher='aes-256-cbc',
@@ -336,13 +339,33 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase):
:return: The local generation before the synchronisation was performed.
:rtype: int
"""
- from u1db.sync import Synchronizer
- from leap.soledad.client.target import SoledadSyncTarget
- return Synchronizer(
- self,
- SoledadSyncTarget(url,
- creds=creds,
- crypto=self._crypto)).sync(autocreate=autocreate)
+ if not self.syncer:
+ self._create_syncer(url, creds=creds)
+ return self.syncer.sync(autocreate=autocreate)
+
+ @property
+ def syncer(self):
+ """
+ Accesor for synchronizer.
+ """
+ return self._syncer
+
+ def _create_syncer(self, url, creds=None):
+ """
+ Creates a synchronizer
+
+ :param url: The url of the target replica to sync with.
+ :type url: str
+ :param creds: optional dictionary giving credentials.
+ to authorize the operation with the server.
+ :type creds: dict
+ """
+ if self._syncer is None:
+ self._syncer = Synchronizer(
+ self,
+ SoledadSyncTarget(url,
+ creds=creds,
+ crypto=self._crypto))
def _extra_schema_init(self, c):
"""
@@ -719,7 +742,7 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase):
definition = self._get_index_definition(index_name)
if len(key_values) != len(definition):
- raise errors.InvalidValueForIndex()
+ raise u1db_errors.InvalidValueForIndex()
tables = ["document_fields d%d" % i for i in range(len(definition))]
novalue_where = ["d.doc_id = d%d.doc_id"
" AND d%d.field_name = ?"
diff --git a/client/src/leap/soledad/client/target.py b/client/src/leap/soledad/client/target.py
index 73f719fb..3b3d6870 100644
--- a/client/src/leap/soledad/client/target.py
+++ b/client/src/leap/soledad/client/target.py
@@ -63,6 +63,7 @@ logger = logging.getLogger(__name__)
# Exceptions
#
+
class DocumentNotEncrypted(Exception):
"""
Raised for failures in document encryption.