Force view via request headers instead of temporary flag
The flag approach is not thread safe.
This commit is contained in:
parent
d92df9c698
commit
95a449530e
3 changed files with 10 additions and 14 deletions
|
|
@ -762,10 +762,14 @@ class Server(object):
|
|||
self._logger.info("Preemptively caching {} (plugin {}) for {!r}".format(route, plugin, kwargs))
|
||||
else:
|
||||
self._logger.info("Preemptively caching {} for {!r}".format(route, kwargs))
|
||||
|
||||
headers = kwargs.get("headers", dict())
|
||||
headers["X-Force-View"] = plugin if plugin else "_default"
|
||||
kwargs["headers"] = headers
|
||||
|
||||
builder = EnvironBuilder(**kwargs)
|
||||
with preemptive_cache.cache_environment(dict(plugin=plugin if plugin is not None else "_default")):
|
||||
with preemptive_cache.disable_access_logging():
|
||||
app(builder.get_environ(), lambda *a, **kw: None)
|
||||
with preemptive_cache.disable_access_logging():
|
||||
app(builder.get_environ(), lambda *a, **kw: None)
|
||||
except:
|
||||
self._logger.exception("Error while trying to preemptively cache {} for {!r}".format(route, kwargs))
|
||||
|
||||
|
|
|
|||
|
|
@ -448,13 +448,6 @@ class PreemptiveCache(object):
|
|||
yield
|
||||
self._log_access = True
|
||||
|
||||
@contextlib.contextmanager
|
||||
def cache_environment(self, environment):
|
||||
with self._environment_lock:
|
||||
self.environment = environment
|
||||
yield
|
||||
self.environment = None
|
||||
|
||||
def clean_all_data(self, cleanup_function):
|
||||
assert callable(cleanup_function)
|
||||
|
||||
|
|
|
|||
|
|
@ -327,10 +327,7 @@ def index():
|
|||
dict())
|
||||
return preemptively_cached()
|
||||
|
||||
forced_view = None
|
||||
preemptive_cache_environment = preemptiveCache.environment
|
||||
if preemptive_cache_environment is not None and isinstance(preemptive_cache_environment, dict):
|
||||
forced_view = preemptive_cache_environment.get("plugin", "_default")
|
||||
forced_view = request.headers.get("X-Forced-View", None)
|
||||
|
||||
if forced_view:
|
||||
# we have view forced by the preemptive cache
|
||||
|
|
@ -353,6 +350,8 @@ def index():
|
|||
response = plugin_view(plugin)
|
||||
if response is not None:
|
||||
break
|
||||
else:
|
||||
_logger.warn("UiPlugin {} returned an empty response".format(plugin._identifier))
|
||||
else:
|
||||
response = default_view()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue