Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greedy ioloop working #1870

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions synapse/glob.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import signal
import asyncio
import logging
import threading
import faulthandler

logger = logging.getLogger(__name__)

_glob_loop = None
_glob_thrd = None

Expand Down Expand Up @@ -47,21 +50,26 @@ def initloop():
_glob_loop = asyncio.get_running_loop()
# if we get here, it's us!
_glob_thrd = threading.currentThread()
# Enable debug and greedy coro collection
setGreedCoro(_glob_loop)

except RuntimeError:

# otherwise, lets fire one...
_glob_loop = asyncio.new_event_loop()
greedy_threshold = os.environ.get('SYN_GREEDY_CORO')
if greedy_threshold is not None:
_glob_loop.slow_callback_duration = float(greedy_threshold)
setGreedCoro(_glob_loop)

_glob_thrd = threading.Thread(target=_glob_loop.run_forever, name='SynLoop')
_glob_thrd.setDaemon(True)
_glob_thrd.start()

return _glob_loop

def setGreedCoro(loop: asyncio.AbstractEventLoop):
greedy_threshold = os.environ.get('SYN_GREEDY_CORO')
if greedy_threshold is not None: # pragma: no cover
logger.info(f'Setting ioloop.slow_callback_duration to {greedy_threshold}')
loop.set_debug(True)
loop.slow_callback_duration = float(greedy_threshold)

def iAmLoop():
initloop()
return threading.currentThread() == _glob_thrd
Expand Down