o
    ND©i02  ã                   @   sø   d dl Z d dlmZ d dlmZ d dlZd dlZd dlZd dlZd dl	m
Z
mZ d dlZd dlmZ zd dlm  mZ W n eyH   edƒ‚w d dlmZmZmZmZmZ e e¡Zdd	„ ZG d
d„ deƒZda e  !eet ƒ¡ G dd„ deƒZ"dS )é    N)Údeque)Úpartial)ÚLockÚThread)ÚDependencyExceptiona4  The C extension needed to use libev was not found.  This probably means that you didn't have the required build dependencies when installing the driver.  See http://datastax.github.io/python-driver/installation.html#c-extensions for instructions on installing build dependencies and building the C extension.)Ú
ConnectionÚConnectionShutdownÚNONBLOCKINGÚTimerÚTimerManagerc                 C   s   | r|   ¡  d S d S ©N)Ú_cleanup)Úloop© r   úR/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/io/libevreactor.pyr   +   s   ÿr   c                   @   sd   e Z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d„ Zdd„ Zdd„ ZdS )Ú	LibevLoopc                 C   s¼   t  ¡ | _t ¡ | _t | j¡| _| j ¡  | j 	¡  d| _
d| _tƒ | _tƒ | _d | _tƒ | _tƒ | _tƒ | _tƒ | _t | j| j¡| _| j 	¡  | j ¡  tƒ | _t | j| j¡| _d S )NF)ÚosÚgetpidÚ_pidÚlibevÚLoopÚ_loopÚAsyncÚ	_notifierÚstartÚunrefÚ_startedÚ	_shutdownr   Ú_lockÚ_lock_threadÚ_threadÚsetÚ_live_connsÚ
_new_connsÚ_closed_connsÚ_conn_set_lockÚPrepareÚ_loop_will_runÚ	_preparerr   Ú_timersr
   Ú_on_loop_timerÚ_loop_timer©Úselfr   r   r   Ú__init__2   s&   





zLibevLoop.__init__c                 C   s¤   d}| j  | jst d¡ d| _d}W d   ƒ n1 sw   Y  |rK| j | js<t| jdd| _d| j_	| j 
¡  W d   ƒ n1 sFw   Y  | j ¡  d S )NFzStarting libev event loopTÚ
event_loop)ÚtargetÚname)r   r   ÚlogÚdebugr   r   r   Ú	_run_loopr    Údaemonr   r   Úsend)r-   Úshould_startr   r   r   Úmaybe_startS   s"   
€ü
€üzLibevLoop.maybe_startc                 C   sn   	 | j  ¡  | j% | js| jrt d¡ 	 W d   ƒ q t d¡ d| _	 W d   ƒ d S 1 s1w   Y  q)NTzRestarting event loopz2All Connections currently closed, event loop endedF)r   r   r   r   r"   r2   r3   r   r,   r   r   r   r4   d   s   

ý
 øýzLibevLoop._run_loopc                 C   s¬   d| _ | jsd S | j| jB | jB D ]}| ¡  |j|jfD ]}|r%| ¡  qq|  	¡  | j
 | jjdd W d   ƒ n1 s@w   Y  | j ¡ rOt d¡ t d¡ d S )NTg      ð?)ÚtimeoutzrEvent loop thread could not be joined, so shutdown may not be clean. Please call Cluster.shutdown() to avoid this.zEvent loop thread was joined)r   r    r"   r#   r$   ÚcloseÚ_write_watcherÚ_read_watcherÚstopÚnotifyr   ÚjoinÚis_aliver2   Úwarningr3   )r-   ÚconnÚwatcherr   r   r   r   r   s&   €þÿ
