单个匿名访客会话。
| 153 | |
| 154 | @dataclass |
| 155 | class GuestSession: |
| 156 | """单个匿名访客会话。""" |
| 157 | |
| 158 | token: str |
| 159 | user_id: str |
| 160 | username: str |
| 161 | created_at: float = field(default_factory=time.time) |
| 162 | expires_at: float = field(default_factory=_build_session_expiry) |
| 163 | active_requests: int = 0 |
| 164 | valid: bool = True |
| 165 | failure_count: int = 0 |
| 166 | last_failure_time: float = 0.0 |
| 167 | |
| 168 | @property |
| 169 | def age(self) -> float: |
| 170 | """会话存活时间。""" |
| 171 | return time.time() - self.created_at |
| 172 | |
| 173 | @property |
| 174 | def is_expired(self) -> bool: |
| 175 | """判断会话是否已过期。""" |
| 176 | return time.time() >= self.expires_at |
| 177 | |
| 178 | def snapshot(self) -> Dict[str, str]: |
| 179 | """获取当前会话快照。""" |
| 180 | return { |
| 181 | "token": self.token, |
| 182 | "user_id": self.user_id, |
| 183 | "username": self.username, |
| 184 | } |
| 185 | |
| 186 | |
| 187 | class GuestSessionPool: |
no outgoing calls