perf(platform-fulfillment): 优化本地选号与失败任务调度
This commit is contained in:
@@ -25,6 +25,10 @@ AVAILABILITY_EXPIRED = "expired"
|
||||
AVAILABILITY_UNKNOWN = "unknown"
|
||||
|
||||
DEFAULT_CAPABILITY_TTL_SECONDS = 15 * 60
|
||||
DEFAULT_STABLE_CAPABILITY_TTL_SECONDS = 6 * 60 * 60
|
||||
DEFAULT_LIMITED_CAPABILITY_TTL_SECONDS = 24 * 60 * 60
|
||||
DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS = 15 * 60
|
||||
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS = 5 * 60
|
||||
|
||||
|
||||
def _env_positive_int(name: str, default: int) -> int:
|
||||
@@ -35,11 +39,39 @@ def _env_positive_int(name: str, default: int) -> int:
|
||||
return value if value > 0 else default
|
||||
|
||||
|
||||
def capability_ttl_seconds() -> int:
|
||||
return _env_positive_int(
|
||||
def capability_ttl_seconds(availability: str | None = None) -> int:
|
||||
"""按能力稳定性返回快照 TTL。
|
||||
|
||||
30k 级账号池无法在 15 分钟内全量刷新一遍。把稳定的“可兑换/未开放”
|
||||
仍按 15 分钟处理,会让本地能力池长期处于过期状态,选号又退化到逐个
|
||||
实时扫描。库存和未知状态变化快,因此保持短 TTL;账号是否具备活动资格、
|
||||
是否达到限兑上限则使用长 TTL,兑换前仍会再做实时复核。
|
||||
"""
|
||||
generic = _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_STATE_TTL_SECONDS",
|
||||
DEFAULT_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability in {AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN}:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_STABLE_TTL_SECONDS",
|
||||
DEFAULT_STABLE_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_LIMITED:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_LIMITED_TTL_SECONDS",
|
||||
DEFAULT_LIMITED_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_NO_STOCK:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_NO_STOCK_TTL_SECONDS",
|
||||
DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_UNKNOWN:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_UNKNOWN_TTL_SECONDS",
|
||||
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
return generic
|
||||
|
||||
|
||||
def _to_int(value: Any) -> int | None:
|
||||
@@ -310,8 +342,6 @@ def sync_account_goods_states(
|
||||
if not goods_items or account.id is None:
|
||||
return 0
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
ttl_valid_until = moment + timedelta(seconds=capability_ttl_seconds())
|
||||
|
||||
existing_rows = (
|
||||
db.query(AccountCommodityState)
|
||||
.filter(
|
||||
@@ -347,6 +377,9 @@ def sync_account_goods_states(
|
||||
|
||||
availability, reason, raw_status = classify_goods_item(item, now=moment)
|
||||
window = goods_time_window(item, now=moment)
|
||||
ttl_valid_until = moment + timedelta(
|
||||
seconds=capability_ttl_seconds(availability)
|
||||
)
|
||||
last_exchange_at, exchange_count = exchange_stats.get(
|
||||
commodity_id,
|
||||
(None, 0),
|
||||
@@ -392,7 +425,9 @@ def sync_account_goods_states(
|
||||
state.schedule_json = None
|
||||
state.observed_at = moment
|
||||
state.valid_until = None
|
||||
state.next_check_at = ttl_valid_until
|
||||
state.next_check_at = moment + timedelta(
|
||||
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
|
||||
)
|
||||
state.source = source[:24]
|
||||
state.updated_at = moment
|
||||
changed += 1
|
||||
@@ -418,7 +453,7 @@ def record_account_commodity_state(
|
||||
"""写入单个商品的实时检查结论,供下一轮匹配直接使用。"""
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
ttl = (
|
||||
capability_ttl_seconds()
|
||||
capability_ttl_seconds(availability)
|
||||
if valid_for_seconds is None
|
||||
else max(1, int(valid_for_seconds))
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user