ÿzLibevLoop._cleanupc                 C   s   | j  |¡ | j ¡  d S r   )r)   Ú	add_timerr   r6   )r-   Útimerr   r   r   rD   Š   s   zLibevLoop.add_timerc                 C   s>   | j s| j ¡ }|r| j |t ¡  ¡ d S d S | j ¡  d S r   )r   r)   Úservice_timeoutsr+   r   Útimer=   )r-   Únext_endr   r   r   Ú_update_timerŽ   s   
ÿzLibevLoop._update_timerc                 C   ó   | j  ¡  d S r   )r)   rF   r,   r   r   r   r*   –   ó   zLibevLoop._on_loop_timerc                 C   rJ   r   )r   r6   r,   r   r   r   r>   ™   rK   zLibevLoop.notifyc                 C   s`   | j # | j ¡ }| |¡ || _| j ¡ }| |¡ || _W d   ƒ d S 1 s)w   Y  d S r   )r%   r"   ÚcopyÚaddr#   )r-   rB   Únew_live_connsÚnew_new_connsr   r   r   Úconnection_createdœ   s   



"ùzLibevLoop.connection_createdc                 C   sh   | j " | j ¡ }| |¡ || _| j ¡ }| |¡ || _W d   ƒ n1 s(w   Y  | j ¡  d S r   )r%   r"   rL   Údiscardr$   rM   r   r6   )r-   rB   rN   Únew_closed_connsr   r   r   Úconnection_destroyed¦   s   



ù	zLibevLoop.connection_destroyedc                 C   s8  d}| j D ]&}|js|jr|jr|j ¡  d|_d}q|jr+|js+|j ¡  d|_d}q| jrU| j | j}tƒ | _W d   ƒ n1 sDw   Y  |D ]}|j	 ¡  qKd}| j
r| j | j
}tƒ | _
W d   ƒ n1 smw   Y  |D ]}|jr€|j ¡  |`|j	rŠ|j	 ¡  |`	qtd}|  ¡  |rš| j ¡  d S d S )NFT)r"   r   Ú_write_watcher_is_activer;   r=   r   r#   r%   r!   r<   r$   rI   r   r6   )r-   ÚprepareÚchangedrB   Úto_startÚto_stopr   r   r   r'   ²   sL   


€
þ
þ

€ÿzLibevLoop._loop_will_runN)Ú__name__Ú
__module__Ú__qualname__r.   r8   r4   r   rD   rI   r*   r>   rP   rS   r'   r   r   r   r   r   0   s    !
r   c                   @   sp   e Zd ZdZdZdZdZdZedd„ ƒZ	edd„ ƒZ
edd	„ ƒZd
d„ Zdd„ Zddd„Zddd„Zdd„ ZdS )ÚLibevConnectionzW
    An implementation of :class:`.Connection` that uses libev for its event loop.
    FNc                 C   s<   t stƒ a d S t jt ¡ krt d¡ |  ¡  tƒ a d S d S )Nz8Detected fork, clearing and reinitializing reactor state)Ú_global_loopr   r   r   r   r2   r3   Úhandle_fork©Úclsr   r   r   Úinitialize_reactorî   s   


ýz"LibevConnection.initialize_reactorc                 C   s   t r
t  ¡  d a d S d S r   )r]   r   r_   r   r   r   r^   ù   s   þzLibevConnection.handle_forkc                 C   s   t ||ƒ}t |¡ |S r   )r
   r]   rD   )r`   r9   ÚcallbackrE   r   r   r   Úcreate_timer   s   

zLibevConnection.create_timerc                 O   s¼   t j| g|¢R i |¤Ž tƒ | _tƒ | _|  ¡  | j d¡ tj	& t
 | j ¡ t
jtj| j¡| _t
 | j ¡ t
jtj| j¡| _W d   ƒ n1 sJw   Y  |  ¡  t | ¡ t ¡  d S ©Nr   )r   r.   r   r   Ú_deque_lockÚ_connect_socketÚ_socketÚsetblockingr]   r   r   ÚIOÚfilenoÚEV_READr   Úhandle_readr<   ÚEV_WRITEÚhandle_writer;   Ú_send_options_messagerP   r8   )r-   ÚargsÚkwargsr   r   r   r.     s    þ
zLibevConnection.__init__c                 C   sœ   | j  | jr	 W d   ƒ d S d| _W d   ƒ n1 sw   Y  t dt| ƒ| j¡ t | ¡ | j 	¡  t d| j¡ | j
sL|  td| j ƒ¡ d S d S )NTzClosing connection (%s) to %szClosed socket to %szConnection to %s was closed)ÚlockÚ	is_closedr2   r3   ÚidÚendpointr]   rS   rg   r:   Ú
is_defunctÚerror_all_requestsr   r,   r   r   r   r:     s   þý

