Plugin index: Avoid making multiple network requests in parallel

This commit is contained in:
Rikko 2022-10-03 22:35:47 +05:30
parent 8b12e00db6
commit 6f9fc4baaa
2 changed files with 12 additions and 9 deletions

View file

@ -1,12 +1,7 @@
{
"plugin_manager_url": "http://github.com/bombsquad-community/plugin-manager/{content_type}/{tag}/plugin_manager.py",
"versions": {
"0.1.7": {
"api_version": 7,
"commit_sha": "09991f7",
"released_on": "03-10-2022",
"md5sum": "283fdf4b1b16102d77b8b67615780d5d"
},
"0.1.7": null,
"0.1.6": {
"api_version": 7,
"commit_sha": "2a7ad8e",

View file

@ -920,6 +920,7 @@ class PluginManager:
self._index = _CACHE.get("index", {})
self.categories = {}
self.module_path = sys.modules[__name__].__file__
self._index_setup_in_progress = False
async def get_index(self):
if not self._index:
@ -932,13 +933,20 @@ class PluginManager:
headers=self.request_headers,
)
response = await async_send_network_request(request)
self._index = json.loads(response.read())
self.set_index_global_cache(self._index)
index = json.loads(response.read())
self.set_index_global_cache(index)
self._index = index
return self._index
async def setup_index(self):
while self._index_setup_in_progress:
# Avoid making multiple network calls to the same resource in parallel.
# Rather wait for the previous network call to complete.
await asyncio.sleep(0.1)
self._index_setup_in_progress = not bool(self._index)
index = await self.get_index()
await self.setup_plugin_categories(index)
self._index_setup_in_progress = False
async def setup_plugin_categories(self, plugin_index):
# A hack to have the "All" category show at the top.