perf(douyu): 降低商品能力同步写放大

This commit is contained in:
yml
2026-11-23 06:00:19 +08:00
parent 8e01c3c69e
commit 1e67111715
4 changed files with 157 additions and 65 deletions
@@ -45,6 +45,31 @@ DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS = 15 * 60
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS = 5 * 60
def _set_if_changed(instance: Any, field: str, value: Any) -> bool:
"""只在值真正变化时赋值,避免无意义 UPDATE 写满 binlog。"""
if getattr(instance, field) == value:
return False
setattr(instance, field, value)
return True
def _commodity_state_is_fresh(
state: AccountCommodityState,
moment: datetime,
) -> bool:
return bool(state.next_check_at is not None and state.next_check_at > moment)
def _apply_commodity_state_values(
state: AccountCommodityState,
values: dict[str, Any],
) -> bool:
changed = False
for field, value in values.items():
changed = _set_if_changed(state, field, value) or changed
return changed
def _env_positive_int(name: str, default: int) -> int:
try:
value = int(os.getenv(name, str(default)))
@@ -507,34 +532,42 @@ def sync_account_goods_states(
commodity_id,
(None, 0),
)
state.commodity_name = _commodity_name(item)[:256]
state.account_eligibility = assessment.account_eligibility
state.offer_state = assessment.offer_state
state.stock_state = assessment.stock_state
state.stock_count = assessment.stock_count
state.store_status = assessment.store_status
state.open_status = assessment.open_status
state.availability = assessment.availability
state.blocking_reason = assessment.blocking_reason[:255]
state.raw_status = assessment.raw_status[:16]
state.raw_json = dict(item)
state.time_limited = window.time_limited
state.available_from = window.start
state.available_until = window.end
state.schedule_json = window.schedule or None
state.observed_at = moment
state.valid_until = (
valid_until = (
min(ttl_valid_until, window.next_boundary)
if window.next_boundary is not None
else ttl_valid_until
)
state.next_check_at = state.valid_until
state.last_exchange_at = last_exchange_at
state.exchange_count_365d = int(exchange_count)
state.source = source[:24]
state.error_code = ""
state.updated_at = moment
changed += 1
fresh_before = _commodity_state_is_fresh(state, moment)
data_changed = _apply_commodity_state_values(
state,
{
"commodity_name": _commodity_name(item)[:256],
"account_eligibility": assessment.account_eligibility,
"offer_state": assessment.offer_state,
"stock_state": assessment.stock_state,
"stock_count": assessment.stock_count,
"store_status": assessment.store_status,
"open_status": assessment.open_status,
"availability": assessment.availability,
"blocking_reason": assessment.blocking_reason[:255],
"raw_status": assessment.raw_status[:16],
"raw_json": dict(item),
"time_limited": window.time_limited,
"available_from": window.start,
"available_until": window.end,
"schedule_json": window.schedule or None,
"last_exchange_at": last_exchange_at,
"exchange_count_365d": int(exchange_count),
"source": source[:24],
"error_code": "",
},
)
if data_changed or not fresh_before:
state.observed_at = moment
state.valid_until = valid_until
state.next_check_at = valid_until
state.updated_at = moment
changed += 1
if replace_missing:
for commodity_id, state in by_commodity.items():
@@ -545,27 +578,33 @@ def sync_account_goods_states(
and state.blocking_reason == "官方商品列表本次未返回该商品"
):
continue
state.availability = AVAILABILITY_NOT_OFFERED
state.account_eligibility = ACCOUNT_ELIGIBILITY_UNKNOWN
state.offer_state = OFFER_STATE_NOT_OFFERED
state.stock_state = STOCK_STATE_UNKNOWN
state.stock_count = None
state.store_status = None
state.open_status = None
state.blocking_reason = "官方商品列表本次未返回该商品"
state.raw_json = None
state.time_limited = False
state.available_from = None
state.available_until = None
state.schedule_json = None
state.observed_at = moment
state.valid_until = None
state.next_check_at = moment + timedelta(
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
data_changed = _apply_commodity_state_values(
state,
{
"availability": AVAILABILITY_NOT_OFFERED,
"account_eligibility": ACCOUNT_ELIGIBILITY_UNKNOWN,
"offer_state": OFFER_STATE_NOT_OFFERED,
"stock_state": STOCK_STATE_UNKNOWN,
"stock_count": None,
"store_status": None,
"open_status": None,
"blocking_reason": "官方商品列表本次未返回该商品",
"raw_json": None,
"time_limited": False,
"available_from": None,
"available_until": None,
"schedule_json": None,
"source": source[:24],
},
)
state.source = source[:24]
state.updated_at = moment
changed += 1
if data_changed:
state.observed_at = moment
state.valid_until = None
state.next_check_at = moment + timedelta(
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
)
state.updated_at = moment
changed += 1
db.flush()
return changed
@@ -643,23 +682,30 @@ def record_account_commodity_state(
now=moment,
)
last_exchange_at, exchange_count = exchange_stats.get(commodity_id, (None, 0))
state.account_eligibility = account_eligibility
state.offer_state = offer_state
state.stock_state = stock_state
state.stock_count = stock_count
state.store_status = store_status
state.open_status = open_status
state.availability = availability
state.blocking_reason = blocking_reason[:255]
state.raw_status = raw_status[:16]
state.observed_at = moment
state.valid_until = moment + timedelta(seconds=ttl)
state.next_check_at = state.valid_until
state.last_exchange_at = last_exchange_at
state.exchange_count_365d = int(exchange_count)
state.source = source[:24]
state.error_code = error_code[:64]
state.updated_at = moment
fresh_before = _commodity_state_is_fresh(state, moment)
data_changed = _apply_commodity_state_values(
state,
{
"account_eligibility": account_eligibility,
"offer_state": offer_state,
"stock_state": stock_state,
"stock_count": stock_count,
"store_status": store_status,
"open_status": open_status,
"availability": availability,
"blocking_reason": blocking_reason[:255],
"raw_status": raw_status[:16],
"last_exchange_at": last_exchange_at,
"exchange_count_365d": int(exchange_count),
"source": source[:24],
"error_code": error_code[:64],
},
)
if data_changed or not fresh_before:
state.observed_at = moment
state.valid_until = moment + timedelta(seconds=ttl)
state.next_check_at = state.valid_until
state.updated_at = moment
db.flush()
return state