ÿÿzLibevConnection.closec                 C   s˜  |t j@ r|rt|t |¡ƒ}ntdƒ}|  |¡ d S 	 z| j | j 	¡ }W d   ƒ n1 s0w   Y  W n t
yF   | jsCd| _Y d S w z| j |¡}W nO tjyž } zB|jd tv sj|jd tjtjfv rŽ|jd tv rtd| _| j | j |¡ W d   ƒ n1 sˆw   Y  n|  |¡ W Y d }~d S d }~ww |t|ƒk rË| j | j ||d … ¡ W d   ƒ n1 s½w   Y  |dkrËd| _d S q)Núlibev reported an errorTr   F)r   ÚEV_ERRORÚIOErrorr   ÚstrerrorÚ	ExceptionÚdefunctre   r   ÚpopleftÚ
IndexErrorÚ_socket_writablerg   r6   ÚsocketÚerrorrp   r	   ÚsslÚSSL_ERROR_WANT_READÚSSL_ERROR_WANT_WRITEÚ
appendleftÚlen)r-   rC   ÚreventsÚerrnoÚexcÚnext_msgÚsentÚerrr   r   r   rn   *  sP   

ÿ€ýÿ€
€÷ÿãzLibevConnection.handle_writec              
   C   sR  |t j@ r|rt|t |¡ƒ}ntdƒ}|  |¡ d S z	 | j | j	¡}| j
 |¡ t|ƒ| j	k r2nqW n] tjy‘ } zPt|tjƒrg|jd tjtjfv r[| j
 ¡ sZW Y d }~d S n,|  |¡ W Y d }~d S |jd tv r{| j
 ¡ szW Y d }~d S n|  |¡ W Y d }~d S W Y d }~nd }~ww | j
 ¡ r|  ¡  d S t d| ¡ |  ¡  d S )Nrx   Tr   zConnection %s closed by server)r   ry   rz   r   r{   r|   r}   rg   ÚrecvÚin_buffer_sizeÚ_iobufÚwriter‡   r   r‚   Ú
isinstancerƒ   ÚSSLErrorrp   r„   r…   Útellr	   Úprocess_io_bufferr2   r3   r:   )r-   rC   rˆ   r‰   rŠ   Úbufr   r   r   r   rl   S  sD   

ü
ÿ

ÿ
€ó
zLibevConnection.handle_readc                 C   sŠ   | j }t|ƒ|kr"g }tdt|ƒ|ƒD ]}| |||| … ¡ qn|g}| j | j |¡ t ¡  W d   ƒ d S 1 s>w   Y  d S rd   )	Úout_buffer_sizer‡   ÚrangeÚappendre   r   Úextendr]   r>   )r-   ÚdataÚsabsÚchunksÚir   r   r   Úpushw  s   ÿ
"þzLibevConnection.pushr   )rY   rZ   r[   Ú__doc__rT   r<   r;   rg   Úclassmethodra   r^   rc   r.   r:   rn   rl   rŸ   r   r   r   r   r\   å   s"    





)$r\   )#ÚatexitÚcollectionsr   Ú	functoolsr   Úloggingr   r   rƒ   Ú	threadingr   r   rG   Ú	cassandrar   Úcassandra.io.libevwrapperÚioÚlibevwrapperr   ÚImportErrorÚcassandra.connectionr   r   r	   r
   r   Ú	getLoggerrY   r2   r   Úobjectr   r]   Úregisterr\   r   r   r   r   Ú<module>   s2   ÿÿ	
 2