o 6au@sHddlZddlZddlZddlZddlmZeeZGdddZ dS)N)guarantee_single_callablec@s^eZdZdZdZdddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ dS)StatelessServerax Base server class that handles basic concepts like application instance creation/pooling, exception handling, and similar, for stateless protocols (i.e. ones without actual incoming connections to the process) Your code should override the handle() method, doing whatever it needs to, and calling get_or_create_application_instance with a unique `scope_id` and `scope` for the scope it wants to get. If an application instance is found with the same `scope_id`, you are given its input queue, otherwise one is made for you with the scope provided and you are given that fresh new input queue. Either way, you should do something like: input_queue = self.get_or_create_application_instance( "user-123456", {"type": "testprotocol", "user_id": "123456", "username": "andrew"}, ) input_queue.put_nowait(message) If you try and create an application instance and there are already `max_application` instances, the oldest/least recently used one will be reclaimed and shut down to make space. Application coroutines that error will be found periodically (every 100ms by default) and have their exceptions printed to the console. Override application_exception() if you want to do more when this happens. If you override run(), make sure you handle things like launching the application checker. g?cCs||_||_i|_dSN) applicationmax_applicationsapplication_instances)selfrrr 0/usr/lib/python3/dist-packages/asgiref/server.py__init__.s zStatelessServer.__init__cCsJt}t|z ||WdSty$tdYdSw)zD Runs the asyncio event loop with our handler loop. zExiting due to Ctrl-C/interruptN) asyncioget_event_loop ensure_futureapplication_checkerrun_until_completehandleKeyboardInterruptloggerinfo)r event_loopr r r run7s zStatelessServer.runc td)NzYou must implement handle()NotImplementedError)r r r r rBszStatelessServer.handlecr)zM Receives outbound sends from applications and handles them. z%You must implement application_send()r)r scopemessager r r application_sendEsz StatelessServer.application_sendcs|jvrtj|d<j|dStjjkr)tjjkst}tj}t ||j fddd}||tdj|<|S)zH Creates an application instance and returns its queue. last_used input_queuecs |Sr)r)rrr r r _s zDStatelessServer.get_or_create_application_instance..)rreceivesend)r futurerr) r timelenr"delete_oldest_application_instancerQueuerrrget)r scope_idrr application_instancer%r r!r "get_or_create_application_instanceMs*    z2StatelessServer.get_or_create_application_instancecCsLtdd|jD}|jD]\}}|d|kr#||dSqdS)zC Finds and deletes the oldest application instance css|]}|dVqdS)rNr ).0detailsr r r ns zEStatelessServer.delete_oldest_application_instance..rN)minr valuesitemsdelete_application_instance)r oldest_timer+r/r r r r(js  z2StatelessServer.delete_oldest_application_instancecCs2|j|}|j|=|ds|ddSdS)z Removes an application instance (makes sure its task is stopped, then removes it from the current set) r%N)r donecancel)r r+r/r r r r4xs  z+StatelessServer.delete_application_instancec s~ t|jIdHt|jD]+\}}|dr=|d}|r-|||IdHz|j|=Wqt y<Yqwqq)z Goes through the set of current application instance Futures and cleans up any that are done/prints exceptions for any that errored. TNr%) rsleepapplication_checker_intervallistr r3r6 exceptionapplication_exceptionKeyError)r r+r/r;r r r rs    z#StatelessServer.application_checkerc s*td|dt|jd|dS)zL Called whenever an application coroutine has an exception. z%Exception inside application: %s %s%sz N)loggingerrorjoin traceback format_tb __traceback__)r r;application_detailsr r r r<sz%StatelessServer.application_exceptionN)r)__name__ __module__ __qualname____doc__r9r rrrr-r(r4rr<r r r r r s    r) rr?r&rB compatibilityr getLoggerrFrrr r r r s