mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-03 05:34:57 -04:00
Convert some util functions to async (#8035)
This commit is contained in:
parent
d4a7829b12
commit
fe6cfc80ec
4 changed files with 40 additions and 62 deletions
|
@ -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):
|
||||
"""
|
||||
Used to decorate an async function with a `Measure` context manager.
|
||||
|
||||
Usage:
|
||||
|
||||
@measure_func()
|
||||
async def foo(...):
|
||||
...
|
||||
|
||||
Which is analogous to:
|
||||
|
||||
async def foo(...):
|
||||
with Measure(...):
|
||||
...
|
||||
|
||||
"""
|
||||
|
||||
def wrapper(func):
|
||||
block_name = func.__name__ if name is None else name
|
||||
|
||||
if inspect.iscoroutinefunction(func):
|
||||
|
||||
@wraps(func)
|
||||
async def measured_func(self, *args, **kwargs):
|
||||
with Measure(self.clock, block_name):
|
||||
r = await func(self, *args, **kwargs)
|
||||
return r
|
||||
|
||||
else:
|
||||
|
||||
@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
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue