Compare commits

...

2 Commits

Author SHA1 Message Date
Andrew Morgan
6d091f6e12 newsfile 2025-10-07 16:53:47 +01:00
Andrew Morgan
6e349db4da Allow Synapse modules to specify their own threadpool when calling defer_to_thread 2025-10-07 16:50:59 +01:00
2 changed files with 12 additions and 2 deletions

View File

@@ -0,0 +1 @@
Allow Synapse modules to specify a custom threadpool when calling `defer_to_thread`.

View File

@@ -43,6 +43,7 @@ from typing_extensions import Concatenate, ParamSpec
from twisted.internet import defer from twisted.internet import defer
from twisted.internet.interfaces import IDelayedCall from twisted.internet.interfaces import IDelayedCall
from twisted.python.threadpool import ThreadPool
from twisted.web.resource import Resource from twisted.web.resource import Resource
from synapse.api import errors from synapse.api import errors
@@ -78,7 +79,7 @@ from synapse.http.server import (
from synapse.http.servlet import parse_json_object_from_request from synapse.http.servlet import parse_json_object_from_request
from synapse.http.site import SynapseRequest from synapse.http.site import SynapseRequest
from synapse.logging.context import ( from synapse.logging.context import (
defer_to_thread, defer_to_threadpool,
make_deferred_yieldable, make_deferred_yieldable,
run_in_background, run_in_background,
) )
@@ -1716,6 +1717,7 @@ class ModuleApi:
async def defer_to_thread( async def defer_to_thread(
self, self,
f: Callable[P, T], f: Callable[P, T],
threadpool: Optional[ThreadPool] = None,
*args: P.args, *args: P.args,
**kwargs: P.kwargs, **kwargs: P.kwargs,
) -> T: ) -> T:
@@ -1731,7 +1733,14 @@ class ModuleApi:
Returns: Returns:
The return value of the function once ran in a thread. The return value of the function once ran in a thread.
""" """
return await defer_to_thread(self._hs.get_reactor(), f, *args, **kwargs) # If a threadpool is not provided by the module, then use the default
# reactor threadpool of the homeserver.
if threadpool is None:
threadpool = self._hs.get_reactor().getThreadPool()
return await defer_to_threadpool(
self._hs.get_reactor(), threadpool, f, *args, **kwargs
)
async def check_username(self, username: str) -> None: async def check_username(self, username: str) -> None:
"""Checks if the provided username uses the grammar defined in the Matrix """Checks if the provided username uses the grammar defined in the Matrix