Lock same api routes

This commit is contained in:
Ruud
2013-09-01 00:24:47 +02:00
parent 1e5d6bad2a
commit af2876bd71
2 changed files with 16 additions and 8 deletions
+3 -3
View File
@@ -25,7 +25,7 @@ class WebHandler(RequestHandler):
if not views.get(route):
page_not_found(self)
return
self.finish(views[route]())
self.write(views[route]())
def addView(route, func, static = False):
views[route] = func
@@ -64,7 +64,7 @@ class KeyHandler(RequestHandler):
if (self.get_argument('u') == md5(username) or not username) and (self.get_argument('p') == password or not password):
api = Env.setting('api_key')
self.finish({
self.write({
'success': api is not None,
'api_key': api
})
@@ -81,5 +81,5 @@ def page_not_found(rh):
time.sleep(0.1)
rh.set_status(404)
rh.finish('Wrong API key used')
rh.write('Wrong API key used')
+13 -5
View File
@@ -4,10 +4,12 @@ from threading import Thread
from tornado.gen import coroutine
from tornado.web import RequestHandler, asynchronous
import json
import threading
import tornado
import urllib
api = {}
api_locks = {}
api_nonblock = {}
api_docs = {}
@@ -39,7 +41,7 @@ class NonBlockHandler(RequestHandler):
if self.request.connection.stream.closed():
return
self.finish(response)
self.write(response)
def on_connection_close(self):
@@ -63,9 +65,11 @@ class ApiHandler(RequestHandler):
def get(self, route, *args, **kwargs):
route = route.strip('/')
if not api.get(route):
self.finish('API call doesn\'t seem to exist')
self.write('API call doesn\'t seem to exist')
return
api_locks[route].acquire()
kwargs = {}
for x in self.request.arguments:
kwargs[x] = urllib.unquote(self.get_argument(x))
@@ -88,16 +92,20 @@ class ApiHandler(RequestHandler):
jsonp_callback = self.get_argument('callback_func', default = None)
if jsonp_callback:
self.finish(str(jsonp_callback) + '(' + json.dumps(result) + ')')
self.write(str(jsonp_callback) + '(' + json.dumps(result) + ')')
elif isinstance(result, (tuple)) and result[0] == 'redirect':
self.redirect(result[1])
else:
self.finish(result)
self.write(result)
api_locks[route].release()
def addApiView(route, func, static = False, docs = None, **kwargs):
if static: func(route)
else: api[route] = func
else:
api[route] = func
api_locks[route] = threading.Lock()
if docs:
api_docs[route[4:] if route[0:4] == 'api.' else route] = docs