Performance improvement - Only reset backends loaded into memory (#4274)

This commit is contained in:
Bert Blommers 2021-09-15 21:53:31 +00:00 committed by GitHub
parent a147eeac67
commit 3498689d9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 9 deletions

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import importlib
import moto
import sys
decorators = [
@ -26,8 +27,16 @@ def backends():
yield _import_backend(module_name, backends_name)
def named_backends():
for name, (module_name, backends_name) in BACKENDS.items():
def loaded_backends():
loaded_modules = sys.modules.keys()
loaded_modules = [m for m in loaded_modules if m.startswith("moto.")]
imported_backends = [
name
for name, (module_name, _) in BACKENDS.items()
if f"moto.{module_name}" in loaded_modules
]
for name in imported_backends:
module_name, backends_name = BACKENDS[name]
yield name, _import_backend(module_name, backends_name)

View File

@ -840,7 +840,7 @@ class MotoAPIBackend(BaseBackend):
def reset(self):
import moto.backends as backends
for name, backends_ in backends.named_backends():
for name, backends_ in backends.loaded_backends():
if name == "moto_api":
continue
for region_name, backend in backends_.items():

View File

@ -74,13 +74,13 @@ class MediaPackageBackend(BaseBackend):
def __init__(self, region_name=None):
super(MediaPackageBackend, self).__init__()
self.region_name = region_name
self._channels = OrderedDict()
self._origin_endpoints = OrderedDict()
def reset(self):
region_name = self.region_name
self.__dict__ = {}
self.__init__(region_name)
self._channels = OrderedDict()
self._origin_endpoints = OrderedDict()
def create_channel(self, description, id, tags):
arn = "arn:aws:mediapackage:channel:{}".format(id)

View File

@ -68,10 +68,13 @@ class DomainDispatcherApplication(object):
if pattern.match("http://%s" % host):
return backend
print(
"Unable to find appropriate URL for {}."
"Remember to add the URL to urls.py, and run script/update_backend_index.py to index it."
)
if "amazonaws.com" in host:
print(
"Unable to find appropriate backend for {}."
"Remember to add the URL to urls.py, and run script/update_backend_index.py to index it.".format(
host
)
)
def infer_service_region_host(self, environ):
auth = environ.get("HTTP_AUTHORIZATION")