38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
"""slow_sanctions and sanctions_errors -- phase-0-base-project-spec.md §7.
|
|||
|
|
|
||
|
|
Both flags belong to `sanctions`, the fake third-party watchlist vendor.
|
||
|
|
"""
|
||
|
|
import math
|
||
|
|
import random
|
||
|
|
|
||
|
|
from common.chaos_flags import is_enabled
|
||
|
|
|
||
|
|
_MEDIAN_SECONDS = 0.04
|
||
|
|
_P99_SECONDS = 1.8
|
||
|
|
_MU = math.log(_MEDIAN_SECONDS)
|
||
|
|
_Z99 = 2.326347874
|
||
|
|
_SIGMA = (math.log(_P99_SECONDS) - _MU) / _Z99
|
||
|
|
_TIMEOUT_TRIGGER_PROBABILITY = 0.015
|
||
|
|
_TIMEOUT_TRIGGER_SECONDS = 5.0
|
||
|
|
_ERROR_PROBABILITY = 0.03
|
||
|
|
|
||
|
|
|
||
|
|
async def slow_sanctions_enabled() -> bool:
|
||
|
|
return await is_enabled("slow_sanctions")
|
||
|
|
|
||
|
|
|
||
|
|
async def sanctions_errors_enabled() -> bool:
|
||
|
|
return await is_enabled("sanctions_errors")
|
||
|
|
|
||
|
|
|
||
|
|
def sample_delay_seconds() -> float:
|
||
|
|
"""Log-normal delay: median ~40ms, p99 ~1.8s. 1.5% of calls sleep 5s,
|
||
|
|
which exceeds the caller's 3s downstream timeout and surfaces as one."""
|
||
|
|
if random.random() < _TIMEOUT_TRIGGER_PROBABILITY:
|
||
|
|
return _TIMEOUT_TRIGGER_SECONDS
|
||
|
|
return random.lognormvariate(_MU, _SIGMA)
|
||
|
|
|
||
|
|
|
||
|
|
def should_error() -> bool:
|
||
|
|
return random.random() < _ERROR_PROBABILITY
|