forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_request_authority.py
More file actions
78 lines (63 loc) · 2.45 KB
/
Copy pathframe_request_authority.py
File metadata and controls
78 lines (63 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Shared rollout and account-generation authority for JIT frame requests.
Frame requests deliberately use the one backend JIT rollout flag and kill
switch owned by :mod:`utils.jit_rollout`. This adapter adds only the current
account-generation fence needed by the device queue; it never creates a second
PostHog control plane or performs synchronous provider IO on an async caller.
"""
from __future__ import annotations
from dataclasses import dataclass
from database.account_cutover import get_account_cutover_record
from utils.executors import db_executor, run_blocking
from utils.jit_rollout import JITDecisionStage, resolve_jit_rollout
@dataclass(frozen=True)
class FrameRequestAuthorityDecision:
enabled: bool
account_generation: int | None = None
kill_switch: bool = False
def _account_generation(uid: str) -> int:
return get_account_cutover_record(uid).account_generation
async def resolve_frame_request_authority(
uid: str,
*,
stage: JITDecisionStage,
force_refresh: bool = False,
) -> FrameRequestAuthorityDecision:
"""Resolve shared bounded JIT control, then the current owner generation."""
owner_uid = uid.strip()
if not owner_uid:
return FrameRequestAuthorityDecision(enabled=False)
try:
rollout = await resolve_jit_rollout(
owner_uid,
stage=stage,
force_refresh=force_refresh,
)
if not rollout.permits_work:
return FrameRequestAuthorityDecision(
enabled=False,
kill_switch=rollout.kill_switch.value == "enabled",
)
generation = await run_blocking(db_executor, _account_generation, owner_uid)
except Exception:
return FrameRequestAuthorityDecision(enabled=False, kill_switch=True)
return FrameRequestAuthorityDecision(enabled=True, account_generation=generation)
async def authorize_frame_request(
uid: str,
account_generation: int,
*,
stage: JITDecisionStage,
force_refresh: bool = False,
) -> FrameRequestAuthorityDecision:
decision = await resolve_frame_request_authority(
uid,
stage=stage,
force_refresh=force_refresh,
)
if not decision.enabled or decision.account_generation != account_generation:
raise PermissionError("frame request rollout or account generation mismatch")
return decision
__all__ = [
"FrameRequestAuthorityDecision",
"authorize_frame_request",
"resolve_frame_request_authority",
]