summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/backend
diff options
context:
space:
mode:
authorIvan Alejandro <ivanalejandro0@gmail.com>2014-07-24 12:54:12 -0300
committerIvan Alejandro <ivanalejandro0@gmail.com>2014-12-19 17:15:55 -0300
commitdf160c0d44e8d0439d54313f097b2a4d9ada7357 (patch)
treec3d2cb9f68f696bc9377dbaf20897cf0a560afde /src/leap/bitmask/backend
parent30b02e9153b21d177bf0f79e7132157bf25b636d (diff)
Allow frontend and backend to be run separately.
Add the 'check_online' method to check whether the backend is accessible or not. Reduce the wait for running threads timeout on quit. Add retry feature to the backend requests send.
Diffstat (limited to 'src/leap/bitmask/backend')
-rw-r--r--src/leap/bitmask/backend/backend.py2
-rw-r--r--src/leap/bitmask/backend/backend_proxy.py30
-rw-r--r--src/leap/bitmask/backend/signaler.py1
3 files changed, 30 insertions, 3 deletions
diff --git a/src/leap/bitmask/backend/backend.py b/src/leap/bitmask/backend/backend.py
index 37535f37..75eff8a9 100644
--- a/src/leap/bitmask/backend/backend.py
+++ b/src/leap/bitmask/backend/backend.py
@@ -135,7 +135,7 @@ class Backend(object):
i.e.:
use threads.deferToThread(this_method) instead of this_method()
"""
- wait_max = 5 # seconds
+ wait_max = 3 # seconds
wait_step = 0.5
wait = 0
while self._ongoing_defers and wait < wait_max:
diff --git a/src/leap/bitmask/backend/backend_proxy.py b/src/leap/bitmask/backend/backend_proxy.py
index e2611251..9de3501e 100644
--- a/src/leap/bitmask/backend/backend_proxy.py
+++ b/src/leap/bitmask/backend/backend_proxy.py
@@ -67,6 +67,7 @@ class BackendProxy(object):
socket.curve_serverkey = public
socket.setsockopt(zmq.RCVTIMEO, 1000)
+ socket.setsockopt(zmq.LINGER, 0) # Terminate early
socket.connect(self.SERVER)
self._socket = socket
@@ -75,8 +76,23 @@ class BackendProxy(object):
self._call_queue = Queue.Queue()
self._worker_caller = threading.Thread(target=self._worker)
+
+ def start(self):
self._worker_caller.start()
+ def check_online(self):
+ """
+ Return whether the backend is accessible or not.
+ You don't need to do `run` in order to use this.
+
+ :rtype: bool
+ """
+ # we use a small timeout in order to response quickly if the backend is
+ # offline
+ self._send_request(PING_REQUEST, retry=False, timeout=500)
+ self._socket.close()
+ return self.online
+
def _worker(self):
"""
Worker loop that processes the Queue of pending requests to do.
@@ -150,7 +166,7 @@ class BackendProxy(object):
if api_method == STOP_REQUEST:
self._call_queue.put(STOP_REQUEST)
- def _send_request(self, request):
+ def _send_request(self, request, retry=True, timeout=None):
"""
Send the given request to the server.
This is used from a thread safe loop in order to avoid sending a
@@ -158,6 +174,10 @@ class BackendProxy(object):
:param request: the request to send.
:type request: str
+ :param retry: whether we should retry or not in case of timeout.
+ :type retry: bool
+ :param timeout: a custom timeout (milliseconds) to wait for a response.
+ :type timeout: int
"""
# logger.debug("Sending request to backend: {0}".format(request))
self._socket.send(request)
@@ -166,10 +186,16 @@ class BackendProxy(object):
poll.register(self._socket, zmq.POLLIN)
reply = None
+
tries = 0
+ if not retry:
+ tries = self.POLL_TRIES + 1 # this means: no retries left
+
+ if timeout is None:
+ timeout = self.POLL_TIMEOUT
while True:
- socks = dict(poll.poll(self.POLL_TIMEOUT))
+ socks = dict(poll.poll(timeout))
if socks.get(self._socket) == zmq.POLLIN:
reply = self._socket.recv()
break
diff --git a/src/leap/bitmask/backend/signaler.py b/src/leap/bitmask/backend/signaler.py
index 574bfa71..43cba994 100644
--- a/src/leap/bitmask/backend/signaler.py
+++ b/src/leap/bitmask/backend/signaler.py
@@ -60,6 +60,7 @@ class Signaler(object):
socket.curve_serverkey = public
socket.setsockopt(zmq.RCVTIMEO, 1000)
+ socket.setsockopt(zmq.LINGER, 0) # Terminate early
socket.connect(self.SERVER)
self._socket = socket