Files
scrabble-game/backend/internal/payments/cache.go
T
Ilia Denisov 1c06d1d0d1
CI / changes (pull_request) Successful in 2s
CI / unit (pull_request) Successful in 11s
CI / integration (pull_request) Successful in 22s
CI / ui (pull_request) Successful in 1m7s
CI / conformance (pull_request) Successful in 9s
CI / gate (pull_request) Successful in 0s
CI / deploy (pull_request) Successful in 1m41s
feat(payments): chip wallet, store-compliance gate and benefit application
Stand up the internal chip/benefit mechanic behind the narrow payments interface:
context-aware balances and benefits, an atomic chip spend, admin grants as
zero-price value sales, the one-directional store-compliance gate (VK/TG same-
origin only, web draws direct→vk→tg, VK-iOS frozen, untrusted fail-closed), and
per-origin hint and no-ads application with term stacking. Reads are served from
an in-process, account-keyed write-through cache (mirroring the suspension gate),
so hot paths issue no query to the payments schema.

Flip the online-game hint wallet and the ad-banner suppression from the deprecated
accounts.hint_balance / paid_account columns to the payments benefit (a hint
balance no longer suppresses the banner — only a no-ads benefit does), and fold
chip segments and benefits by origin on account merge, inside the merge tx. Add
the GET/POST /api/v1/user/wallet edge chain (REST → Connect → FlatBuffers) plus
its codec unit test; no wallet UI yet.

Bring the frozen owner decisions log into the repo at
docs/PAYMENTS_DECISIONS_ru.md (it was untracked under .vscode) and reference it
from PLAN.md; record the read-cache design and the present-sources interface in
PLAN.md and docs/PAYMENTS.md (+ RU mirror).
2026-07-08 06:06:40 +02:00

73 lines
2.6 KiB
Go

package payments
import (
"sync"
"time"
"github.com/google/uuid"
)
// benefitState is an account's benefit row for one origin: the no-ads term end (nil = none),
// the perpetual forever flag, and the hint count. It is the context-independent stored form;
// the read paths aggregate it over the origins applicable in a context.
type benefitState struct {
adsPaidUntil *time.Time
adsForever bool
hints int
}
// walletState is an account's full payments state: chip balances by source and benefits by
// origin. It is the raw, context-independent data every read path filters per execution
// context — the value the cache holds and the store recomputes from the materialized tables.
type walletState struct {
chips map[Source]int
benefits map[Source]benefitState
}
// chipsOf returns the chip balance for a source, zero when the segment has no row (an absent
// segment reads as zero, §2).
func (w walletState) chipsOf(s Source) int { return w.chips[s] }
// benefitOf returns the benefit for an origin, the zero benefit when the origin has no row.
func (w walletState) benefitOf(o Source) benefitState { return w.benefits[o] }
// walletCache is the payments read model: each account's chip/benefit state, keyed by account
// id, read on every ad-eligibility / hint / wallet / gate request and invalidated on every
// payments mutation (spend, grant, fund, refund, merge). It is a write-through cache in front
// of the materialized balances/benefits tables — the same pattern the account-suspension gate
// uses (backend/internal/account/suspension.go) — so the steady-state hot path issues no query
// to the payments schema. It is single-instance, matching the deployment (one shared Store); a
// multi-instance backend would need a shared cache.
type walletCache struct {
mu sync.RWMutex
m map[uuid.UUID]walletState
}
// newWalletCache constructs an empty cache.
func newWalletCache() *walletCache {
return &walletCache{m: make(map[uuid.UUID]walletState)}
}
// get returns the cached state for an account and whether it was present.
func (c *walletCache) get(id uuid.UUID) (walletState, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
s, ok := c.m[id]
return s, ok
}
// put stores an account's state.
func (c *walletCache) put(id uuid.UUID, s walletState) {
c.mu.Lock()
defer c.mu.Unlock()
c.m[id] = s
}
// invalidate drops an account's entry so the next read reloads it from the materialized tables.
// Called after every mutation once its transaction has committed.
func (c *walletCache) invalidate(id uuid.UUID) {
c.mu.Lock()
defer c.mu.Unlock()
delete(c.m, id)
}