def _seconds_until_refresh()

in google/cloud/alloydb/connector/refresh_utils.py [0:0]


def _seconds_until_refresh(expiration: datetime) -> int:
    """
    Calculates the duration to wait before starting the next refresh.
    Usually the duration will be half of the time until certificate
    expiration.

    Args:
        expiration (datetime.datetime): Time of certificate expiration.
    Returns:
        int: Time in seconds to wait before performing next refresh.
    """

    duration = int((expiration - datetime.now(timezone.utc)).total_seconds())

    # if certificate duration is less than 1 hour
    if duration < 3600:
        # something is wrong with certificate, refresh now
        if duration < _refresh_buffer:
            return 0
        # otherwise wait until 4 minutes before expiration for next refresh
        return duration - _refresh_buffer
    return duration // 2