Improve performance of etag calculation for /
- Only compute etag once (rather than twice) per request. - Only stat files once per request. - Avoid calling `isdir` or `isfile` in `collect_files`. - Leave it to `compute_lastmodified` to stat files, which tells us if the path exists, if it is a file, and it's last modified. - Reduces the number of stat systemcalls by 50%.
This commit is contained in:
1 parent
93f78efed4
commit
01574aadfe
2 files changed
+40
-53
No files matched your search
@@ -242,19 +242,6 @@ def index():
|
||||
def cache_key():
|
||||
return _cache_key(key, additional_key_data=additional_key_data)
|
||||
|
||||
def check_etag_and_lastmodified():
|
||||
files = collect_files()
|
||||
lastmodified = compute_lastmodified(files)
|
||||
lastmodified_ok = util.flask.check_lastmodified(lastmodified)
|
||||
etag_ok = util.flask.check_etag(compute_etag(files=files,
|
||||
lastmodified=lastmodified,
|
||||
additional=[cache_key()] + additional_etag))
|
||||
return lastmodified_ok and etag_ok
|
||||
|
||||
def validate_cache(cached):
|
||||
etag_different = compute_etag(additional=[cache_key()] + additional_etag) != cached.get_etag()[0]
|
||||
return force_refresh or etag_different
|
||||
|
||||
def collect_files():
|
||||
if callable(custom_files):
|
||||
try:
|
||||
@@ -264,13 +251,11 @@ def index():
|
||||
except Exception:
|
||||
_logger.exception("Error while trying to retrieve tracked files for plugin {}".format(key))
|
||||
|
||||
templates = _get_all_templates()
|
||||
assets = _get_all_assets()
|
||||
translations = _get_all_translationfiles(g.locale.language if g.locale else "en",
|
||||
files = _get_all_templates()
|
||||
files += _get_all_assets()
|
||||
files += _get_all_translationfiles(g.locale.language if g.locale else "en",
|
||||
"messages")
|
||||
|
||||
files = templates + assets + translations
|
||||
|
||||
if callable(additional_files):
|
||||
try:
|
||||
af = additional_files()
|
||||
@@ -281,7 +266,7 @@ def index():
|
||||
|
||||
return sorted(set(files))
|
||||
|
||||
def compute_lastmodified(files=None):
|
||||
def compute_lastmodified(files):
|
||||
if callable(custom_lastmodified):
|
||||
try:
|
||||
lastmodified = custom_lastmodified()
|
||||
@@ -290,11 +275,9 @@ def index():
|
||||
except Exception:
|
||||
_logger.exception("Error while trying to retrieve custom LastModified value for plugin {}".format(key))
|
||||
|
||||
if files is None:
|
||||
files = collect_files()
|
||||
return _compute_date(files)
|
||||
|
||||
def compute_etag(files=None, lastmodified=None, additional=None):
|
||||
def compute_etag(files, lastmodified, additional=None):
|
||||
if callable(custom_etag):
|
||||
try:
|
||||
etag = custom_etag()
|
||||
@@ -303,10 +286,6 @@ def index():
|
||||
except Exception:
|
||||
_logger.exception("Error while trying to retrieve custom ETag value for plugin {}".format(key))
|
||||
|
||||
if files is None:
|
||||
files = collect_files()
|
||||
if lastmodified is None:
|
||||
lastmodified = compute_lastmodified(files)
|
||||
if lastmodified and not isinstance(lastmodified, basestring):
|
||||
from werkzeug.http import http_date
|
||||
lastmodified = http_date(lastmodified)
|
||||
@@ -326,9 +305,22 @@ def index():
|
||||
hash_update(add)
|
||||
return hash.hexdigest()
|
||||
|
||||
current_files = collect_files()
|
||||
current_lastmodified = compute_lastmodified(current_files)
|
||||
current_etag = compute_etag(files=current_files, lastmodified=current_lastmodified,
|
||||
additional=[cache_key()] + additional_etag)
|
||||
|
||||
def check_etag_and_lastmodified():
|
||||
lastmodified_ok = util.flask.check_lastmodified(current_lastmodified)
|
||||
etag_ok = util.flask.check_etag(current_etag)
|
||||
return lastmodified_ok and etag_ok
|
||||
|
||||
def validate_cache(cached):
|
||||
return force_refresh or (current_etag != cached.get_etag()[0])
|
||||
|
||||
decorated_view = view
|
||||
decorated_view = util.flask.lastmodified(lambda _: compute_lastmodified())(decorated_view)
|
||||
decorated_view = util.flask.etagged(lambda _: compute_etag(additional=[cache_key()] + additional_etag))(decorated_view)
|
||||
decorated_view = util.flask.lastmodified(lambda _: current_lastmodified)(decorated_view)
|
||||
decorated_view = util.flask.etagged(lambda _: current_etag)(decorated_view)
|
||||
decorated_view = util.flask.cached(timeout=-1,
|
||||
refreshif=validate_cache,
|
||||
key=cache_key,
|
||||
@@ -966,9 +958,21 @@ def _compute_date_for_i18n(locale, domain):
|
||||
|
||||
|
||||
def _compute_date(files):
|
||||
# Note, we do not expect everything in 'files' to exist.
|
||||
from datetime import datetime
|
||||
timestamps = list(map(lambda path: os.stat(path).st_mtime, files)) + [0] if files else []
|
||||
max_timestamp = max(*timestamps) if timestamps else None
|
||||
import stat
|
||||
max_timestamp = 0
|
||||
for path in files:
|
||||
try:
|
||||
# try to stat file. If an exception is thrown, its because it does not exist.
|
||||
s = os.stat(path)
|
||||
if stat.S_ISREG(s.st_mode) and s.st_mtime > max_timestamp:
|
||||
# is a regular file and has a newer timestamp
|
||||
max_timestamp = s.st_mtime
|
||||
except Exception:
|
||||
# path does not exist.
|
||||
continue
|
||||
|
||||
if max_timestamp:
|
||||
# we set the micros to 0 since microseconds are not speced for HTTP
|
||||
max_timestamp = datetime.fromtimestamp(max_timestamp).replace(microsecond=0)
|
||||
@@ -994,22 +998,14 @@ def _get_all_templates():
|
||||
|
||||
def _get_all_assets():
|
||||
from octoprint.util.jinja import get_all_asset_paths
|
||||
return get_all_asset_paths(app.jinja_env.assets_environment)
|
||||
return get_all_asset_paths(app.jinja_env.assets_environment, verifyExist=False)
|
||||
|
||||
|
||||
def _get_all_translationfiles(locale, domain):
|
||||
from flask import _request_ctx_stack
|
||||
|
||||
def get_po_path(basedir, locale, domain):
|
||||
path = os.path.join(basedir, locale)
|
||||
if not os.path.isdir(path):
|
||||
return None
|
||||
|
||||
path = os.path.join(path, "LC_MESSAGES", "{domain}.po".format(**locals()))
|
||||
if not os.path.isfile(path):
|
||||
return None
|
||||
|
||||
return path
|
||||
return os.path.join(basedir, locale, "LC_MESSAGES", "{domain}.po".format(**locals()))
|
||||
|
||||
po_files = []
|
||||
|
||||
@@ -1021,13 +1017,7 @@ def _get_all_translationfiles(locale, domain):
|
||||
for name, plugin in plugins.items():
|
||||
dirs = [os.path.join(user_plugin_path, name), os.path.join(plugin.location, 'translations')]
|
||||
for dirname in dirs:
|
||||
if not os.path.isdir(dirname):
|
||||
continue
|
||||
|
||||
po_file = get_po_path(dirname, locale, domain)
|
||||
if po_file:
|
||||
po_files.append(po_file)
|
||||
break
|
||||
po_files.append(get_po_path(dirname, locale, domain))
|
||||
|
||||
# core translations
|
||||
ctx = _request_ctx_stack.top
|
||||
@@ -1035,10 +1025,7 @@ def _get_all_translationfiles(locale, domain):
|
||||
|
||||
dirs = [user_base_path, base_path]
|
||||
for dirname in dirs:
|
||||
po_file = get_po_path(dirname, locale, domain)
|
||||
if po_file:
|
||||
po_files.append(po_file)
|
||||
break
|
||||
po_files.append(get_po_path(dirname, locale, domain))
|
||||
|
||||
return po_files
|
||||
|
||||
|
||||
@@ -150,7 +150,7 @@ def get_all_template_paths(loader):
|
||||
return collect_templates_for_loader(loader)
|
||||
|
||||
|
||||
def get_all_asset_paths(env):
|
||||
def get_all_asset_paths(env, verifyExist=True):
|
||||
result = []
|
||||
|
||||
def get_paths(bundle):
|
||||
@@ -163,7 +163,7 @@ def get_all_asset_paths(env):
|
||||
r += get_paths(content[1])
|
||||
else:
|
||||
path = content[1]
|
||||
if not os.path.isfile(path):
|
||||
if verifyExist is True and not os.path.isfile(path):
|
||||
continue
|
||||
r.append(path)
|
||||
except IndexError:
|
||||
|
||||
Reference in new issue
Block a user