]> www.vanbest.org Git - motioneye-debian/commitdiff
(hopefully) fixed the too many remote snapshot requests
authorCalin Crisan <ccrisan@gmail.com>
Sat, 19 Oct 2013 12:56:51 +0000 (15:56 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 19 Oct 2013 12:58:16 +0000 (15:58 +0300)
doc/todo.txt
src/remote.py

index e7ad0eaf16cdc6def4d262ce882a845098920157..399df2de5afce29a76b3393572fcd3f3501eee09 100644 (file)
@@ -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
index 0a3d9cca51cdee05b5795a70c381efa4919ed099..870accc0589515a2b967d93f42a07c8927c76288 100644 (file)
@@ -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)