From: Calin Crisan Date: Sat, 19 Oct 2013 12:56:51 +0000 (+0300) Subject: (hopefully) fixed the too many remote snapshot requests X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=d267598bc012d1fb90874058b2e73c775ff99936;p=motioneye-debian (hopefully) fixed the too many remote snapshot requests --- diff --git a/doc/todo.txt b/doc/todo.txt index e7ad0ea..399df2d 100644 --- a/doc/todo.txt +++ b/doc/todo.txt @@ -1,5 +1,3 @@ --> http clients don't die: max_clients limit reached, request queued. 10 active, 217 queued requests. - -> add an autoupdate mechanism -> style scroll bars diff --git a/src/remote.py b/src/remote.py index 0a3d9cc..870accc 100644 --- a/src/remote.py +++ b/src/remote.py @@ -4,6 +4,10 @@ import logging from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest +import settings + +_snapshot_cache = {} + def _make_request(host, port, username, password, uri, method='GET', data=None, query=None): url = '%(scheme)s://%(host)s:%(port)s%(uri)s' % { @@ -15,7 +19,8 @@ def _make_request(host, port, username, password, uri, method='GET', data=None, if query: url += '?' + '='.join(query.items()) - request = HTTPRequest(url, method, body=data, auth_username=username, auth_password=password) + request = HTTPRequest(url, method, body=data, auth_username=username, auth_password=password, + request_timeout=settings.REMOTE_REQUEST_TIMEOUT) return request @@ -141,6 +146,8 @@ def set_preview(host, port, username, password, camera_id, controls, callback): def current_snapshot(host, port, username, password, camera_id, callback): + global _snapshot_cache + logging.debug('getting current snapshot for remote camera %(id)s on %(host)s:%(port)s' % { 'id': camera_id, 'host': host, @@ -148,7 +155,14 @@ def current_snapshot(host, port, username, password, camera_id, callback): request = _make_request(host, port, username, password, '/snapshot/%(id)s/current/' % {'id': camera_id}) + cached = _snapshot_cache.setdefault(request.url, {'pending': 0, 'jpg': None}) + if cached['pending'] > 0: # a pending request for this snapshot exists + return callback(cached['jpg']) + def on_response(response): + cached['pending'] -= 1 + cached['jpg'] = response.body + if response.error: logging.error('failed to get current snapshot for remote camera %(id)s on %(host)s:%(port)s: %(msg)s' % { 'id': camera_id, @@ -160,5 +174,7 @@ def current_snapshot(host, port, username, password, camera_id, callback): callback(response.body) + cached['pending'] += 1 + http_client = AsyncHTTPClient() http_client.fetch(request, on_response)