diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2020-08-06 08:39:35 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-06 08:39:35 -0400 |
commit | fe6cfc80ec6ed3b9e29ca74cde5dcfae3d8236ea (patch) | |
tree | b7ef564fb0367dd1bdc01d73f21b52ec6be07259 /synapse/util/metrics.py | |
parent | Convert synapse.api to async/await (#8031) (diff) | |
download | synapse-fe6cfc80ec6ed3b9e29ca74cde5dcfae3d8236ea.tar.xz |
Convert some util functions to async (#8035)
Diffstat (limited to 'synapse/util/metrics.py')
-rw-r--r-- | synapse/util/metrics.py | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/synapse/util/metrics.py b/synapse/util/metrics.py index ec61e14423..a805f51df1 100644 --- a/synapse/util/metrics.py +++ b/synapse/util/metrics.py @@ -13,14 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import inspect import logging from functools import wraps from prometheus_client import Counter -from twisted.internet import defer - from synapse.logging.context import LoggingContext, current_context from synapse.metrics import InFlightGauge @@ -62,25 +59,31 @@ in_flight = InFlightGauge( def measure_func(name=None): - def wrapper(func): - block_name = func.__name__ if name is None else name + """ + Used to decorate an async function with a `Measure` context manager. + + Usage: - if inspect.iscoroutinefunction(func): + @measure_func() + async def foo(...): + ... - @wraps(func) - async def measured_func(self, *args, **kwargs): - with Measure(self.clock, block_name): - r = await func(self, *args, **kwargs) - return r + Which is analogous to: - else: + async def foo(...): + with Measure(...): + ... + + """ + + def wrapper(func): + block_name = func.__name__ if name is None else name - @wraps(func) - @defer.inlineCallbacks - def measured_func(self, *args, **kwargs): - with Measure(self.clock, block_name): - r = yield func(self, *args, **kwargs) - return r + @wraps(func) + async def measured_func(self, *args, **kwargs): + with Measure(self.clock, block_name): + r = await func(self, *args, **kwargs) + return r return measured_func |