VM concurrency

This commit is contained in:
Julien Duponchelle 2015-02-24 21:53:38 +01:00
parent 9153b42b9d
commit 46b348e46a
2 changed files with 28 additions and 2 deletions

View File

@ -20,6 +20,8 @@ from ...schemas.version import VERSION_SCHEMA
from ...version import __version__
from aiohttp.web import HTTPConflict
import asyncio
class VersionHandler:
@ -45,3 +47,12 @@ class VersionHandler:
if request.json["version"] != __version__:
raise HTTPConflict(text="Client version {} differs with server version {}".format(request.json["version"], __version__))
response.json({"version": __version__})
@staticmethod
@Route.get(
r"/sleep/{vm_id}",
description="Retrieve the server version number",
output=VERSION_SCHEMA)
def sleep(request, response):
yield from asyncio.sleep(1)
response.json({"version": __version__})

View File

@ -63,6 +63,8 @@ class Route(object):
_routes = []
_documentation = {}
_vms_lock = {}
@classmethod
def get(cls, path, *args, **kw):
return cls._route('GET', path, *args, **kw)
@ -150,9 +152,22 @@ class Route(object):
return response
cls._routes.append((method, cls._path, control_schema))
@asyncio.coroutine
def vm_concurrency(request):
"""
To avoid strange effect we prevent concurrency
between the same instance of the vm
"""
return control_schema
if "vm_id" in request.match_info:
cls._vms_lock.setdefault(request.match_info["vm_id"], asyncio.Lock())
with (yield from cls._vms_lock[request.match_info["vm_id"]]):
response = yield from control_schema(request)
return response
cls._routes.append((method, cls._path, vm_concurrency))
return vm_concurrency
return register
@classmethod