server.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import asyncio
  2. import logging
  3. import time
  4. import traceback
  5. from .compatibility import get_running_loop, guarantee_single_callable, run_future
  6. logger = logging.getLogger(__name__)
  7. class StatelessServer:
  8. """
  9. Base server class that handles basic concepts like application instance
  10. creation/pooling, exception handling, and similar, for stateless protocols
  11. (i.e. ones without actual incoming connections to the process)
  12. Your code should override the handle() method, doing whatever it needs to,
  13. and calling get_or_create_application_instance with a unique `scope_id`
  14. and `scope` for the scope it wants to get.
  15. If an application instance is found with the same `scope_id`, you are
  16. given its input queue, otherwise one is made for you with the scope provided
  17. and you are given that fresh new input queue. Either way, you should do
  18. something like:
  19. input_queue = self.get_or_create_application_instance(
  20. "user-123456",
  21. {"type": "testprotocol", "user_id": "123456", "username": "andrew"},
  22. )
  23. input_queue.put_nowait(message)
  24. If you try and create an application instance and there are already
  25. `max_application` instances, the oldest/least recently used one will be
  26. reclaimed and shut down to make space.
  27. Application coroutines that error will be found periodically (every 100ms
  28. by default) and have their exceptions printed to the console. Override
  29. application_exception() if you want to do more when this happens.
  30. If you override run(), make sure you handle things like launching the
  31. application checker.
  32. """
  33. application_checker_interval = 0.1
  34. def __init__(self, application, max_applications=1000):
  35. # Parameters
  36. self.application = application
  37. self.max_applications = max_applications
  38. # Initialisation
  39. self.application_instances = {}
  40. ### Mainloop and handling
  41. def run(self):
  42. """
  43. Runs the asyncio event loop with our handler loop.
  44. """
  45. event_loop = get_running_loop()
  46. asyncio.ensure_future(self.application_checker())
  47. try:
  48. event_loop.run_until_complete(self.handle())
  49. except KeyboardInterrupt:
  50. logger.info("Exiting due to Ctrl-C/interrupt")
  51. async def handle(self):
  52. raise NotImplementedError("You must implement handle()")
  53. async def application_send(self, scope, message):
  54. """
  55. Receives outbound sends from applications and handles them.
  56. """
  57. raise NotImplementedError("You must implement application_send()")
  58. ### Application instance management
  59. def get_or_create_application_instance(self, scope_id, scope):
  60. """
  61. Creates an application instance and returns its queue.
  62. """
  63. if scope_id in self.application_instances:
  64. self.application_instances[scope_id]["last_used"] = time.time()
  65. return self.application_instances[scope_id]["input_queue"]
  66. # See if we need to delete an old one
  67. while len(self.application_instances) > self.max_applications:
  68. self.delete_oldest_application_instance()
  69. # Make an instance of the application
  70. input_queue = asyncio.Queue()
  71. application_instance = guarantee_single_callable(self.application)
  72. # Run it, and stash the future for later checking
  73. future = run_future(
  74. application_instance(
  75. scope=scope,
  76. receive=input_queue.get,
  77. send=lambda message: self.application_send(scope, message),
  78. ),
  79. )
  80. self.application_instances[scope_id] = {
  81. "input_queue": input_queue,
  82. "future": future,
  83. "scope": scope,
  84. "last_used": time.time(),
  85. }
  86. return input_queue
  87. def delete_oldest_application_instance(self):
  88. """
  89. Finds and deletes the oldest application instance
  90. """
  91. oldest_time = min(
  92. details["last_used"] for details in self.application_instances.values()
  93. )
  94. for scope_id, details in self.application_instances.items():
  95. if details["last_used"] == oldest_time:
  96. self.delete_application_instance(scope_id)
  97. # Return to make sure we only delete one in case two have
  98. # the same oldest time
  99. return
  100. def delete_application_instance(self, scope_id):
  101. """
  102. Removes an application instance (makes sure its task is stopped,
  103. then removes it from the current set)
  104. """
  105. details = self.application_instances[scope_id]
  106. del self.application_instances[scope_id]
  107. if not details["future"].done():
  108. details["future"].cancel()
  109. async def application_checker(self):
  110. """
  111. Goes through the set of current application instance Futures and cleans up
  112. any that are done/prints exceptions for any that errored.
  113. """
  114. while True:
  115. await asyncio.sleep(self.application_checker_interval)
  116. for scope_id, details in list(self.application_instances.items()):
  117. if details["future"].done():
  118. exception = details["future"].exception()
  119. if exception:
  120. await self.application_exception(exception, details)
  121. try:
  122. del self.application_instances[scope_id]
  123. except KeyError:
  124. # Exception handling might have already got here before us. That's fine.
  125. pass
  126. async def application_exception(self, exception, application_details):
  127. """
  128. Called whenever an application coroutine has an exception.
  129. """
  130. logging.error(
  131. "Exception inside application: %s\n%s%s",
  132. exception,
  133. "".join(traceback.format_tb(exception.__traceback__)),
  134. f" {exception}",
  135. )