api docs: Load the OpenAPI file only when needed.

We found out in #9953 that, appparently, loading the OpenAPI file was
taking abut a 5% of the Zulip server startup time.

Since in many cases (especially in development) having the file loaded
won't be necessary at all, we read it on the first time data from the
OpenAPI spec is needed.

Tweaked by tabbott to add a test.
This commit is contained in:
Yago González 2018-08-08 01:35:41 +02:00 committed by Tim Abbott
parent 25d2efb9ca
commit e1662024d1
2 changed files with 7 additions and 1 deletions

View File

@ -23,7 +23,7 @@ EXCLUDE_PROPERTIES = {
class OpenAPISpec():
def __init__(self, path: str) -> None:
self.path = path
self.reload()
self.last_update = None # type: Optional[float]
def reload(self) -> None:
self.last_update = os.path.getmtime(self.path)

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import mock
from typing import Dict, Any
import zerver.lib.openapi as openapi
@ -132,3 +133,8 @@ class OpenAPIToolsTest(ZulipTestCase):
# Check that the file has been reloaded by verifying that the last
# update date isn't zero anymore
self.assertNotEqual(openapi_spec.last_update, 0)
# Now verify calling it again doesn't call reload
with mock.patch('zerver.lib.openapi.openapi_spec.reload') as mock_reload:
get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD)
self.assertFalse(mock_reload.called)