sync-v2 step 5: activity tiers (hot/warm/cold)
Each owned account is tiered by the coordinator; the runner shapes its work to
match — correctness identical, only latency/socket cost differ.
- hot: persistent INBOX IDLE + secondary poll every POLL_WARM.
- warm: no persistent socket; transient INBOX+special poll every POLL_WARM.
- cold: same transient poll every POLL_COLD.
- engine: poll_all_loop (transient full poll); secondary_poll_loop interval is
now a parameter; POLL_INTERVAL removed in favor of POLL_WARM/POLL_COLD.
- coordinator: tier eval every 120s from session heat (sess:active:{user}) +
recent mail (max internal_date within TIER_HOT_ACTIVITY_H), with the
HOT_BUDGET_PER_WORKER guard shedding least-recently-active session-less hot
accounts; user.active event promotes a user's accounts to hot at once (≤5s).
- API: login + WS connect/ping publish user.active and refresh sess:active
(TTL TIER_SESSION_GRACE_MIN). New sync:tier:{id} key, default warm on claim.
- config: POLL_WARM/POLL_COLD/TIER_*/HOT_BUDGET_PER_WORKER.
- tests: 5 tier tests (session/recent-mail promotion, cold, user.active,
budget demotion) alongside the lease/lifecycle acceptance tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,8 @@ from app.redis_client import aw
|
||||
LIVE_SET = "sync:live"
|
||||
_LEASE_PREFIX = "sync:lease:"
|
||||
WORKERS_PREFIX = "sync:workers:"
|
||||
_TIER_PREFIX = "sync:tier:"
|
||||
_SESSION_PREFIX = "sess:active:"
|
||||
|
||||
|
||||
def lease_key(account_id: uuid.UUID) -> str:
|
||||
@@ -30,6 +32,20 @@ def worker_key(worker_id: str) -> str:
|
||||
return f"{WORKERS_PREFIX}{worker_id}"
|
||||
|
||||
|
||||
def tier_key(account_id: uuid.UUID) -> str:
|
||||
return f"{_TIER_PREFIX}{account_id}"
|
||||
|
||||
|
||||
def session_key(user_id: uuid.UUID | str) -> str:
|
||||
"""Presence key the API refreshes while a user has an active session (§5)."""
|
||||
return f"{_SESSION_PREFIX}{user_id}"
|
||||
|
||||
|
||||
async def mark_user_active(redis: aioredis.Redis, user_id: uuid.UUID | str, grace_s: int) -> None:
|
||||
"""Refresh the user's session-heat key; workers read it for tier promotion."""
|
||||
await aw(redis.set(session_key(user_id), "1", ex=grace_s))
|
||||
|
||||
|
||||
# Renew only if we still own the lease (guards against a stale worker extending a
|
||||
# lease another worker has already claimed after expiry). Returns 1 on success.
|
||||
_RENEW_LUA = """
|
||||
|
||||
Reference in New Issue
Block a user