U
    ڲgu                     @   sH   d dl Z d dlZd dlZd dlZddlmZ eeZG dd dZ	dS )    N   )guarantee_single_callablec                   @   s^   e Zd ZdZdZdddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )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?  c                 C   s   || _ || _i | _d S N)applicationmax_applicationsapplication_instances)selfr   r    r   2/tmp/pip-unpacked-wheel-hay1n6u8/asgiref/server.py__init__.   s    zStatelessServer.__init__c                 C   sL   t  }t |   z||   W n tk
rF   td Y nX dS )zD
        Runs the asyncio event loop with our handler loop.
        zExiting due to Ctrl-C/interruptN)	asyncioZget_event_loopensure_futureapplication_checkerZrun_until_completehandleKeyboardInterruptloggerinfo)r
   Z
event_loopr   r   r   run7   s    zStatelessServer.runc                    s   t dd S )NzYou must implement handle()NotImplementedError)r
   r   r   r   r   B   s    zStatelessServer.handlec                    s   t ddS )zM
        Receives outbound sends from applications and handles them.
        z%You must implement application_send()Nr   )r
   scopemessager   r   r   application_sendE   s    z StatelessServer.application_sendc                    s   |j kr*t j | d< j | d S tj jkrD  q*t }tj}t	| |j
 fddd}|| t dj |< |S )zH
        Creates an application instance and returns its queue.
        	last_usedinput_queuec                    s     | S r   )r   )r   r   r
   r   r   <lambda>_       zDStatelessServer.get_or_create_application_instance.<locals>.<lambda>)r   Zreceivesend)r   futurer   r   )r	   timelenr   "delete_oldest_application_instancer   Queuer   r   r   get)r
   scope_idr   r   Zapplication_instancer!   r   r   r   "get_or_create_application_instanceM   s(    


z2StatelessServer.get_or_create_application_instancec                 C   sL   t dd | j D }| j D ]$\}}|d |kr"| |  dS q"dS )zC
        Finds and deletes the oldest application instance
        c                 s   s   | ]}|d  V  qdS )r   Nr   ).0detailsr   r   r   	<genexpr>n   s    zEStatelessServer.delete_oldest_application_instance.<locals>.<genexpr>r   N)minr	   valuesitemsdelete_application_instance)r
   Zoldest_timer'   r*   r   r   r   r$   j   s    
z2StatelessServer.delete_oldest_application_instancec                 C   s.   | j | }| j |= |d  s*|d   dS )z
        Removes an application instance (makes sure its task is stopped,
        then removes it from the current set)
        r!   N)r	   donecancel)r
   r'   r*   r   r   r   r/   x   s    
z+StatelessServer.delete_application_instancec              	      s   t | jI dH  t| j D ]X\}}|d  r |d  }|rV| ||I dH  z| j|= W q  t	k
rv   Y q X q q dS )z
        Goes through the set of current application instance Futures and cleans up
        any that are done/prints exceptions for any that errored.
        Nr!   )
r   sleepapplication_checker_intervallistr	   r.   r0   	exceptionapplication_exceptionKeyError)r
   r'   r*   r5   r   r   r   r      s    z#StatelessServer.application_checkerc              	      s(   t d|dt|jd|  dS )zL
        Called whenever an application coroutine has an exception.
        z%Exception inside application: %s
%s%s z  N)loggingerrorjoin	traceback	format_tb__traceback__)r
   r5   Zapplication_detailsr   r   r   r6      s    z%StatelessServer.application_exceptionN)r   )__name__
__module____qualname____doc__r3   r   r   r   r   r(   r$   r/   r   r6   r   r   r   r   r      s    
	
r   )
r   r9   r"   r<   Zcompatibilityr   	getLoggerr?   r   r   r   r   r   r   <module>   s   
