o
    NDiM                     @   s>   d Z ddlZddlZddlmZ eeZG dd deZ	dS )zc
This module contains utilities for generating timestamps for client-side
timestamp specification.
    N)Lockc                   @   sD   e Zd ZdZdZ	 dZ	 dZ	 dddZdd Zdd	 Z	d
d Z
dS )MonotonicTimestampGeneratoras  
    An object that, when called, returns ``int(time.time() * 1e6)`` when
    possible, but, if the value returned by ``time.time`` doesn't increase,
    drifts into the future and logs warnings.
    Exposed configuration attributes can be configured with arguments to
    ``__init__`` or by changing attributes on an initialized object.

    .. versionadded:: 3.8.0
    T   c                 C   sP   t  | _| j d| _d| _W d    n1 sw   Y  || _|| _|| _d S )Nr   )r   locklast
_last_warnwarn_on_driftwarning_thresholdwarning_interval)selfr   r	   r
    r   M/var/www/Datamplify/venv/lib/python3.10/site-packages/cassandra/timestamps.py__init__8   s   
z$MonotonicTimestampGenerator.__init__c                 C   s.   ||kr	|| _ |S | j|d |d | _ | j S )a4  
        Returns the timestamp that should be used if ``now`` is the current
        time and ``last`` is the last timestamp returned by this object.
        Intended for internal and testing use only; to generate timestamps,
        call an instantiated ``MonotonicTimestampGenerator`` object.

        :param int now: an integer to be used as the current time, typically
            representing the current time in microseconds since the UNIX epoch
        :param int last: an integer representing the last timestamp returned by
            this object
        )nowr   )r   _maybe_warn)r   r   r   r   r   r   _next_timestampA   s   
z+MonotonicTimestampGenerator._next_timestampc                 C   sF   | j  | jtt d | jdW  d   S 1 sw   Y  dS )zw
        Makes ``MonotonicTimestampGenerator`` objects callable; defers
        internally to _next_timestamp.
            .A)r   r   N)r   r   inttimer   )r   r   r   r   __call__U   s
   $z$MonotonicTimestampGenerator.__call__c                 C   s`   | j | }|| j }| jo|| jd ko|| jd k}|r.tdj||| j d || _d S d S )Nr   zClock skew detected: current tick ({now}) was {diff} microseconds behind the last generated timestamp ({last}), returned timestamps will be artificially incremented to guarantee monotonicity.)r   diffr   )r   r   r   r	   r
   logwarningformat)r   r   r   since_last_warnwarnr   r   r   r   ^   s    


z'MonotonicTimestampGenerator._maybe_warnN)Tr   r   )__name__
__module____qualname____doc__r   r	   r
   r   r   r   r   r   r   r   r   r      s    

		r   )
r   loggingr   	threadingr   	getLoggerr   r   objectr   r   r   r   r   <module>   s   
