DNF90 服务端与启动器完整源码(含 GM 工具功能增强)
服务端(Go): - GM 塞背包/带属性装备发放:在线角色推送 op14 增量物品行,背包实时刷新,无需回角色选择界面 - GM 改金币/改等级:去掉在线拒绝,落库后推送 op14 钱包/op37 等级状态,在线实时刷新 - 等级端点统一切换到新版 GMSetCharacterLevel(自带在线刷新) - 无限/锁死疲劳模式 + 燃烧疲劳:每房间消耗 2 点、打怪与通关经验双倍 - 燃烧疲劳开关时给所有在线角色推送系统提示(op12 mode 0) - 深渊史诗掉率:GM 全局倍率 + 城镇增益药剂窗口倍率,真实接入掉落结算 启动器(Python/JS): - 标题栏自绘、窗口 1204x717、禁止拖拽改大小、仅最小化/关闭 - GM 工具:疲劳与燃烧、深渊史诗掉率、拍卖行上架(单一定价对齐) - 拍卖行上架前端对齐服务端单一价格语义,修复 400 报错
This commit is contained in:
commit
645b64fe21
4388 files changed
+1405457
No files matched your search
@@ -0,0 +1,12 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"longheng.io/server/internal/platform/app"
|
||||
)
|
||||
|
||||
func Manifest(builder app.Builder, configurers ...app.EnvConfigurer) app.ServiceManifest {
|
||||
return app.ServiceManifest{
|
||||
Configure: append([]app.EnvConfigurer(nil), configurers...),
|
||||
Components: builder,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
histQueueMax = 4096
|
||||
histFlushTimeout = 3 * time.Second
|
||||
histRetryInterval = 200 * time.Millisecond
|
||||
)
|
||||
|
||||
var (
|
||||
errHistStoreNil = errors.New("leaderboard history store is nil")
|
||||
errHistClosed = errors.New("leaderboard history queue is closed")
|
||||
errHistFull = errors.New("leaderboard history queue is full")
|
||||
)
|
||||
|
||||
// AsyncHistoryStore 把排行榜历史写入移出热路径,适用于非 strict 的 SQL/outbox 历史。
|
||||
type AsyncHistoryStore struct {
|
||||
next HistoryStore
|
||||
max int
|
||||
timeout time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
pending []HistoryEntry
|
||||
closed bool
|
||||
flushing bool
|
||||
flushDone chan struct{}
|
||||
errs uint64
|
||||
|
||||
wake chan struct{}
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewAsyncHistoryStore 创建异步排行榜历史存储。
|
||||
func NewAsyncHistoryStore(next HistoryStore) *AsyncHistoryStore {
|
||||
store := &AsyncHistoryStore{
|
||||
next: next,
|
||||
max: histQueueMax,
|
||||
timeout: histFlushTimeout,
|
||||
wake: make(chan struct{}, 1),
|
||||
stop: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go store.run()
|
||||
return store
|
||||
}
|
||||
|
||||
// Append 只入队历史,后台负责慢存储写入。
|
||||
func (s *AsyncHistoryStore) Append(ctx context.Context, entry HistoryEntry) error {
|
||||
if s == nil || s.next == nil {
|
||||
return errHistStoreNil
|
||||
}
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
entry = normHistoryEntry(entry)
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.closed {
|
||||
return errHistClosed
|
||||
}
|
||||
if len(s.pending) >= s.max {
|
||||
atomic.AddUint64(&s.errs, 1)
|
||||
return errHistFull
|
||||
}
|
||||
s.pending = append(s.pending, entry)
|
||||
s.wakeLocked()
|
||||
return nil
|
||||
}
|
||||
|
||||
// List 刷新 pending 后透传底层查询,管理面能读到已排队历史。
|
||||
func (s *AsyncHistoryStore) List(ctx context.Context, leaderboardID string, limit int) ([]HistoryEntry, error) {
|
||||
if s == nil || s.next == nil {
|
||||
return nil, errHistStoreNil
|
||||
}
|
||||
if err := s.Flush(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.next.List(ctx, leaderboardID, limit)
|
||||
}
|
||||
|
||||
// Flush 同步排空待写历史。
|
||||
func (s *AsyncHistoryStore) Flush(ctx context.Context) error {
|
||||
if s == nil || s.next == nil {
|
||||
return errHistStoreNil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
for {
|
||||
batch, wait := s.beginFlush()
|
||||
if wait != nil {
|
||||
if err := waitHistFlush(ctx, wait); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(batch) == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := s.writeBatch(ctx, batch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close 停止后台 worker 并排空待写历史。
|
||||
func (s *AsyncHistoryStore) Close(ctx context.Context) error {
|
||||
if s == nil {
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
closeCtx, cancel := s.closeContext(ctx)
|
||||
defer cancel()
|
||||
s.once.Do(func() {
|
||||
s.mu.Lock()
|
||||
s.closed = true
|
||||
s.mu.Unlock()
|
||||
close(s.stop)
|
||||
})
|
||||
select {
|
||||
case <-s.done:
|
||||
case <-closeCtx.Done():
|
||||
return closeCtx.Err()
|
||||
}
|
||||
return errors.Join(s.Flush(closeCtx), closeHistory(closeCtx, s.next))
|
||||
}
|
||||
|
||||
// HistoryErrors 返回异步写入失败计数。
|
||||
func (s *AsyncHistoryStore) HistoryErrors() uint64 {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
return atomic.LoadUint64(&s.errs)
|
||||
}
|
||||
|
||||
// EventLogErrors 透传被包装历史存储的 outbox 错误计数。
|
||||
func (s *AsyncHistoryStore) EventLogErrors() uint64 {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
return historyStoreErrors(s.next)
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) run() {
|
||||
defer close(s.done)
|
||||
retry := time.NewTimer(time.Hour)
|
||||
stopHistTimer(retry)
|
||||
var retryC <-chan time.Time
|
||||
defer stopHistTimer(retry)
|
||||
for {
|
||||
select {
|
||||
case <-s.wake:
|
||||
if err := s.flushBackground(); err != nil {
|
||||
resetHistTimer(retry, histRetryInterval)
|
||||
retryC = retry.C
|
||||
} else {
|
||||
retryC = nil
|
||||
}
|
||||
case <-retryC:
|
||||
retryC = nil
|
||||
if err := s.flushBackground(); err != nil {
|
||||
resetHistTimer(retry, histRetryInterval)
|
||||
retryC = retry.C
|
||||
}
|
||||
case <-s.stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) flushBackground() error {
|
||||
timeout := s.timeout
|
||||
if timeout <= 0 {
|
||||
timeout = histFlushTimeout
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
err := s.Flush(ctx)
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) closeContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if _, ok := ctx.Deadline(); ok {
|
||||
return ctx, func() {}
|
||||
}
|
||||
timeout := s.timeout
|
||||
if timeout <= 0 {
|
||||
timeout = histFlushTimeout
|
||||
}
|
||||
return context.WithTimeout(ctx, timeout)
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) beginFlush() ([]HistoryEntry, <-chan struct{}) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.flushing {
|
||||
return nil, s.flushDone
|
||||
}
|
||||
if len(s.pending) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
batch := cloneHistoryEntries(s.pending)
|
||||
s.pending = nil
|
||||
s.flushing = true
|
||||
s.flushDone = make(chan struct{})
|
||||
return batch, nil
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) writeBatch(ctx context.Context, batch []HistoryEntry) error {
|
||||
var err error
|
||||
failedAt := len(batch)
|
||||
for i, entry := range batch {
|
||||
if err = ctxErr(ctx); err != nil {
|
||||
failedAt = i
|
||||
break
|
||||
}
|
||||
if err = s.next.Append(ctx, entry); err != nil {
|
||||
failedAt = i
|
||||
break
|
||||
}
|
||||
}
|
||||
s.finishFlush(batch[failedAt:], err)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) finishFlush(failed []HistoryEntry, err error) {
|
||||
s.mu.Lock()
|
||||
if err != nil {
|
||||
atomic.AddUint64(&s.errs, 1)
|
||||
restored := cloneHistoryEntries(failed)
|
||||
restored = append(restored, s.pending...)
|
||||
s.pending = restored
|
||||
}
|
||||
done := s.flushDone
|
||||
s.flushing = false
|
||||
s.flushDone = nil
|
||||
s.mu.Unlock()
|
||||
if done != nil {
|
||||
close(done)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AsyncHistoryStore) wakeLocked() {
|
||||
select {
|
||||
case s.wake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func wrapHistoryStore(store HistoryStore, strict bool) HistoryStore {
|
||||
if strict {
|
||||
return store
|
||||
}
|
||||
switch store.(type) {
|
||||
case nil, *MemoryHistoryStore, *AsyncHistoryStore:
|
||||
return store
|
||||
case *SQLHistoryStore, *EventLogHistoryStore:
|
||||
return NewAsyncHistoryStore(store)
|
||||
default:
|
||||
return store
|
||||
}
|
||||
}
|
||||
|
||||
func flushHistory(ctx context.Context, store HistoryStore) error {
|
||||
if flusher, ok := store.(interface {
|
||||
Flush(context.Context) error
|
||||
}); ok {
|
||||
return flusher.Flush(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func closeHistory(ctx context.Context, store HistoryStore) error {
|
||||
if closer, ok := store.(interface {
|
||||
Close(context.Context) error
|
||||
}); ok {
|
||||
return closer.Close(ctx)
|
||||
}
|
||||
return flushHistory(ctx, store)
|
||||
}
|
||||
|
||||
func waitHistFlush(ctx context.Context, done <-chan struct{}) error {
|
||||
if done == nil {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
case <-done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func resetHistTimer(timer *time.Timer, delay time.Duration) {
|
||||
if timer == nil {
|
||||
return
|
||||
}
|
||||
if !timer.Stop() {
|
||||
select {
|
||||
case <-timer.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
timer.Reset(delay)
|
||||
}
|
||||
|
||||
func stopHistTimer(timer *time.Timer) {
|
||||
if timer == nil {
|
||||
return
|
||||
}
|
||||
if !timer.Stop() {
|
||||
select {
|
||||
case <-timer.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNilEventLogHistoryStore = errors.New("leaderboard eventlog history store is required")
|
||||
ErrNilEventLog = errors.New("leaderboard eventlog is required")
|
||||
)
|
||||
|
||||
const (
|
||||
LeaderboardEventStream = "leaderboard"
|
||||
DefaultLeaderboardHistoryEventType = "history.appended"
|
||||
histUndoTimeout = 3 * time.Second
|
||||
)
|
||||
|
||||
type HistoryStoreOptions struct {
|
||||
Stream string
|
||||
Type string
|
||||
Strict bool
|
||||
}
|
||||
|
||||
type EventLogHistoryStore struct {
|
||||
next HistoryStore
|
||||
log *eventlog.Log
|
||||
stream string
|
||||
typ string
|
||||
strict bool
|
||||
errors uint64
|
||||
}
|
||||
|
||||
type evErrorReporter interface {
|
||||
EventLogErrors() uint64
|
||||
}
|
||||
|
||||
type histErrorReporter interface {
|
||||
HistoryErrors() uint64
|
||||
}
|
||||
|
||||
type histUndo func(context.Context) error
|
||||
|
||||
type histUndoer interface {
|
||||
appendWithRollback(context.Context, HistoryEntry) (histUndo, error)
|
||||
}
|
||||
|
||||
func NewEventLogHistoryStore(next HistoryStore, log *eventlog.Log, options HistoryStoreOptions) (*EventLogHistoryStore, error) {
|
||||
if next == nil {
|
||||
return nil, ErrNilEventLogHistoryStore
|
||||
}
|
||||
if log == nil {
|
||||
return nil, ErrNilEventLog
|
||||
}
|
||||
stream := strings.TrimSpace(options.Stream)
|
||||
if stream == "" {
|
||||
stream = LeaderboardEventStream
|
||||
}
|
||||
typ := strings.TrimSpace(options.Type)
|
||||
if typ == "" {
|
||||
typ = DefaultLeaderboardHistoryEventType
|
||||
}
|
||||
return &EventLogHistoryStore{
|
||||
next: next,
|
||||
log: log,
|
||||
stream: stream,
|
||||
typ: typ,
|
||||
strict: options.Strict,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type eventHistoryStore struct {
|
||||
log *eventlog.Log
|
||||
stream string
|
||||
typ string
|
||||
}
|
||||
|
||||
func newEventHistoryStore(log *eventlog.Log, stream string, typ string) *eventHistoryStore {
|
||||
return &eventHistoryStore{
|
||||
log: log,
|
||||
stream: normalizeEventStream(stream),
|
||||
typ: normHistoryEventType(typ),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EventLogHistoryStore) Append(ctx context.Context, entry HistoryEntry) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.next == nil {
|
||||
return ErrNilEventLogHistoryStore
|
||||
}
|
||||
if s.log == nil {
|
||||
return ErrNilEventLog
|
||||
}
|
||||
entry = normHistoryEntry(entry)
|
||||
payload, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rollback, err := appendHist(ctx, s.next, entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.log.Append(ctx, eventlog.Event{
|
||||
Stream: s.stream,
|
||||
Type: s.typ,
|
||||
AggregateID: entry.LeaderboardID,
|
||||
IdempotencyKey: historyIdemKey(entry, payload),
|
||||
Payload: payload,
|
||||
Headers: map[string]string{
|
||||
"action": entry.Action,
|
||||
"leaderboard_id": entry.LeaderboardID,
|
||||
"owner_id": entry.OwnerID,
|
||||
},
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
atomic.AddUint64(&s.errors, 1)
|
||||
if s.strict {
|
||||
if rollbackErr := undoHist(ctx, rollback); rollbackErr != nil {
|
||||
return errors.Join(err, rollbackErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EventLogHistoryStore) List(ctx context.Context, leaderboardID string, limit int) ([]HistoryEntry, error) {
|
||||
if s == nil || s.next == nil {
|
||||
return nil, ErrNilEventLogHistoryStore
|
||||
}
|
||||
return s.next.List(ctx, leaderboardID, limit)
|
||||
}
|
||||
|
||||
func (s *EventLogHistoryStore) EventLogErrors() uint64 {
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
return atomic.LoadUint64(&s.errors)
|
||||
}
|
||||
|
||||
func (s *eventHistoryStore) Append(ctx context.Context, entry HistoryEntry) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.log == nil {
|
||||
return ErrNilEventLog
|
||||
}
|
||||
entry = normHistoryEntry(entry)
|
||||
payload, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.log.Append(ctx, eventlog.Event{
|
||||
Stream: s.stream,
|
||||
Type: s.typ,
|
||||
AggregateID: entry.LeaderboardID,
|
||||
IdempotencyKey: historyIdemKey(entry, payload),
|
||||
Payload: payload,
|
||||
Headers: map[string]string{
|
||||
"action": entry.Action,
|
||||
"leaderboard_id": entry.LeaderboardID,
|
||||
"owner_id": entry.OwnerID,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *eventHistoryStore) List(context.Context, string, int) ([]HistoryEntry, error) {
|
||||
return nil, ErrNilEventLogHistoryStore
|
||||
}
|
||||
|
||||
func historyStoreErrors(store HistoryStore) uint64 {
|
||||
reporter, ok := store.(evErrorReporter)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return reporter.EventLogErrors()
|
||||
}
|
||||
|
||||
func historyErrors(store HistoryStore) uint64 {
|
||||
reporter, ok := store.(histErrorReporter)
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return reporter.HistoryErrors()
|
||||
}
|
||||
|
||||
func appendHist(ctx context.Context, store HistoryStore, entry HistoryEntry) (histUndo, error) {
|
||||
if rollbacker, ok := store.(histUndoer); ok {
|
||||
return rollbacker.appendWithRollback(ctx, entry)
|
||||
}
|
||||
if err := store.Append(ctx, entry); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func undoHist(parent context.Context, rollback histUndo) error {
|
||||
if rollback == nil {
|
||||
return nil
|
||||
}
|
||||
base := context.Background()
|
||||
if parent != nil {
|
||||
base = context.WithoutCancel(parent)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(base, histUndoTimeout)
|
||||
defer cancel()
|
||||
return rollback(ctx)
|
||||
}
|
||||
|
||||
func normHistoryEntry(entry HistoryEntry) HistoryEntry {
|
||||
entry = cloneHistoryEntry(entry)
|
||||
entry.Action = strings.TrimSpace(entry.Action)
|
||||
entry.LeaderboardID = strings.TrimSpace(entry.LeaderboardID)
|
||||
entry.OwnerID = strings.TrimSpace(entry.OwnerID)
|
||||
if entry.At.IsZero() {
|
||||
entry.At = time.Now().UTC()
|
||||
} else {
|
||||
entry.At = entry.At.UTC()
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func historyIdemKey(entry HistoryEntry, payload []byte) string {
|
||||
raw := fmt.Sprintf("%s|%s|%s|%d|%x",
|
||||
entry.LeaderboardID,
|
||||
entry.Action,
|
||||
entry.OwnerID,
|
||||
entry.At.UTC().UnixNano(),
|
||||
payload,
|
||||
)
|
||||
hash := sha256.Sum256([]byte(raw))
|
||||
return "leaderboard:history:" + hex.EncodeToString(hash[:16])
|
||||
}
|
||||
@@ -0,0 +1,278 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func normalizeSubmission(submission Submission) (Submission, error) {
|
||||
submission.OwnerID = strings.TrimSpace(submission.OwnerID)
|
||||
if submission.OwnerID == "" {
|
||||
return Submission{}, fmt.Errorf("%w: owner_id is required", ErrInvalidSubmission)
|
||||
}
|
||||
submission.Metadata = cloneStringMap(submission.Metadata)
|
||||
return submission, nil
|
||||
}
|
||||
|
||||
func addScoreValue(left int64, right int64) (int64, error) {
|
||||
if (right > 0 && left > math.MaxInt64-right) || (right < 0 && left < math.MinInt64-right) {
|
||||
return 0, fmt.Errorf("%w: %w", ErrInvalidSubmission, ErrScoreOverflow)
|
||||
}
|
||||
return left + right, nil
|
||||
}
|
||||
|
||||
func normRepairReq(request RepairRequest) (RepairRequest, error) {
|
||||
request.OwnerID = strings.TrimSpace(request.OwnerID)
|
||||
if request.OwnerID == "" {
|
||||
return RepairRequest{}, fmt.Errorf("%w: owner_id is required", ErrInvalidSubmission)
|
||||
}
|
||||
request.Reason = strings.TrimSpace(request.Reason)
|
||||
request.OperatorID = strings.TrimSpace(request.OperatorID)
|
||||
request.RequestID = strings.TrimSpace(request.RequestID)
|
||||
request.Metadata = cloneStringMap(request.Metadata)
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func repairIdempotencyKey(leaderboardID string, requestID string) string {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
requestID = strings.TrimSpace(requestID)
|
||||
if leaderboardID == "" || requestID == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join([]string{"v1", encodeKeyPart(leaderboardID), encodeKeyPart(requestID)}, ":")
|
||||
}
|
||||
|
||||
func encodeKeyPart(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "_"
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(value))
|
||||
}
|
||||
|
||||
func normCaptureOpts(options CaptureOptions) CaptureOptions {
|
||||
if options.Limit <= 0 {
|
||||
options.Limit = defaultListLimit
|
||||
}
|
||||
if options.Limit > maxListLimit {
|
||||
options.Limit = maxListLimit
|
||||
}
|
||||
options.Reason = strings.TrimSpace(options.Reason)
|
||||
options.OperatorID = strings.TrimSpace(options.OperatorID)
|
||||
options.RequestID = strings.TrimSpace(options.RequestID)
|
||||
options.Metadata = cloneStringMap(options.Metadata)
|
||||
return options
|
||||
}
|
||||
|
||||
func normalizeSortOrder(sortOrder string) (string, error) {
|
||||
sortOrder = strings.ToLower(strings.TrimSpace(sortOrder))
|
||||
switch sortOrder {
|
||||
case "", SortDescending:
|
||||
return SortDescending, nil
|
||||
case SortAscending:
|
||||
return SortAscending, nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: unsupported sort_order %q", ErrInvalidDefinition, sortOrder)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeOperator(operator string) (string, error) {
|
||||
operator = strings.ToLower(strings.TrimSpace(operator))
|
||||
switch operator {
|
||||
case "", OperatorBest:
|
||||
return OperatorBest, nil
|
||||
case OperatorSet, OperatorIncrement:
|
||||
return operator, nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: unsupported operator %q", ErrInvalidDefinition, operator)
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeListOptions(options ListOptions) (int, int) {
|
||||
offset := options.Offset
|
||||
if offset < 0 {
|
||||
offset = 0
|
||||
}
|
||||
limit := options.Limit
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
return offset, limit
|
||||
}
|
||||
|
||||
func betterThan(candidate Record, existing Record, sortOrder string) bool {
|
||||
switch {
|
||||
case candidate.Score != existing.Score:
|
||||
if sortOrder == SortAscending {
|
||||
return candidate.Score < existing.Score
|
||||
}
|
||||
return candidate.Score > existing.Score
|
||||
case candidate.Subscore != existing.Subscore:
|
||||
if sortOrder == SortAscending {
|
||||
return candidate.Subscore < existing.Subscore
|
||||
}
|
||||
return candidate.Subscore > existing.Subscore
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func compareRecords(a Record, b Record, sortOrder string) int {
|
||||
if a.Score != b.Score {
|
||||
if sortOrder == SortAscending {
|
||||
if a.Score < b.Score {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if a.Score > b.Score {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if a.Subscore != b.Subscore {
|
||||
if sortOrder == SortAscending {
|
||||
if a.Subscore < b.Subscore {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if a.Subscore > b.Subscore {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if !a.CreatedAt.Equal(b.CreatedAt) {
|
||||
if a.CreatedAt.Before(b.CreatedAt) {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if a.OwnerID < b.OwnerID {
|
||||
return -1
|
||||
}
|
||||
if a.OwnerID > b.OwnerID {
|
||||
return 1
|
||||
}
|
||||
if a.LeaderboardID < b.LeaderboardID {
|
||||
return -1
|
||||
}
|
||||
if a.LeaderboardID > b.LeaderboardID {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func cloneDefinition(definition Definition) Definition {
|
||||
definition.Metadata = cloneStringMap(definition.Metadata)
|
||||
return definition
|
||||
}
|
||||
|
||||
func cloneRecord(record Record) Record {
|
||||
record.Metadata = cloneStringMap(record.Metadata)
|
||||
return record
|
||||
}
|
||||
|
||||
func cloneRecords(records []Record) []Record {
|
||||
out := make([]Record, len(records))
|
||||
for i, record := range records {
|
||||
out[i] = cloneRecord(record)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneDefinitionMap(definitions map[string]Definition) map[string]Definition {
|
||||
out := make(map[string]Definition, len(definitions))
|
||||
for id, definition := range definitions {
|
||||
out[id] = cloneDefinition(definition)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRecordMap(records map[string]map[string]Record) map[string]map[string]Record {
|
||||
out := make(map[string]map[string]Record, len(records))
|
||||
for leaderboardID, boardRecords := range records {
|
||||
out[leaderboardID] = cloneRecordSet(boardRecords)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRecordSet(records map[string]Record) map[string]Record {
|
||||
out := make(map[string]Record, len(records))
|
||||
for ownerID, record := range records {
|
||||
out[ownerID] = cloneRecord(record)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRepairMap(repairs map[string]RepairReceipt) map[string]RepairReceipt {
|
||||
out := make(map[string]RepairReceipt, len(repairs))
|
||||
for key, receipt := range repairs {
|
||||
out[key] = cloneRepairReceipt(receipt)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRepairReceipt(receipt RepairReceipt) RepairReceipt {
|
||||
if receipt.Before != nil {
|
||||
before := cloneRecord(*receipt.Before)
|
||||
receipt.Before = &before
|
||||
}
|
||||
if receipt.After != nil {
|
||||
after := cloneRecord(*receipt.After)
|
||||
receipt.After = &after
|
||||
}
|
||||
receipt.Metadata = cloneStringMap(receipt.Metadata)
|
||||
return receipt
|
||||
}
|
||||
|
||||
func cloneCapture(capture Capture) Capture {
|
||||
capture.Records = cloneRecords(capture.Records)
|
||||
capture.Metadata = cloneStringMap(capture.Metadata)
|
||||
return capture
|
||||
}
|
||||
|
||||
func repairHistoryEntry(receipt RepairReceipt, record *Record) HistoryEntry {
|
||||
entry := HistoryEntry{
|
||||
Action: HistoryActionRepairRecord,
|
||||
LeaderboardID: receipt.LeaderboardID,
|
||||
OwnerID: receipt.OwnerID,
|
||||
Reason: receipt.Reason,
|
||||
OperatorID: receipt.OperatorID,
|
||||
RequestID: receipt.RequestID,
|
||||
Metadata: cloneStringMap(receipt.Metadata),
|
||||
At: receipt.At,
|
||||
}
|
||||
if record != nil {
|
||||
cloned := cloneRecord(*record)
|
||||
entry.Record = &cloned
|
||||
} else if receipt.Before != nil {
|
||||
cloned := cloneRecord(*receipt.Before)
|
||||
entry.Record = &cloned
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func cloneStringMap(in map[string]string) map[string]string {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(in))
|
||||
for key, value := range in {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func ctxErr(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -0,0 +1,433 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNilHistoryStoreDB = errors.New("leaderboard history sql db is required")
|
||||
ErrInvalidTableName = errors.New("leaderboard history table name is invalid")
|
||||
ErrHistoryAppendRollbackMissed = errors.New("leaderboard history append rollback missed")
|
||||
ErrSQLHistoryRows = errors.New("leaderboard sql history rows are required")
|
||||
)
|
||||
|
||||
const defaultHistoryTable = "leaderboard_history"
|
||||
|
||||
type MemoryHistoryStore struct {
|
||||
mu sync.Mutex
|
||||
limit int
|
||||
entries map[string][]HistoryEntry
|
||||
}
|
||||
|
||||
func NewMemoryHistoryStore(limit int) *MemoryHistoryStore {
|
||||
return &MemoryHistoryStore{
|
||||
limit: limit,
|
||||
entries: make(map[string][]HistoryEntry),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryHistoryStore) Append(ctx context.Context, entry HistoryEntry) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil {
|
||||
return errors.New("leaderboard memory history store is nil")
|
||||
}
|
||||
entry, err := normHistStore(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.appendLocked(entry)
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryHistoryStore) appendWithRollback(ctx context.Context, entry HistoryEntry) (histUndo, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil {
|
||||
return nil, errors.New("leaderboard memory history store is nil")
|
||||
}
|
||||
entry, err := normHistStore(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
leaderboardID := entry.LeaderboardID
|
||||
s.mu.Lock()
|
||||
before := cloneHistoryEntries(s.entries[leaderboardID])
|
||||
s.appendLocked(entry)
|
||||
s.mu.Unlock()
|
||||
return func(ctx context.Context) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.Lock()
|
||||
if len(before) == 0 {
|
||||
delete(s.entries, leaderboardID)
|
||||
} else {
|
||||
s.entries[leaderboardID] = cloneHistoryEntries(before)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MemoryHistoryStore) appendLocked(entry HistoryEntry) {
|
||||
if s.entries == nil {
|
||||
s.entries = make(map[string][]HistoryEntry)
|
||||
}
|
||||
s.entries[entry.LeaderboardID] = append(s.entries[entry.LeaderboardID], cloneHistoryEntry(entry))
|
||||
if s.limit > 0 && len(s.entries[entry.LeaderboardID]) > s.limit {
|
||||
s.entries[entry.LeaderboardID] = append([]HistoryEntry(nil), s.entries[entry.LeaderboardID][len(s.entries[entry.LeaderboardID])-s.limit:]...)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryHistoryStore) List(ctx context.Context, leaderboardID string, limit int) ([]HistoryEntry, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil {
|
||||
return nil, errors.New("leaderboard memory history store is nil")
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
s.mu.Lock()
|
||||
entries := s.entries[leaderboardID]
|
||||
start := 0
|
||||
if limit > 0 && len(entries) > limit {
|
||||
start = len(entries) - limit
|
||||
}
|
||||
out := cloneHistoryEntries(entries[start:])
|
||||
s.mu.Unlock()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type SQLHistoryRows interface {
|
||||
Next() bool
|
||||
Scan(...any) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
type SQLHistoryDB interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
QueryHistoryContext(context.Context, string, ...any) (SQLHistoryRows, error)
|
||||
}
|
||||
|
||||
type SQLHistoryDBAdapter struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (a SQLHistoryDBAdapter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
if a.DB == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
return a.DB.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (a SQLHistoryDBAdapter) QueryHistoryContext(ctx context.Context, query string, args ...any) (SQLHistoryRows, error) { //nolint:rowserrcheck // 适配层只返回 rows,调用方负责遍历结束后的 Err 检查。
|
||||
if a.DB == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
return a.DB.QueryContext(ctx, query, args...) //nolint:rowserrcheck // rows 返回后由上层统一关闭并检查 Err。
|
||||
}
|
||||
|
||||
type SQLHistoryStoreOptions struct {
|
||||
Table string
|
||||
}
|
||||
|
||||
type SQLHistoryStore struct {
|
||||
db SQLHistoryDB
|
||||
table string
|
||||
}
|
||||
|
||||
func NewSQLHistoryStore(db SQLHistoryDB, options SQLHistoryStoreOptions) (*SQLHistoryStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
table, err := normHistoryTable(options.Table)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SQLHistoryStore{db: db, table: table}, nil
|
||||
}
|
||||
|
||||
func NewSQLHistoryStoreFromDB(db *sql.DB, options SQLHistoryStoreOptions) (*SQLHistoryStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
return NewSQLHistoryStore(SQLHistoryDBAdapter{DB: db}, options)
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) EnsureSchema(ctx context.Context) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNilHistoryStoreDB
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, s.Schema())
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) Schema() string {
|
||||
table := quoteHistoryID(s.table)
|
||||
return fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
leaderboard_id VARCHAR(128) NOT NULL,
|
||||
owner_id VARCHAR(128) NOT NULL DEFAULT '',
|
||||
action VARCHAR(64) NOT NULL,
|
||||
record_json JSON NULL,
|
||||
definition_json JSON NULL,
|
||||
entry_json JSON NOT NULL,
|
||||
created_at DATETIME(6) NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_leaderboard_history_board_id (leaderboard_id, id),
|
||||
KEY idx_leaderboard_history_owner_id (owner_id, id),
|
||||
KEY idx_leaderboard_history_action_id (action, id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, table)
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) Append(ctx context.Context, entry HistoryEntry) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNilHistoryStoreDB
|
||||
}
|
||||
_, err := s.appendWithRollback(ctx, entry)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) appendWithRollback(ctx context.Context, entry HistoryEntry) (histUndo, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
entry, err := normHistStore(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entryJSON, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recordJSON, err := nullableJSON(entry.Record)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definitionJSON, err := nullableJSON(entry.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := s.db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteHistoryID(s.table)+" (leaderboard_id, owner_id, action, record_json, definition_json, entry_json, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
entry.LeaderboardID,
|
||||
entry.OwnerID,
|
||||
entry.Action,
|
||||
recordJSON,
|
||||
definitionJSON,
|
||||
string(entryJSON),
|
||||
entry.At,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return func(ctx context.Context) error {
|
||||
return s.rollbackAppend(ctx, entry, result)
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) rollbackAppend(ctx context.Context, entry HistoryEntry, result sql.Result) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNilHistoryStoreDB
|
||||
}
|
||||
table := quoteHistoryID(s.table)
|
||||
if result != nil {
|
||||
if id, err := result.LastInsertId(); err == nil && id > 0 {
|
||||
deleted, err := s.db.ExecContext(ctx,
|
||||
"DELETE FROM "+table+" WHERE id = ? AND leaderboard_id = ? AND owner_id = ? AND action = ?",
|
||||
id,
|
||||
entry.LeaderboardID,
|
||||
entry.OwnerID,
|
||||
entry.Action,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return checkHistUndo(deleted)
|
||||
}
|
||||
}
|
||||
deleted, err := s.db.ExecContext(ctx,
|
||||
"DELETE FROM "+table+" WHERE id = (SELECT id FROM (SELECT id FROM "+table+" WHERE leaderboard_id = ? AND owner_id = ? AND action = ? AND created_at = ? ORDER BY id DESC LIMIT 1) AS rollback_candidate)",
|
||||
entry.LeaderboardID,
|
||||
entry.OwnerID,
|
||||
entry.Action,
|
||||
entry.At,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return checkHistUndo(deleted)
|
||||
}
|
||||
|
||||
func checkHistUndo(result sql.Result) error {
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrHistoryAppendRollbackMissed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func normHistStore(entry HistoryEntry) (HistoryEntry, error) {
|
||||
entry = cloneHistoryEntry(entry)
|
||||
entry.LeaderboardID = strings.TrimSpace(entry.LeaderboardID)
|
||||
if entry.LeaderboardID == "" {
|
||||
return HistoryEntry{}, ErrLeaderboardNotFound
|
||||
}
|
||||
if entry.At.IsZero() {
|
||||
entry.At = time.Now().UTC()
|
||||
} else {
|
||||
entry.At = entry.At.UTC()
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func (s *SQLHistoryStore) List(ctx context.Context, leaderboardID string, limit int) (out []HistoryEntry, err error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return nil, ErrNilHistoryStoreDB
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
rows, err := s.db.QueryHistoryContext(ctx,
|
||||
"SELECT entry_json FROM "+quoteHistoryID(s.table)+" WHERE leaderboard_id = ? ORDER BY id DESC LIMIT ?",
|
||||
leaderboardID,
|
||||
limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rows == nil {
|
||||
return nil, ErrSQLHistoryRows
|
||||
}
|
||||
defer closeHistRowsErr(rows, &err)
|
||||
out = make([]HistoryEntry, 0, limit)
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var entry HistoryEntry
|
||||
if err := json.Unmarshal(data, &entry); err != nil {
|
||||
return nil, fmt.Errorf("decode leaderboard sql history: %w", err)
|
||||
}
|
||||
out = append(out, cloneHistoryEntry(entry))
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reverseHistory(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func closeHistRowsErr(rows SQLHistoryRows, err *error) {
|
||||
if rows == nil || err == nil {
|
||||
return
|
||||
}
|
||||
if closeErr := rows.Close(); closeErr != nil && *err == nil {
|
||||
*err = closeErr
|
||||
}
|
||||
}
|
||||
|
||||
func cloneHistoryEntry(entry HistoryEntry) HistoryEntry {
|
||||
if entry.Record != nil {
|
||||
record := cloneRecord(*entry.Record)
|
||||
entry.Record = &record
|
||||
}
|
||||
entry.Records = cloneRecords(entry.Records)
|
||||
if entry.Definition != nil {
|
||||
definition := cloneDefinition(*entry.Definition)
|
||||
entry.Definition = &definition
|
||||
}
|
||||
entry.Metadata = cloneStringMap(entry.Metadata)
|
||||
return entry
|
||||
}
|
||||
|
||||
func cloneHistoryEntries(entries []HistoryEntry) []HistoryEntry {
|
||||
out := make([]HistoryEntry, len(entries))
|
||||
for i, entry := range entries {
|
||||
out[i] = cloneHistoryEntry(entry)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func reverseHistory(entries []HistoryEntry) {
|
||||
for i, j := 0, len(entries)-1; i < j; i, j = i+1, j-1 {
|
||||
entries[i], entries[j] = entries[j], entries[i]
|
||||
}
|
||||
}
|
||||
|
||||
func nullableJSON(value any) (any, error) {
|
||||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func normHistoryTable(table string) (string, error) {
|
||||
table = strings.TrimSpace(table)
|
||||
if table == "" {
|
||||
table = defaultHistoryTable
|
||||
}
|
||||
if !histTablePattern.MatchString(table) {
|
||||
return "", ErrInvalidTableName
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
||||
func quoteHistoryID(identifier string) string {
|
||||
return "`" + identifier + "`"
|
||||
}
|
||||
|
||||
var histTablePattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
@@ -0,0 +1,865 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Manager struct {
|
||||
name string
|
||||
now func() time.Time
|
||||
gen func(prefix string) string
|
||||
hist HistoryStore
|
||||
|
||||
historyStrict bool
|
||||
historyErrors int
|
||||
|
||||
mu sync.Mutex
|
||||
next uint64
|
||||
definitions map[string]Definition
|
||||
records map[string]map[string]Record
|
||||
repairs map[string]RepairReceipt
|
||||
}
|
||||
|
||||
type managerStateSnapshot struct {
|
||||
definitions map[string]Definition
|
||||
records map[string]map[string]Record
|
||||
repairs map[string]RepairReceipt
|
||||
}
|
||||
|
||||
type boardRankData struct {
|
||||
definition Definition
|
||||
records map[string]Record
|
||||
}
|
||||
|
||||
func New(options Options) *Manager {
|
||||
name := strings.TrimSpace(options.Name)
|
||||
if name == "" {
|
||||
name = "leaderboard"
|
||||
}
|
||||
now := options.Now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
historyStore := options.HistoryStore
|
||||
if historyStore == nil && options.HistoryLimit >= 0 {
|
||||
historyLimit := options.HistoryLimit
|
||||
if historyLimit == 0 {
|
||||
historyLimit = defaultHistoryLimit
|
||||
}
|
||||
historyStore = NewMemoryHistoryStore(historyLimit)
|
||||
}
|
||||
historyStore = wrapHistoryStore(historyStore, options.HistoryStrict)
|
||||
return &Manager{
|
||||
name: name,
|
||||
now: now,
|
||||
gen: options.IDGenerator,
|
||||
hist: historyStore,
|
||||
historyStrict: options.HistoryStrict,
|
||||
definitions: make(map[string]Definition),
|
||||
records: make(map[string]map[string]Record),
|
||||
repairs: make(map[string]RepairReceipt),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) Create(ctx context.Context, definition Definition) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Definition{}, ErrManagerRequired
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
normalized, err := m.normDefinitionLocked(definition)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
if _, exists := m.definitions[normalized.ID]; exists {
|
||||
return Definition{}, ErrLeaderboardExists
|
||||
}
|
||||
before := m.snapshotStateLocked()
|
||||
m.definitions[normalized.ID] = normalized
|
||||
m.records[normalized.ID] = make(map[string]Record)
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionCreate, normalized.ID, nil, &normalized, normalized.CreatedAt); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Definition{}, err
|
||||
}
|
||||
return cloneDefinition(normalized), nil
|
||||
}
|
||||
|
||||
func (m *Manager) Delete(ctx context.Context, leaderboardID string) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Definition{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
if !ok {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
before := m.snapshotStateLocked()
|
||||
delete(m.definitions, leaderboardID)
|
||||
delete(m.records, leaderboardID)
|
||||
if err := m.appendHistoryLocked(ctx, HistoryDeleteBoard, definition.ID, nil, &definition, m.now().UTC()); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Definition{}, err
|
||||
}
|
||||
return cloneDefinition(definition), nil
|
||||
}
|
||||
|
||||
func (m *Manager) Definition(leaderboardID string) (Definition, bool) {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if m == nil || leaderboardID == "" {
|
||||
return Definition{}, false
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
return cloneDefinition(definition), ok
|
||||
}
|
||||
|
||||
func (m *Manager) Definitions() []Definition {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
out := make([]Definition, 0, len(m.definitions))
|
||||
for _, definition := range m.definitions {
|
||||
out = append(out, cloneDefinition(definition))
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
return out
|
||||
}
|
||||
|
||||
func (m *Manager) Submit(ctx context.Context, leaderboardID string, submission Submission) (Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Record{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
if !ok {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized, err := normalizeSubmission(submission)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
now := m.now().UTC()
|
||||
recordMap := m.records[leaderboardID]
|
||||
existing, exists := recordMap[normalized.OwnerID]
|
||||
record := Record{
|
||||
LeaderboardID: leaderboardID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Score: normalized.Score,
|
||||
Subscore: normalized.Subscore,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if exists {
|
||||
record.CreatedAt = existing.CreatedAt
|
||||
switch definition.Operator {
|
||||
case OperatorBest:
|
||||
candidate := record
|
||||
if !betterThan(candidate, existing, definition.SortOrder) {
|
||||
// Best 模式只接受更优成绩;较差提交返回当前排名,不写历史噪音。
|
||||
ranked, ok := m.recordWithRankLocked(leaderboardID, normalized.OwnerID)
|
||||
if !ok {
|
||||
return Record{}, ErrRecordNotFound
|
||||
}
|
||||
return ranked, nil
|
||||
}
|
||||
case OperatorIncrement:
|
||||
// Increment 模式用于累计榜,调用方要保证加分来源已经在关键写路径里做幂等。
|
||||
record.Score, err = addScoreValue(existing.Score, normalized.Score)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
record.Subscore, err = addScoreValue(existing.Subscore, normalized.Subscore)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
case OperatorSet:
|
||||
default:
|
||||
return Record{}, fmt.Errorf("%w: unsupported operator %q", ErrInvalidDefinition, definition.Operator)
|
||||
}
|
||||
}
|
||||
before := m.snapshotStateLocked()
|
||||
recordMap[normalized.OwnerID] = record
|
||||
var trimmed []Record
|
||||
if definition.MaxSize > 0 {
|
||||
// 内存实现按 MaxSize 裁剪只保证当前进程视角;跨服榜生产应使用 Redis/SQL 方案。
|
||||
trimmed = m.trimLocked(definition)
|
||||
}
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionSubmit, definition.ID, &record, nil, record.UpdatedAt); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Record{}, err
|
||||
}
|
||||
for _, trimmedRecord := range trimmed {
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionTrimRecord, definition.ID, &trimmedRecord, nil, m.now().UTC()); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Record{}, err
|
||||
}
|
||||
}
|
||||
ranked, ok := m.recordWithRankLocked(leaderboardID, normalized.OwnerID)
|
||||
if !ok {
|
||||
return Record{}, ErrRecordTrimmed
|
||||
}
|
||||
return ranked, nil
|
||||
}
|
||||
|
||||
func (m *Manager) DeleteRecord(ctx context.Context, leaderboardID string, ownerID string) (Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Record{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
if ownerID == "" {
|
||||
return Record{}, ErrRecordNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
if _, ok := m.definitions[leaderboardID]; !ok {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
record, ok := m.recordWithRankLocked(leaderboardID, ownerID)
|
||||
if !ok {
|
||||
return Record{}, ErrRecordNotFound
|
||||
}
|
||||
before := m.snapshotStateLocked()
|
||||
delete(m.records[leaderboardID], ownerID)
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionDeleteRecord, leaderboardID, &record, nil, m.now().UTC()); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Record{}, err
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Repair(ctx context.Context, leaderboardID string, request RepairRequest) (RepairReceipt, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return RepairReceipt{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return RepairReceipt{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized, err := normRepairReq(request)
|
||||
if err != nil {
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
if !ok {
|
||||
return RepairReceipt{}, ErrLeaderboardNotFound
|
||||
}
|
||||
repairKey := repairIdempotencyKey(leaderboardID, normalized.RequestID)
|
||||
if repairKey != "" {
|
||||
// GM 修榜必须支持 request_id 重放,避免审批系统或网络重试重复修改成绩。
|
||||
if previous, ok := m.repairs[repairKey]; ok {
|
||||
return cloneRepairReceipt(previous), nil
|
||||
}
|
||||
}
|
||||
now := m.now().UTC()
|
||||
receipt := RepairReceipt{
|
||||
LeaderboardID: leaderboardID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Action: HistoryActionRepairRecord,
|
||||
Reason: normalized.Reason,
|
||||
OperatorID: normalized.OperatorID,
|
||||
RequestID: normalized.RequestID,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
At: now,
|
||||
}
|
||||
if before, found := m.recordWithRankLocked(leaderboardID, normalized.OwnerID); found {
|
||||
before := before
|
||||
receipt.Before = &before
|
||||
}
|
||||
stateBefore := m.snapshotStateLocked()
|
||||
if normalized.Delete {
|
||||
if receipt.Before == nil {
|
||||
return RepairReceipt{}, ErrRecordNotFound
|
||||
}
|
||||
// 删除和改分都写 history,方便后续审计、回滚和跨节点重建。
|
||||
delete(m.records[leaderboardID], normalized.OwnerID)
|
||||
if err := m.appendHistoryEntry(ctx, repairHistoryEntry(receipt, nil)); err != nil {
|
||||
m.restoreStateLocked(stateBefore)
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
if repairKey != "" {
|
||||
m.repairs[repairKey] = cloneRepairReceipt(receipt)
|
||||
}
|
||||
return cloneRepairReceipt(receipt), nil
|
||||
}
|
||||
createdAt := now
|
||||
if receipt.Before != nil && !normalized.ResetCreatedAt {
|
||||
createdAt = receipt.Before.CreatedAt
|
||||
}
|
||||
record := Record{
|
||||
LeaderboardID: leaderboardID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Score: normalized.Score,
|
||||
Subscore: normalized.Subscore,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CreatedAt: createdAt,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
m.records[leaderboardID][record.OwnerID] = record
|
||||
trimmed := m.trimLocked(definition)
|
||||
if after, found := m.recordWithRankLocked(leaderboardID, record.OwnerID); found {
|
||||
after := after
|
||||
receipt.After = &after
|
||||
}
|
||||
if err := m.appendHistoryEntry(ctx, repairHistoryEntry(receipt, receipt.After)); err != nil {
|
||||
m.restoreStateLocked(stateBefore)
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
for _, trimmedRecord := range trimmed {
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionTrimRecord, definition.ID, &trimmedRecord, nil, now); err != nil {
|
||||
m.restoreStateLocked(stateBefore)
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
}
|
||||
if repairKey != "" {
|
||||
m.repairs[repairKey] = cloneRepairReceipt(receipt)
|
||||
}
|
||||
return cloneRepairReceipt(receipt), nil
|
||||
}
|
||||
|
||||
func (m *Manager) Record(leaderboardID string, ownerID string) (Record, bool) {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if m == nil || leaderboardID == "" || ownerID == "" {
|
||||
return Record{}, false
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
data, ok := m.rankDataLocked(leaderboardID)
|
||||
m.mu.Unlock()
|
||||
if !ok {
|
||||
return Record{}, false
|
||||
}
|
||||
return recordWithRank(data.definition, data.records, ownerID)
|
||||
}
|
||||
|
||||
func (m *Manager) List(ctx context.Context, leaderboardID string, options ListOptions) ([]Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil {
|
||||
return nil, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
data, ok := m.rankDataLocked(leaderboardID)
|
||||
m.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
ranked := rankRecords(data.definition, data.records)
|
||||
offset, limit := normalizeListOptions(options)
|
||||
if offset >= len(ranked) {
|
||||
return []Record{}, nil
|
||||
}
|
||||
end := offset + limit
|
||||
if end > len(ranked) {
|
||||
end = len(ranked)
|
||||
}
|
||||
return cloneRecords(ranked[offset:end]), nil
|
||||
}
|
||||
|
||||
func (m *Manager) AroundOwner(ctx context.Context, leaderboardID string, ownerID string, limit int) ([]Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil {
|
||||
return nil, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if ownerID == "" {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
data, ok := m.rankDataLocked(leaderboardID)
|
||||
m.mu.Unlock()
|
||||
if !ok {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
ranked := rankRecords(data.definition, data.records)
|
||||
idx := -1
|
||||
for i, record := range ranked {
|
||||
if record.OwnerID == ownerID {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultAroundLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
if limit > len(ranked) {
|
||||
limit = len(ranked)
|
||||
}
|
||||
start := idx - limit/2
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(ranked) {
|
||||
end = len(ranked)
|
||||
start = end - limit
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
}
|
||||
return cloneRecords(ranked[start:end]), nil
|
||||
}
|
||||
|
||||
func (m *Manager) Rank(ctx context.Context, leaderboardID string, ownerID string) (int, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if m == nil {
|
||||
return 0, false, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" {
|
||||
return 0, false, ErrLeaderboardNotFound
|
||||
}
|
||||
if ownerID == "" {
|
||||
return 0, false, nil
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
data, ok := m.rankDataLocked(leaderboardID)
|
||||
m.mu.Unlock()
|
||||
if !ok {
|
||||
return 0, false, ErrLeaderboardNotFound
|
||||
}
|
||||
record, ok := recordWithRank(data.definition, data.records, ownerID)
|
||||
return record.Rank, ok, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Reset(ctx context.Context, leaderboardID string) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Definition{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
if !ok {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
before := m.snapshotStateLocked()
|
||||
definition.UpdatedAt = m.now().UTC()
|
||||
m.definitions[leaderboardID] = definition
|
||||
m.records[leaderboardID] = make(map[string]Record)
|
||||
if err := m.appendHistoryLocked(ctx, HistoryActionReset, definition.ID, nil, &definition, definition.UpdatedAt); err != nil {
|
||||
m.restoreStateLocked(before)
|
||||
return Definition{}, err
|
||||
}
|
||||
return cloneDefinition(definition), nil
|
||||
}
|
||||
|
||||
func (m *Manager) Capture(ctx context.Context, leaderboardID string, options CaptureOptions) (Capture, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Capture{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return Capture{}, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Capture{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized := normCaptureOpts(options)
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
if _, ok := m.definitions[leaderboardID]; !ok {
|
||||
return Capture{}, ErrLeaderboardNotFound
|
||||
}
|
||||
ranked := m.rankedRecordsLocked(leaderboardID)
|
||||
limit := normalized.Limit
|
||||
if limit > len(ranked) {
|
||||
limit = len(ranked)
|
||||
}
|
||||
capture := Capture{
|
||||
LeaderboardID: leaderboardID,
|
||||
Records: cloneRecords(ranked[:limit]),
|
||||
RecordCount: len(ranked),
|
||||
Reason: normalized.Reason,
|
||||
OperatorID: normalized.OperatorID,
|
||||
RequestID: normalized.RequestID,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CapturedAt: m.now().UTC(),
|
||||
}
|
||||
if err := m.appendHistoryEntry(ctx, HistoryEntry{
|
||||
Action: HistoryActionCapture,
|
||||
LeaderboardID: leaderboardID,
|
||||
Records: capture.Records,
|
||||
Reason: capture.Reason,
|
||||
OperatorID: capture.OperatorID,
|
||||
RequestID: capture.RequestID,
|
||||
Metadata: capture.Metadata,
|
||||
At: capture.CapturedAt,
|
||||
}); err != nil {
|
||||
return Capture{}, err
|
||||
}
|
||||
return cloneCapture(capture), nil
|
||||
}
|
||||
|
||||
func (m *Manager) History(ctx context.Context, leaderboardID string, limit int) ([]HistoryEntry, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil {
|
||||
return nil, ErrManagerRequired
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
store := m.hist
|
||||
strict := m.historyStrict
|
||||
m.mu.Unlock()
|
||||
if store == nil {
|
||||
return []HistoryEntry{}, nil
|
||||
}
|
||||
history, err := store.List(ctx, leaderboardID, limit)
|
||||
if err != nil {
|
||||
m.mu.Lock()
|
||||
m.historyErrors++
|
||||
m.mu.Unlock()
|
||||
if strict {
|
||||
return nil, err
|
||||
}
|
||||
return []HistoryEntry{}, nil
|
||||
}
|
||||
return history, nil
|
||||
}
|
||||
|
||||
func (m *Manager) Snapshot() Snapshot {
|
||||
if m == nil {
|
||||
return Snapshot{}
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.ensureReadyLocked()
|
||||
|
||||
snapshot := Snapshot{
|
||||
Name: m.name,
|
||||
LeaderboardCount: len(m.definitions),
|
||||
RecordsByBoard: make(map[string]int, len(m.records)),
|
||||
HistoryStoreErrors: m.historyErrors + intFromU64Sat(historyErrors(m.hist)),
|
||||
}
|
||||
for id, records := range m.records {
|
||||
if len(records) == 0 {
|
||||
continue
|
||||
}
|
||||
snapshot.RecordCount += len(records)
|
||||
snapshot.RecordsByBoard[id] = len(records)
|
||||
}
|
||||
if len(snapshot.RecordsByBoard) == 0 {
|
||||
snapshot.RecordsByBoard = nil
|
||||
}
|
||||
return snapshot
|
||||
}
|
||||
|
||||
// Flush 刷新排行榜历史异步队列。
|
||||
func (m *Manager) Flush(ctx context.Context) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
store := m.hist
|
||||
m.mu.Unlock()
|
||||
return flushHistory(ctx, store)
|
||||
}
|
||||
|
||||
// Close 关闭排行榜历史异步队列。
|
||||
func (m *Manager) Close(ctx context.Context) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
store := m.hist
|
||||
m.mu.Unlock()
|
||||
return closeHistory(ctx, store)
|
||||
}
|
||||
|
||||
func (m *Manager) ensureReadyLocked() {
|
||||
if m.name == "" {
|
||||
m.name = "leaderboard"
|
||||
}
|
||||
if m.now == nil {
|
||||
m.now = time.Now
|
||||
}
|
||||
if m.definitions == nil {
|
||||
m.definitions = make(map[string]Definition)
|
||||
}
|
||||
if m.records == nil {
|
||||
m.records = make(map[string]map[string]Record)
|
||||
}
|
||||
if m.repairs == nil {
|
||||
m.repairs = make(map[string]RepairReceipt)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) snapshotStateLocked() managerStateSnapshot {
|
||||
return managerStateSnapshot{
|
||||
definitions: cloneDefinitionMap(m.definitions),
|
||||
records: cloneRecordMap(m.records),
|
||||
repairs: cloneRepairMap(m.repairs),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) restoreStateLocked(snapshot managerStateSnapshot) {
|
||||
m.definitions = cloneDefinitionMap(snapshot.definitions)
|
||||
m.records = cloneRecordMap(snapshot.records)
|
||||
m.repairs = cloneRepairMap(snapshot.repairs)
|
||||
}
|
||||
|
||||
func (m *Manager) normDefinitionLocked(definition Definition) (Definition, error) {
|
||||
definition.ID = strings.TrimSpace(definition.ID)
|
||||
if definition.ID == "" {
|
||||
definition.ID = m.nextIDLocked("leaderboard")
|
||||
}
|
||||
definition.Title = strings.TrimSpace(definition.Title)
|
||||
sortOrder, err := normalizeSortOrder(definition.SortOrder)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
operator, err := normalizeOperator(definition.Operator)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
definition.SortOrder = sortOrder
|
||||
definition.Operator = operator
|
||||
if definition.MaxSize < 0 {
|
||||
return Definition{}, fmt.Errorf("%w: max_size must not be negative", ErrInvalidDefinition)
|
||||
}
|
||||
now := m.now().UTC()
|
||||
if definition.CreatedAt.IsZero() {
|
||||
definition.CreatedAt = now
|
||||
} else {
|
||||
definition.CreatedAt = definition.CreatedAt.UTC()
|
||||
}
|
||||
if definition.UpdatedAt.IsZero() {
|
||||
definition.UpdatedAt = definition.CreatedAt
|
||||
} else {
|
||||
definition.UpdatedAt = definition.UpdatedAt.UTC()
|
||||
}
|
||||
definition.Metadata = cloneStringMap(definition.Metadata)
|
||||
return definition, nil
|
||||
}
|
||||
|
||||
func (m *Manager) rankedRecordsLocked(leaderboardID string) []Record {
|
||||
data, ok := m.rankDataLocked(leaderboardID)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return rankRecords(data.definition, data.records)
|
||||
}
|
||||
|
||||
func (m *Manager) rankDataLocked(leaderboardID string) (boardRankData, bool) {
|
||||
definition, ok := m.definitions[leaderboardID]
|
||||
if !ok {
|
||||
return boardRankData{}, false
|
||||
}
|
||||
return boardRankData{
|
||||
definition: cloneDefinition(definition),
|
||||
records: cloneRecordSet(m.records[leaderboardID]),
|
||||
}, true
|
||||
}
|
||||
|
||||
func rankRecords(definition Definition, records map[string]Record) []Record {
|
||||
out := make([]Record, 0, len(records))
|
||||
for _, record := range records {
|
||||
out = append(out, cloneRecord(record))
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool {
|
||||
return compareRecords(out[i], out[j], definition.SortOrder) < 0
|
||||
})
|
||||
for i := range out {
|
||||
out[i].Rank = i + 1
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (m *Manager) recordWithRankLocked(leaderboardID string, ownerID string) (Record, bool) {
|
||||
ranked := m.rankedRecordsLocked(leaderboardID)
|
||||
return recordFromRanked(ranked, ownerID)
|
||||
}
|
||||
|
||||
func recordWithRank(definition Definition, records map[string]Record, ownerID string) (Record, bool) {
|
||||
return recordFromRanked(rankRecords(definition, records), ownerID)
|
||||
}
|
||||
|
||||
func recordFromRanked(ranked []Record, ownerID string) (Record, bool) {
|
||||
for _, record := range ranked {
|
||||
if record.OwnerID == ownerID {
|
||||
return cloneRecord(record), true
|
||||
}
|
||||
}
|
||||
return Record{}, false
|
||||
}
|
||||
|
||||
func (m *Manager) trimLocked(definition Definition) []Record {
|
||||
if definition.MaxSize <= 0 {
|
||||
return nil
|
||||
}
|
||||
ranked := m.rankedRecordsLocked(definition.ID)
|
||||
if len(ranked) <= definition.MaxSize {
|
||||
return nil
|
||||
}
|
||||
keep := make(map[string]struct{}, definition.MaxSize)
|
||||
for _, record := range ranked[:definition.MaxSize] {
|
||||
keep[record.OwnerID] = struct{}{}
|
||||
}
|
||||
trimmed := cloneRecords(ranked[definition.MaxSize:])
|
||||
for ownerID := range m.records[definition.ID] {
|
||||
if _, ok := keep[ownerID]; !ok {
|
||||
delete(m.records[definition.ID], ownerID)
|
||||
}
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func (m *Manager) appendHistoryLocked(ctx context.Context, action string, leaderboardID string, record *Record, definition *Definition, at time.Time) error {
|
||||
if at.IsZero() {
|
||||
at = m.now().UTC()
|
||||
} else {
|
||||
at = at.UTC()
|
||||
}
|
||||
entry := HistoryEntry{
|
||||
Action: action,
|
||||
LeaderboardID: strings.TrimSpace(leaderboardID),
|
||||
At: at,
|
||||
}
|
||||
if record != nil {
|
||||
cloned := cloneRecord(*record)
|
||||
entry.OwnerID = cloned.OwnerID
|
||||
entry.Record = &cloned
|
||||
}
|
||||
if definition != nil {
|
||||
cloned := cloneDefinition(*definition)
|
||||
entry.Definition = &cloned
|
||||
}
|
||||
return m.appendHistoryEntry(ctx, entry)
|
||||
}
|
||||
|
||||
func (m *Manager) appendHistoryEntry(ctx context.Context, entry HistoryEntry) error {
|
||||
if m.hist == nil {
|
||||
return nil
|
||||
}
|
||||
entry = cloneHistoryEntry(entry)
|
||||
entry.LeaderboardID = strings.TrimSpace(entry.LeaderboardID)
|
||||
entry.Action = strings.TrimSpace(entry.Action)
|
||||
entry.OwnerID = strings.TrimSpace(entry.OwnerID)
|
||||
entry.Reason = strings.TrimSpace(entry.Reason)
|
||||
entry.OperatorID = strings.TrimSpace(entry.OperatorID)
|
||||
entry.RequestID = strings.TrimSpace(entry.RequestID)
|
||||
entry.Metadata = cloneStringMap(entry.Metadata)
|
||||
if entry.At.IsZero() {
|
||||
entry.At = m.now().UTC()
|
||||
} else {
|
||||
entry.At = entry.At.UTC()
|
||||
}
|
||||
if err := m.hist.Append(ctx, entry); err != nil {
|
||||
m.historyErrors++
|
||||
if m.historyStrict {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) nextIDLocked(prefix string) string {
|
||||
if m.gen != nil {
|
||||
if id := strings.TrimSpace(m.gen(prefix)); id != "" {
|
||||
return id
|
||||
}
|
||||
}
|
||||
m.next++
|
||||
return fmt.Sprintf("%s-%d", prefix, m.next)
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (m *Manager) MergedView(ctx context.Context, options MergedViewOptions) (MergedView, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
if m == nil {
|
||||
return MergedView{}, ErrManagerRequired
|
||||
}
|
||||
normalized, err := normMergedViewOpts(options)
|
||||
if err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
m.ensureReadyLocked()
|
||||
sources := make([]boardRankData, 0, len(normalized.IDs))
|
||||
for _, id := range normalized.IDs {
|
||||
data, ok := m.rankDataLocked(id)
|
||||
if !ok {
|
||||
m.mu.Unlock()
|
||||
return MergedView{}, ErrLeaderboardNotFound
|
||||
}
|
||||
sources = append(sources, data)
|
||||
}
|
||||
generatedAt := m.now().UTC()
|
||||
m.mu.Unlock()
|
||||
|
||||
records := make([]Record, 0)
|
||||
for _, data := range sources {
|
||||
records = append(records, rankRecords(data.definition, data.records)...)
|
||||
}
|
||||
return buildMergedView(normalized, records, generatedAt), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) MergedView(ctx context.Context, options MergedViewOptions) (MergedView, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
normalized, err := normMergedViewOpts(options)
|
||||
if err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
|
||||
records := make([]Record, 0)
|
||||
for _, id := range normalized.IDs {
|
||||
if _, ok, err := m.definition(ctx, id); err != nil || !ok {
|
||||
if err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
return MergedView{}, ErrLeaderboardNotFound
|
||||
}
|
||||
ranked, err := m.rankedRecords(ctx, id)
|
||||
if err != nil {
|
||||
return MergedView{}, err
|
||||
}
|
||||
records = append(records, ranked...)
|
||||
}
|
||||
return buildMergedView(normalized, records, m.nowUTC()), nil
|
||||
}
|
||||
|
||||
func normMergedViewOpts(options MergedViewOptions) (MergedViewOptions, error) {
|
||||
ids := make([]string, 0, len(options.IDs))
|
||||
seen := make(map[string]struct{}, len(options.IDs))
|
||||
for _, id := range options.IDs {
|
||||
id = strings.TrimSpace(id)
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return MergedViewOptions{}, fmt.Errorf("%w: ids are required", ErrInvalidDefinition)
|
||||
}
|
||||
sortOrder, err := normalizeSortOrder(options.SortOrder)
|
||||
if err != nil {
|
||||
return MergedViewOptions{}, err
|
||||
}
|
||||
options.IDs = ids
|
||||
options.SortOrder = sortOrder
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func buildMergedView(options MergedViewOptions, records []Record, generatedAt time.Time) MergedView {
|
||||
records = cloneRecords(records)
|
||||
sort.SliceStable(records, func(i, j int) bool {
|
||||
return compareRecords(records[i], records[j], options.SortOrder) < 0
|
||||
})
|
||||
if options.DedupeOwner {
|
||||
records = dedupeMergedRecords(records)
|
||||
}
|
||||
for i := range records {
|
||||
records[i].Rank = i + 1
|
||||
}
|
||||
|
||||
total := len(records)
|
||||
offset, limit := normalizeListOptions(ListOptions{Offset: options.Offset, Limit: options.Limit})
|
||||
if offset >= len(records) {
|
||||
records = []Record{}
|
||||
} else {
|
||||
end := offset + limit
|
||||
if end > len(records) {
|
||||
end = len(records)
|
||||
}
|
||||
records = cloneRecords(records[offset:end])
|
||||
}
|
||||
return MergedView{
|
||||
IDs: append([]string(nil), options.IDs...),
|
||||
SortOrder: options.SortOrder,
|
||||
Records: records,
|
||||
RecordCount: total,
|
||||
GeneratedAt: generatedAt.UTC(),
|
||||
}
|
||||
}
|
||||
|
||||
func dedupeMergedRecords(records []Record) []Record {
|
||||
seen := make(map[string]struct{}, len(records))
|
||||
out := make([]Record, 0, len(records))
|
||||
for _, record := range records {
|
||||
if _, ok := seen[record.OwnerID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[record.OwnerID] = struct{}{}
|
||||
out = append(out, record)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,751 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/db"
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNilRedisExecutor = errors.New("leaderboard redis executor is required")
|
||||
ErrRedisLockToken = errors.New("leaderboard redis lock token generation failed")
|
||||
)
|
||||
|
||||
const (
|
||||
defRedisHistory = defaultHistoryLimit
|
||||
defaultRedisLockTTL = 5 * time.Second
|
||||
defRedisBoardPrefix = "longheng:leaderboards"
|
||||
defRedisBoardMgr = "redis-leaderboard"
|
||||
)
|
||||
|
||||
type RedisOptions struct {
|
||||
Name string
|
||||
Executor db.RedisExecutor
|
||||
KeyPrefix string
|
||||
Now func() time.Time
|
||||
HistoryLimit int
|
||||
HistoryStore HistoryStore
|
||||
HistoryStrict bool
|
||||
EventLog *eventlog.Log
|
||||
EventLogStrict bool
|
||||
EventLogStream string
|
||||
EventLogType string
|
||||
LockTTL time.Duration
|
||||
}
|
||||
|
||||
type RedisManager struct {
|
||||
name string
|
||||
executor db.RedisExecutor
|
||||
prefix string
|
||||
now func() time.Time
|
||||
historyLimit int
|
||||
historyStore HistoryStore
|
||||
historyStrict bool
|
||||
historyErrors uint64
|
||||
eventLog *eventlog.Log
|
||||
eventHist HistoryStore
|
||||
eventLogStrict bool
|
||||
eventLogStream string
|
||||
eventLogType string
|
||||
eventLogErrors uint64
|
||||
lockTTL time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewRedis(options RedisOptions) *RedisManager {
|
||||
name := strings.TrimSpace(options.Name)
|
||||
if name == "" {
|
||||
name = defRedisBoardMgr
|
||||
}
|
||||
now := options.Now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
historyLimit := options.HistoryLimit
|
||||
if historyLimit == 0 {
|
||||
historyLimit = defRedisHistory
|
||||
}
|
||||
lockTTL := options.LockTTL
|
||||
if lockTTL <= 0 {
|
||||
lockTTL = defaultRedisLockTTL
|
||||
}
|
||||
historyStore := wrapHistoryStore(options.HistoryStore, options.HistoryStrict)
|
||||
eventLog := options.EventLog
|
||||
var eventHist HistoryStore
|
||||
if eventLog != nil && !options.EventLogStrict {
|
||||
eventHist = NewAsyncHistoryStore(newEventHistoryStore(eventLog, options.EventLogStream, options.EventLogType))
|
||||
eventLog = nil
|
||||
}
|
||||
return &RedisManager{
|
||||
name: name,
|
||||
executor: options.Executor,
|
||||
prefix: normRedisBoardPrefix(options.KeyPrefix),
|
||||
now: now,
|
||||
historyLimit: historyLimit,
|
||||
historyStore: historyStore,
|
||||
historyStrict: options.HistoryStrict,
|
||||
eventLog: eventLog,
|
||||
eventHist: eventHist,
|
||||
eventLogStrict: options.EventLogStrict,
|
||||
eventLogStream: normalizeEventStream(options.EventLogStream),
|
||||
eventLogType: normHistoryEventType(options.EventLogType),
|
||||
lockTTL: lockTTL,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *RedisManager) Create(ctx context.Context, definition Definition) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
normalized, err := m.normalizeDefinition(ctx, definition)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
data, err := json.Marshal(normalized)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
history, err := m.historyData(HistoryActionCreate, normalized.ID, nil, &normalized, normalized.CreatedAt)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
result, err := m.redisEvalInt(ctx, redisCreateScript, []string{
|
||||
m.definitionKey(normalized.ID),
|
||||
m.indexKey(),
|
||||
m.historyKey(normalized.ID),
|
||||
}, normalized.ID, data, history, m.historyLimitValue())
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
if result < 0 {
|
||||
return Definition{}, ErrLeaderboardExists
|
||||
}
|
||||
if err := m.appendHistoryData(ctx, history); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
return cloneDefinition(normalized), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Delete(ctx context.Context, leaderboardID string) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
var deleted Definition
|
||||
err := m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
history, err := m.historyData(HistoryDeleteBoard, definition.ID, nil, &definition, m.nowUTC())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := m.redisEvalInt(ctx, redisDeleteScript, []string{
|
||||
m.definitionKey(definition.ID),
|
||||
m.recordsKey(definition.ID),
|
||||
m.zsetKey(definition.ID),
|
||||
m.indexKey(),
|
||||
m.historyKey(definition.ID),
|
||||
}, definition.ID, history, m.historyLimitValue())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result < 0 {
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
if err := m.appendHistoryData(ctx, history); err != nil {
|
||||
return err
|
||||
}
|
||||
deleted = definition
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Definition(leaderboardID string) (Definition, bool) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
definition, ok, err := m.DefinitionContext(ctx, leaderboardID)
|
||||
if err != nil {
|
||||
return Definition{}, false
|
||||
}
|
||||
return definition, ok
|
||||
}
|
||||
|
||||
// DefinitionContext 让生产读路径区分 Redis 故障和榜单不存在,避免把后端抖动误判为业务空结果。
|
||||
func (m *RedisManager) DefinitionContext(ctx context.Context, leaderboardID string) (Definition, bool, error) {
|
||||
return m.definition(ctx, leaderboardID)
|
||||
}
|
||||
|
||||
func (m *RedisManager) Definitions() []Definition {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
definitions, err := m.DefinitionsContext(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return definitions
|
||||
}
|
||||
|
||||
// DefinitionsContext 让管理面在 Redis 故障时返回错误,而不是把榜单列表显示为空。
|
||||
func (m *RedisManager) DefinitionsContext(ctx context.Context) ([]Definition, error) {
|
||||
return m.definitions(ctx)
|
||||
}
|
||||
|
||||
func (m *RedisManager) Submit(ctx context.Context, leaderboardID string, submission Submission) (Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized, err := normalizeSubmission(submission)
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
var out Record
|
||||
err = m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
existing, exists, err := m.record(ctx, definition.ID, normalized.OwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
now := m.nowUTC()
|
||||
record := Record{
|
||||
LeaderboardID: definition.ID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Score: normalized.Score,
|
||||
Subscore: normalized.Subscore,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if exists {
|
||||
record.CreatedAt = existing.CreatedAt
|
||||
switch definition.Operator {
|
||||
case OperatorBest:
|
||||
if !betterThan(record, existing, definition.SortOrder) {
|
||||
ranked, ok, err := m.recordWithRank(ctx, definition.ID, normalized.OwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
out = ranked
|
||||
return nil
|
||||
}
|
||||
case OperatorIncrement:
|
||||
record.Score, err = addScoreValue(existing.Score, normalized.Score)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
record.Subscore, err = addScoreValue(existing.Subscore, normalized.Subscore)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
case OperatorSet:
|
||||
default:
|
||||
return fmt.Errorf("%w: unsupported operator %q", ErrInvalidDefinition, definition.Operator)
|
||||
}
|
||||
}
|
||||
if err := m.saveRecord(ctx, definition, record); err != nil {
|
||||
return err
|
||||
}
|
||||
if definition.MaxSize > 0 {
|
||||
if err := m.trim(ctx, definition); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ranked, ok, err := m.recordWithRank(ctx, definition.ID, normalized.OwnerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return ErrRecordTrimmed
|
||||
}
|
||||
out = ranked
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) DeleteRecord(ctx context.Context, leaderboardID string, ownerID string) (Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Record{}, ErrLeaderboardNotFound
|
||||
}
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if ownerID == "" {
|
||||
return Record{}, ErrRecordNotFound
|
||||
}
|
||||
var deleted Record
|
||||
err := m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
record, ok, err := m.recordWithRank(ctx, definition.ID, ownerID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
if err := m.deleteRecords(ctx, definition, []Record{record}, HistoryActionDeleteRecord); err != nil {
|
||||
return err
|
||||
}
|
||||
deleted = record
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
return deleted, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Repair(ctx context.Context, leaderboardID string, request RepairRequest) (RepairReceipt, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return RepairReceipt{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized, err := normRepairReq(request)
|
||||
if err != nil {
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
var receipt RepairReceipt
|
||||
err = m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
now := m.nowUTC()
|
||||
receipt = RepairReceipt{
|
||||
LeaderboardID: definition.ID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Action: HistoryActionRepairRecord,
|
||||
Reason: normalized.Reason,
|
||||
OperatorID: normalized.OperatorID,
|
||||
RequestID: normalized.RequestID,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
At: now,
|
||||
}
|
||||
if before, found, err := m.recordWithRank(ctx, definition.ID, normalized.OwnerID); err != nil {
|
||||
return err
|
||||
} else if found {
|
||||
before := before
|
||||
receipt.Before = &before
|
||||
}
|
||||
details := HistoryDetails{
|
||||
Reason: normalized.Reason,
|
||||
OperatorID: normalized.OperatorID,
|
||||
RequestID: normalized.RequestID,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
}
|
||||
if normalized.Delete {
|
||||
if receipt.Before == nil {
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
if err := m.delRecordsDetail(ctx, definition, []Record{*receipt.Before}, HistoryActionRepairRecord, details); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
createdAt := now
|
||||
if receipt.Before != nil && !normalized.ResetCreatedAt {
|
||||
createdAt = receipt.Before.CreatedAt
|
||||
}
|
||||
record := Record{
|
||||
LeaderboardID: definition.ID,
|
||||
OwnerID: normalized.OwnerID,
|
||||
Score: normalized.Score,
|
||||
Subscore: normalized.Subscore,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CreatedAt: createdAt,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if err := m.saveRecordDetails(ctx, definition, record, HistoryActionRepairRecord, details); err != nil {
|
||||
return err
|
||||
}
|
||||
if definition.MaxSize > 0 {
|
||||
if err := m.trim(ctx, definition); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if after, found, err := m.recordWithRank(ctx, definition.ID, normalized.OwnerID); err != nil {
|
||||
return err
|
||||
} else if found {
|
||||
after := after
|
||||
receipt.After = &after
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return RepairReceipt{}, err
|
||||
}
|
||||
return cloneRepairReceipt(receipt), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Record(leaderboardID string, ownerID string) (Record, bool) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" || ownerID == "" {
|
||||
return Record{}, false
|
||||
}
|
||||
record, found, err := m.recordWithRank(ctx, leaderboardID, ownerID)
|
||||
if err != nil {
|
||||
return Record{}, false
|
||||
}
|
||||
return record, found
|
||||
}
|
||||
|
||||
func (m *RedisManager) List(ctx context.Context, leaderboardID string, options ListOptions) ([]Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if _, ok, err := m.definition(ctx, leaderboardID); err != nil || !ok {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
offset, limit := normalizeListOptions(options)
|
||||
out, err := m.rangeRecords(ctx, leaderboardID, offset, offset+limit-1, offset+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) AroundOwner(ctx context.Context, leaderboardID string, ownerID string, limit int) ([]Record, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if ownerID == "" {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if _, ok, err := m.definition(ctx, leaderboardID); err != nil || !ok {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
rank, ok, err := m.redisRank(ctx, leaderboardID, ownerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultAroundLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
total, err := m.redisInt(ctx, "ZCARD", m.zsetKey(leaderboardID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if total <= 0 {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
if limit > int(total) {
|
||||
limit = int(total)
|
||||
}
|
||||
start := rank - 1 - limit/2
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
if start+limit > int(total) {
|
||||
start = int(total) - limit
|
||||
if start < 0 {
|
||||
start = 0
|
||||
}
|
||||
}
|
||||
out, err := m.rangeRecords(ctx, leaderboardID, start, start+limit-1, start+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Rank(ctx context.Context, leaderboardID string, ownerID string) (int, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" {
|
||||
return 0, false, ErrLeaderboardNotFound
|
||||
}
|
||||
if ownerID == "" {
|
||||
return 0, false, nil
|
||||
}
|
||||
return m.redisRank(ctx, leaderboardID, ownerID)
|
||||
}
|
||||
|
||||
func (m *RedisManager) Reset(ctx context.Context, leaderboardID string) (Definition, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Definition{}, ErrLeaderboardNotFound
|
||||
}
|
||||
var reset Definition
|
||||
err := m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
definition.UpdatedAt = m.nowUTC()
|
||||
data, err := json.Marshal(definition)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
history, err := m.historyData(HistoryActionReset, definition.ID, nil, &definition, definition.UpdatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := m.redisEvalInt(ctx, redisResetScript, []string{
|
||||
m.definitionKey(definition.ID),
|
||||
m.recordsKey(definition.ID),
|
||||
m.zsetKey(definition.ID),
|
||||
m.historyKey(definition.ID),
|
||||
}, data, history, m.historyLimitValue())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result < 0 {
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
if err := m.appendHistoryData(ctx, history); err != nil {
|
||||
return err
|
||||
}
|
||||
reset = cloneDefinition(definition)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
return reset, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Capture(ctx context.Context, leaderboardID string, options CaptureOptions) (Capture, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Capture{}, err
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Capture{}, ErrLeaderboardNotFound
|
||||
}
|
||||
normalized := normCaptureOpts(options)
|
||||
var capture Capture
|
||||
err := m.withLeaderboardLock(ctx, leaderboardID, func() error {
|
||||
if _, ok, err := m.definition(ctx, leaderboardID); err != nil || !ok {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
ranked, err := m.rankedRecords(ctx, leaderboardID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
limit := normalized.Limit
|
||||
if limit > len(ranked) {
|
||||
limit = len(ranked)
|
||||
}
|
||||
capture = Capture{
|
||||
LeaderboardID: leaderboardID,
|
||||
Records: cloneRecords(ranked[:limit]),
|
||||
RecordCount: len(ranked),
|
||||
Reason: normalized.Reason,
|
||||
OperatorID: normalized.OperatorID,
|
||||
RequestID: normalized.RequestID,
|
||||
Metadata: cloneStringMap(normalized.Metadata),
|
||||
CapturedAt: m.nowUTC(),
|
||||
}
|
||||
history, err := m.historyData(HistoryActionCapture, leaderboardID, nil, nil, capture.CapturedAt, HistoryDetails{
|
||||
Reason: capture.Reason,
|
||||
OperatorID: capture.OperatorID,
|
||||
RequestID: capture.RequestID,
|
||||
Metadata: capture.Metadata,
|
||||
Records: capture.Records,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.appendHistoryPayload(ctx, leaderboardID, history)
|
||||
})
|
||||
if err != nil {
|
||||
return Capture{}, err
|
||||
}
|
||||
return cloneCapture(capture), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) History(ctx context.Context, leaderboardID string, limit int) ([]HistoryEntry, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil {
|
||||
return nil, ErrNilRedisExecutor
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
historyLimit := m.historyLimitValue()
|
||||
if limit > historyLimit && historyLimit > 0 {
|
||||
limit = historyLimit
|
||||
}
|
||||
if m.historyStore != nil {
|
||||
history, err := m.historyStore.List(ctx, leaderboardID, limit)
|
||||
if err == nil {
|
||||
return history, nil
|
||||
}
|
||||
if m.historyStrict {
|
||||
return nil, err
|
||||
}
|
||||
atomic.AddUint64(&m.historyErrors, 1)
|
||||
}
|
||||
value, err := m.do(ctx, "LRANGE", m.historyKey(leaderboardID), -limit, -1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, err := redisStrings(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
history := make([]HistoryEntry, 0, len(items))
|
||||
for _, item := range items {
|
||||
var entry HistoryEntry
|
||||
if err := json.Unmarshal([]byte(item), &entry); err != nil {
|
||||
return nil, fmt.Errorf("decode leaderboard history: %w", err)
|
||||
}
|
||||
if entry.Record != nil {
|
||||
cloned := cloneRecord(*entry.Record)
|
||||
entry.Record = &cloned
|
||||
}
|
||||
if entry.Definition != nil {
|
||||
cloned := cloneDefinition(*entry.Definition)
|
||||
entry.Definition = &cloned
|
||||
}
|
||||
history = append(history, entry)
|
||||
}
|
||||
return history, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) Snapshot() Snapshot {
|
||||
if m == nil {
|
||||
return Snapshot{}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
definitions, err := m.definitions(ctx)
|
||||
if err != nil {
|
||||
return Snapshot{Name: m.redisName()}
|
||||
}
|
||||
snapshot := Snapshot{
|
||||
Name: m.redisName(),
|
||||
LeaderboardCount: len(definitions),
|
||||
RecordsByBoard: make(map[string]int, len(definitions)),
|
||||
HistoryStoreErrors: intFromU64Sat(atomic.LoadUint64(&m.historyErrors) + historyErrors(m.historyStore)),
|
||||
EventLogErrors: intFromU64Sat(atomic.LoadUint64(&m.eventLogErrors) + historyStoreErrors(m.historyStore) + historyErrors(m.eventHist)),
|
||||
}
|
||||
for _, definition := range definitions {
|
||||
count, err := m.redisInt(ctx, "HLEN", m.recordsKey(definition.ID))
|
||||
if err != nil || count <= 0 {
|
||||
continue
|
||||
}
|
||||
snapshot.RecordCount += int(count)
|
||||
snapshot.RecordsByBoard[definition.ID] = int(count)
|
||||
}
|
||||
if len(snapshot.RecordsByBoard) == 0 {
|
||||
snapshot.RecordsByBoard = nil
|
||||
}
|
||||
return snapshot
|
||||
}
|
||||
|
||||
// Flush 刷新排行榜外部历史和事件异步队列。
|
||||
func (m *RedisManager) Flush(ctx context.Context) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.Join(flushHistory(ctx, m.historyStore), flushHistory(ctx, m.eventHist))
|
||||
}
|
||||
|
||||
// Close 关闭排行榜外部历史和事件异步队列。
|
||||
func (m *RedisManager) Close(ctx context.Context) error {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.Join(closeHistory(ctx, m.historyStore), closeHistory(ctx, m.eventHist))
|
||||
}
|
||||
|
||||
func intFromU64Sat(value uint64) int {
|
||||
if value > uint64(math.MaxInt) {
|
||||
return math.MaxInt
|
||||
}
|
||||
return int(value) //nolint:gosec // G115:前面已按 int 最大值钳制。
|
||||
}
|
||||
@@ -0,0 +1,345 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
func (m *RedisManager) redisInt(ctx context.Context, command string, args ...any) (int64, error) {
|
||||
value, err := m.do(ctx, command, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return redisInt64(value)
|
||||
}
|
||||
|
||||
func (m *RedisManager) redisEvalInt(ctx context.Context, script string, keys []string, args ...any) (int64, error) {
|
||||
params := make([]any, 0, 2+len(keys)+len(args))
|
||||
params = append(params, script, len(keys))
|
||||
for _, key := range keys {
|
||||
params = append(params, key)
|
||||
}
|
||||
params = append(params, args...)
|
||||
value, err := m.do(ctx, "EVAL", params...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return redisInt64(value)
|
||||
}
|
||||
|
||||
func (m *RedisManager) nextID(ctx context.Context) (string, error) {
|
||||
if m == nil {
|
||||
return "", ErrNilRedisExecutor
|
||||
}
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
n, err := m.redisInt(ctx, "INCR", m.counterKey())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("leaderboard-%d", n), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) do(ctx context.Context, command string, args ...any) (any, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.executor == nil {
|
||||
return nil, ErrNilRedisExecutor
|
||||
}
|
||||
return m.executor.Do(ctx, command, args...)
|
||||
}
|
||||
|
||||
func (m *RedisManager) indexKey() string {
|
||||
return m.redisPrefix() + ":index"
|
||||
}
|
||||
|
||||
func (m *RedisManager) counterKey() string {
|
||||
return m.redisPrefix() + ":counter"
|
||||
}
|
||||
|
||||
func (m *RedisManager) definitionKey(id string) string {
|
||||
return m.redisPrefix() + ":definition:" + strings.TrimSpace(id)
|
||||
}
|
||||
|
||||
func (m *RedisManager) recordsKey(id string) string {
|
||||
return m.redisPrefix() + ":records:" + strings.TrimSpace(id)
|
||||
}
|
||||
|
||||
func (m *RedisManager) zsetKey(id string) string {
|
||||
return m.redisPrefix() + ":z:" + strings.TrimSpace(id)
|
||||
}
|
||||
|
||||
func (m *RedisManager) historyKey(id string) string {
|
||||
return m.redisPrefix() + ":history:" + strings.TrimSpace(id)
|
||||
}
|
||||
|
||||
func (m *RedisManager) lockKey(id string) string {
|
||||
return m.redisPrefix() + ":lock:leaderboard:" + strings.TrimSpace(id)
|
||||
}
|
||||
|
||||
func (m *RedisManager) redisPrefix() string {
|
||||
if m == nil {
|
||||
return defRedisBoardPrefix
|
||||
}
|
||||
return normRedisBoardPrefix(m.prefix)
|
||||
}
|
||||
|
||||
func normRedisBoardPrefix(prefix string) string {
|
||||
prefix = strings.Trim(strings.TrimSpace(prefix), ":")
|
||||
if prefix == "" {
|
||||
return defRedisBoardPrefix
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
|
||||
func (m *RedisManager) redisName() string {
|
||||
if m == nil {
|
||||
return ""
|
||||
}
|
||||
name := strings.TrimSpace(m.name)
|
||||
if name == "" {
|
||||
return defRedisBoardMgr
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (m *RedisManager) nowUTC() time.Time {
|
||||
if m == nil || m.now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return m.now().UTC()
|
||||
}
|
||||
|
||||
func (m *RedisManager) historyLimitValue() int {
|
||||
if m == nil || m.historyLimit == 0 {
|
||||
return defRedisHistory
|
||||
}
|
||||
return m.historyLimit
|
||||
}
|
||||
|
||||
func (m *RedisManager) lockTTLValue() time.Duration {
|
||||
if m == nil || m.lockTTL <= 0 {
|
||||
return defaultRedisLockTTL
|
||||
}
|
||||
return m.lockTTL
|
||||
}
|
||||
|
||||
func (m *RedisManager) appendHistoryData(ctx context.Context, payloads ...[]byte) error {
|
||||
if len(payloads) == 0 || (m.historyStore == nil && m.eventLog == nil && m.eventHist == nil) {
|
||||
return nil
|
||||
}
|
||||
for _, payload := range payloads {
|
||||
var entry HistoryEntry
|
||||
if err := json.Unmarshal(payload, &entry); err != nil {
|
||||
atomic.AddUint64(&m.historyErrors, 1)
|
||||
if m.historyStrict {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if m.historyStore != nil {
|
||||
if err := m.historyStore.Append(ctx, entry); err != nil {
|
||||
atomic.AddUint64(&m.historyErrors, 1)
|
||||
if m.historyStrict {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if m.eventHist != nil {
|
||||
if err := m.eventHist.Append(ctx, entry); err != nil {
|
||||
atomic.AddUint64(&m.eventLogErrors, 1)
|
||||
if m.eventLogStrict {
|
||||
return err
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if err := m.appendEventHistory(ctx, entry); err != nil {
|
||||
atomic.AddUint64(&m.eventLogErrors, 1)
|
||||
if m.eventLogStrict {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) appendEventHistory(ctx context.Context, entry HistoryEntry) error {
|
||||
if m == nil || m.eventLog == nil {
|
||||
return nil
|
||||
}
|
||||
entry = normHistoryEntry(entry)
|
||||
payload, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.eventLog.Append(ctx, eventlog.Event{
|
||||
Stream: m.eventLogStream,
|
||||
Type: m.eventLogType,
|
||||
AggregateID: entry.LeaderboardID,
|
||||
IdempotencyKey: historyIdemKey(entry, payload),
|
||||
Payload: payload,
|
||||
Headers: map[string]string{
|
||||
"action": entry.Action,
|
||||
"leaderboard_id": entry.LeaderboardID,
|
||||
"owner_id": entry.OwnerID,
|
||||
},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func normalizeEventStream(stream string) string {
|
||||
stream = strings.TrimSpace(stream)
|
||||
if stream == "" {
|
||||
return LeaderboardEventStream
|
||||
}
|
||||
return stream
|
||||
}
|
||||
|
||||
func normHistoryEventType(eventType string) string {
|
||||
eventType = strings.TrimSpace(eventType)
|
||||
if eventType == "" {
|
||||
return DefaultLeaderboardHistoryEventType
|
||||
}
|
||||
return eventType
|
||||
}
|
||||
|
||||
func (m *RedisManager) historyData(action string, leaderboardID string, record *Record, definition *Definition, at time.Time, details ...HistoryDetails) ([]byte, error) {
|
||||
if at.IsZero() {
|
||||
at = m.nowUTC()
|
||||
} else {
|
||||
at = at.UTC()
|
||||
}
|
||||
entry := HistoryEntry{
|
||||
Action: action,
|
||||
LeaderboardID: strings.TrimSpace(leaderboardID),
|
||||
At: at,
|
||||
}
|
||||
if len(details) > 0 {
|
||||
detail := details[0]
|
||||
entry.Reason = strings.TrimSpace(detail.Reason)
|
||||
entry.OperatorID = strings.TrimSpace(detail.OperatorID)
|
||||
entry.RequestID = strings.TrimSpace(detail.RequestID)
|
||||
entry.Metadata = cloneStringMap(detail.Metadata)
|
||||
entry.Records = cloneRecords(detail.Records)
|
||||
}
|
||||
if record != nil {
|
||||
cloned := cloneRecord(*record)
|
||||
entry.OwnerID = cloned.OwnerID
|
||||
entry.Record = &cloned
|
||||
}
|
||||
if definition != nil {
|
||||
cloned := cloneDefinition(*definition)
|
||||
entry.Definition = &cloned
|
||||
}
|
||||
return json.Marshal(entry)
|
||||
}
|
||||
|
||||
func redisRankScore(record Record, sortOrder string) float64 {
|
||||
score := float64(record.Score)
|
||||
if sortOrder == SortAscending {
|
||||
return -score
|
||||
}
|
||||
return score
|
||||
}
|
||||
|
||||
func redisBytes(value any) ([]byte, bool, error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return nil, false, nil
|
||||
case []byte:
|
||||
return append([]byte(nil), v...), true, nil
|
||||
case string:
|
||||
return []byte(v), true, nil
|
||||
default:
|
||||
return nil, false, fmt.Errorf("unexpected redis bulk type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func redisStrings(value any) ([]string, error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return nil, nil
|
||||
case []string:
|
||||
return append([]string(nil), v...), nil
|
||||
case [][]byte:
|
||||
out := make([]string, len(v))
|
||||
for i := range v {
|
||||
out[i] = string(v[i])
|
||||
}
|
||||
return out, nil
|
||||
case []any:
|
||||
out := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
switch x := item.(type) {
|
||||
case []byte:
|
||||
out = append(out, string(x))
|
||||
case string:
|
||||
out = append(out, x)
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected redis string item %T", item)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected redis string slice type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func redisBulkBytes(value any) ([][]byte, []bool, error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return nil, nil, nil
|
||||
case []any:
|
||||
data := make([][]byte, len(v))
|
||||
found := make([]bool, len(v))
|
||||
for i, item := range v {
|
||||
bytes, ok, err := redisBytes(item)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
data[i] = bytes
|
||||
found[i] = ok
|
||||
}
|
||||
return data, found, nil
|
||||
case [][]byte:
|
||||
data := make([][]byte, len(v))
|
||||
found := make([]bool, len(v))
|
||||
for i := range v {
|
||||
data[i] = append([]byte(nil), v[i]...)
|
||||
found[i] = true
|
||||
}
|
||||
return data, found, nil
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unexpected redis bulk slice type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func redisInt64(value any) (int64, error) {
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
return int64(v), nil
|
||||
case int64:
|
||||
return v, nil
|
||||
case uint64:
|
||||
const maxInt64 = uint64(1<<63 - 1)
|
||||
if v > maxInt64 {
|
||||
return 0, fmt.Errorf("redis integer overflows int64: %d", v)
|
||||
}
|
||||
return int64(v), nil
|
||||
case []byte:
|
||||
return strconv.ParseInt(string(v), 10, 64)
|
||||
case string:
|
||||
return strconv.ParseInt(v, 10, 64)
|
||||
default:
|
||||
return 0, fmt.Errorf("unexpected redis integer type %T", value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
lockkit "longheng.io/server/internal/platform/lock"
|
||||
)
|
||||
|
||||
var redisLockRandRead = rand.Read
|
||||
|
||||
func (m *RedisManager) withLeaderboardLock(ctx context.Context, leaderboardID string, fn func() error) error {
|
||||
if m == nil || m.executor == nil {
|
||||
return ErrNilRedisExecutor
|
||||
}
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return ErrLeaderboardNotFound
|
||||
}
|
||||
token, err := redisLockToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.acquireLbLock(ctx, leaderboardID, token); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
releaseCtx, cancel := detachedTimeoutCtx(ctx, time.Second)
|
||||
defer cancel()
|
||||
_ = m.releaseLbLock(releaseCtx, leaderboardID, token)
|
||||
}()
|
||||
return fn()
|
||||
}
|
||||
|
||||
func redisLockToken() (string, error) {
|
||||
var raw [16]byte
|
||||
if _, err := redisLockRandRead(raw[:]); err != nil {
|
||||
return "", fmt.Errorf("%w: %w", ErrRedisLockToken, err)
|
||||
}
|
||||
return hex.EncodeToString(raw[:]), nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) acquireLbLock(ctx context.Context, leaderboardID string, token string) error {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if m == nil || m.executor == nil {
|
||||
return ErrNilRedisExecutor
|
||||
}
|
||||
for {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
ok, err := lockkit.AcquireRedis(ctx, m.executor, m.lockKey(leaderboardID), token, m.lockTTLValue())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
}
|
||||
timer := time.NewTimer(10 * time.Millisecond)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *RedisManager) releaseLbLock(ctx context.Context, leaderboardID string, token string) error {
|
||||
if m == nil || m.executor == nil {
|
||||
return ErrNilRedisExecutor
|
||||
}
|
||||
err := lockkit.ReleaseRedis(ctx, m.executor, m.lockKey(leaderboardID), token)
|
||||
if errors.Is(err, lockkit.ErrTokenMismatch) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func detachedTimeoutCtx(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
|
||||
if parent == nil {
|
||||
return context.WithTimeout(context.Background(), timeout)
|
||||
}
|
||||
return context.WithTimeout(context.WithoutCancel(parent), timeout)
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package leaderboard
|
||||
|
||||
const redisCreateScript = `
|
||||
if redis.call("EXISTS", KEYS[1]) == 1 then
|
||||
return -1
|
||||
end
|
||||
redis.call("SET", KEYS[1], ARGV[2])
|
||||
redis.call("SADD", KEYS[2], ARGV[1])
|
||||
local limit = tonumber(ARGV[4])
|
||||
if limit > 0 then
|
||||
redis.call("RPUSH", KEYS[3], ARGV[3])
|
||||
redis.call("LTRIM", KEYS[3], -limit, -1)
|
||||
end
|
||||
return 1
|
||||
`
|
||||
|
||||
const redisDeleteScript = `
|
||||
if redis.call("EXISTS", KEYS[1]) == 0 then
|
||||
return -1
|
||||
end
|
||||
redis.call("DEL", KEYS[1], KEYS[2], KEYS[3])
|
||||
redis.call("SREM", KEYS[4], ARGV[1])
|
||||
local limit = tonumber(ARGV[3])
|
||||
if limit > 0 then
|
||||
redis.call("RPUSH", KEYS[5], ARGV[2])
|
||||
redis.call("LTRIM", KEYS[5], -limit, -1)
|
||||
end
|
||||
return 1
|
||||
`
|
||||
|
||||
const redisSaveRecord = `
|
||||
redis.call("HSET", KEYS[1], ARGV[1], ARGV[2])
|
||||
redis.call("ZADD", KEYS[2], ARGV[3], ARGV[1])
|
||||
local limit = tonumber(ARGV[5])
|
||||
if limit > 0 then
|
||||
redis.call("RPUSH", KEYS[3], ARGV[4])
|
||||
redis.call("LTRIM", KEYS[3], -limit, -1)
|
||||
end
|
||||
return 1
|
||||
`
|
||||
|
||||
const redisDeleteRecords = `
|
||||
local count = tonumber(ARGV[1])
|
||||
local limit = tonumber(ARGV[2])
|
||||
local offset = 3
|
||||
for i = 1, count do
|
||||
local owner_id = ARGV[offset]
|
||||
local history = ARGV[offset + 1]
|
||||
redis.call("HDEL", KEYS[1], owner_id)
|
||||
redis.call("ZREM", KEYS[2], owner_id)
|
||||
if limit > 0 then
|
||||
redis.call("RPUSH", KEYS[3], history)
|
||||
end
|
||||
offset = offset + 2
|
||||
end
|
||||
if limit > 0 then
|
||||
redis.call("LTRIM", KEYS[3], -limit, -1)
|
||||
end
|
||||
return count
|
||||
`
|
||||
|
||||
const redisResetScript = `
|
||||
if redis.call("EXISTS", KEYS[1]) == 0 then
|
||||
return -1
|
||||
end
|
||||
redis.call("SET", KEYS[1], ARGV[1])
|
||||
redis.call("DEL", KEYS[2], KEYS[3])
|
||||
local limit = tonumber(ARGV[3])
|
||||
if limit > 0 then
|
||||
redis.call("RPUSH", KEYS[4], ARGV[2])
|
||||
redis.call("LTRIM", KEYS[4], -limit, -1)
|
||||
end
|
||||
return 1
|
||||
`
|
||||
@@ -0,0 +1,315 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (m *RedisManager) normalizeDefinition(ctx context.Context, definition Definition) (Definition, error) {
|
||||
if m == nil {
|
||||
return Definition{}, ErrNilRedisExecutor
|
||||
}
|
||||
definition.ID = strings.TrimSpace(definition.ID)
|
||||
if definition.ID == "" {
|
||||
id, err := m.nextID(ctx)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
definition.ID = id
|
||||
}
|
||||
definition.Title = strings.TrimSpace(definition.Title)
|
||||
sortOrder, err := normalizeSortOrder(definition.SortOrder)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
operator, err := normalizeOperator(definition.Operator)
|
||||
if err != nil {
|
||||
return Definition{}, err
|
||||
}
|
||||
definition.SortOrder = sortOrder
|
||||
definition.Operator = operator
|
||||
if definition.MaxSize < 0 {
|
||||
return Definition{}, fmt.Errorf("%w: max_size must not be negative", ErrInvalidDefinition)
|
||||
}
|
||||
now := m.nowUTC()
|
||||
if definition.CreatedAt.IsZero() {
|
||||
definition.CreatedAt = now
|
||||
} else {
|
||||
definition.CreatedAt = definition.CreatedAt.UTC()
|
||||
}
|
||||
if definition.UpdatedAt.IsZero() {
|
||||
definition.UpdatedAt = definition.CreatedAt
|
||||
} else {
|
||||
definition.UpdatedAt = definition.UpdatedAt.UTC()
|
||||
}
|
||||
definition.Metadata = cloneStringMap(definition.Metadata)
|
||||
return definition, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) definitions(ctx context.Context) ([]Definition, error) {
|
||||
value, err := m.do(ctx, "SMEMBERS", m.indexKey())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids, err := redisStrings(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
definitions := make([]Definition, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
definition, ok, err := m.definition(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok {
|
||||
_, _ = m.do(ctx, "SREM", m.indexKey(), id)
|
||||
continue
|
||||
}
|
||||
definitions = append(definitions, definition)
|
||||
}
|
||||
sort.Slice(definitions, func(i, j int) bool { return definitions[i].ID < definitions[j].ID })
|
||||
return definitions, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) definition(ctx context.Context, leaderboardID string) (Definition, bool, error) {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
if leaderboardID == "" {
|
||||
return Definition{}, false, nil
|
||||
}
|
||||
value, err := m.do(ctx, "GET", m.definitionKey(leaderboardID))
|
||||
if err != nil {
|
||||
return Definition{}, false, err
|
||||
}
|
||||
data, ok, err := redisBytes(value)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return Definition{}, false, err
|
||||
}
|
||||
return Definition{}, false, nil
|
||||
}
|
||||
var definition Definition
|
||||
if err := json.Unmarshal(data, &definition); err != nil {
|
||||
return Definition{}, false, fmt.Errorf("decode leaderboard definition: %w", err)
|
||||
}
|
||||
return cloneDefinition(definition), true, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) saveRecord(ctx context.Context, definition Definition, record Record) error {
|
||||
return m.saveRecordDetails(ctx, definition, record, HistoryActionSubmit, HistoryDetails{})
|
||||
}
|
||||
|
||||
func (m *RedisManager) saveRecordDetails(ctx context.Context, definition Definition, record Record, action string, details HistoryDetails) error {
|
||||
data, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
history, err := m.historyData(action, definition.ID, &record, nil, record.UpdatedAt, details)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = m.redisEvalInt(ctx, redisSaveRecord, []string{
|
||||
m.recordsKey(definition.ID),
|
||||
m.zsetKey(definition.ID),
|
||||
m.historyKey(definition.ID),
|
||||
}, record.OwnerID, data, redisRankScore(record, definition.SortOrder), history, m.historyLimitValue())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.appendHistoryData(ctx, history)
|
||||
}
|
||||
|
||||
func (m *RedisManager) record(ctx context.Context, leaderboardID string, ownerID string) (Record, bool, error) {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" || ownerID == "" {
|
||||
return Record{}, false, nil
|
||||
}
|
||||
value, err := m.do(ctx, "HGET", m.recordsKey(leaderboardID), ownerID)
|
||||
if err != nil {
|
||||
return Record{}, false, err
|
||||
}
|
||||
data, ok, err := redisBytes(value)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return Record{}, false, err
|
||||
}
|
||||
return Record{}, false, nil
|
||||
}
|
||||
var record Record
|
||||
if err := json.Unmarshal(data, &record); err != nil {
|
||||
return Record{}, false, fmt.Errorf("decode leaderboard record: %w", err)
|
||||
}
|
||||
return cloneRecord(record), true, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) rankedRecords(ctx context.Context, leaderboardID string) ([]Record, error) {
|
||||
definition, ok, err := m.definition(ctx, leaderboardID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, ErrLeaderboardNotFound
|
||||
}
|
||||
return m.rangeRecords(ctx, definition.ID, 0, -1, 1)
|
||||
}
|
||||
|
||||
func (m *RedisManager) rangeRecords(ctx context.Context, leaderboardID string, start int, stop int, rankStart int) ([]Record, error) {
|
||||
value, err := m.do(ctx, "ZREVRANGE", m.zsetKey(leaderboardID), start, stop)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
owners, err := redisStrings(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(owners) == 0 {
|
||||
return []Record{}, nil
|
||||
}
|
||||
return m.recordsByOwners(ctx, leaderboardID, owners, rankStart)
|
||||
}
|
||||
|
||||
func (m *RedisManager) recordsByOwners(ctx context.Context, leaderboardID string, owners []string, rankStart int) ([]Record, error) {
|
||||
if len(owners) == 0 {
|
||||
return []Record{}, nil
|
||||
}
|
||||
args := make([]any, 0, len(owners)+1)
|
||||
args = append(args, m.recordsKey(leaderboardID))
|
||||
for _, ownerID := range owners {
|
||||
args = append(args, ownerID)
|
||||
}
|
||||
value, err := m.do(ctx, "HMGET", args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items, found, err := redisBulkBytes(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Record, 0, len(owners))
|
||||
for idx, ownerID := range owners {
|
||||
if idx >= len(items) || idx >= len(found) || !found[idx] {
|
||||
_, _ = m.do(ctx, "ZREM", m.zsetKey(leaderboardID), ownerID)
|
||||
continue
|
||||
}
|
||||
var record Record
|
||||
if err := json.Unmarshal(items[idx], &record); err != nil {
|
||||
_, _ = m.do(ctx, "HDEL", m.recordsKey(leaderboardID), ownerID)
|
||||
_, _ = m.do(ctx, "ZREM", m.zsetKey(leaderboardID), ownerID)
|
||||
continue
|
||||
}
|
||||
record.Rank = rankStart + idx
|
||||
out = append(out, cloneRecord(record))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) recordWithRank(ctx context.Context, leaderboardID string, ownerID string) (Record, bool, error) {
|
||||
leaderboardID = strings.TrimSpace(leaderboardID)
|
||||
ownerID = strings.TrimSpace(ownerID)
|
||||
if leaderboardID == "" {
|
||||
return Record{}, false, ErrLeaderboardNotFound
|
||||
}
|
||||
if ownerID == "" {
|
||||
return Record{}, false, nil
|
||||
}
|
||||
rank, ok, err := m.redisRank(ctx, leaderboardID, ownerID)
|
||||
if err != nil {
|
||||
return Record{}, false, err
|
||||
}
|
||||
if !ok {
|
||||
return Record{}, false, nil
|
||||
}
|
||||
record, ok, err := m.record(ctx, leaderboardID, ownerID)
|
||||
if err != nil || !ok {
|
||||
if err != nil {
|
||||
return Record{}, false, err
|
||||
}
|
||||
_, _ = m.do(ctx, "ZREM", m.zsetKey(leaderboardID), ownerID)
|
||||
return Record{}, false, nil
|
||||
}
|
||||
record.Rank = rank
|
||||
return cloneRecord(record), true, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) redisRank(ctx context.Context, leaderboardID string, ownerID string) (int, bool, error) {
|
||||
value, err := m.do(ctx, "ZREVRANK", m.zsetKey(leaderboardID), ownerID)
|
||||
if err != nil || value == nil {
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return 0, false, nil
|
||||
}
|
||||
rank, err := redisInt64(value)
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
return int(rank) + 1, true, nil
|
||||
}
|
||||
|
||||
func (m *RedisManager) trim(ctx context.Context, definition Definition) error {
|
||||
if definition.MaxSize <= 0 {
|
||||
return nil
|
||||
}
|
||||
total, err := m.redisInt(ctx, "ZCARD", m.zsetKey(definition.ID))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if total <= int64(definition.MaxSize) {
|
||||
return nil
|
||||
}
|
||||
trimmed, err := m.rangeRecords(ctx, definition.ID, definition.MaxSize, -1, definition.MaxSize+1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.deleteRecords(ctx, definition, trimmed, HistoryActionTrimRecord)
|
||||
}
|
||||
|
||||
func (m *RedisManager) deleteRecords(ctx context.Context, definition Definition, records []Record, action string) error {
|
||||
return m.delRecordsDetail(ctx, definition, records, action, HistoryDetails{})
|
||||
}
|
||||
|
||||
func (m *RedisManager) delRecordsDetail(ctx context.Context, definition Definition, records []Record, action string, details HistoryDetails) error {
|
||||
if len(records) == 0 {
|
||||
return nil
|
||||
}
|
||||
args := make([]any, 0, 2+len(records)*2)
|
||||
args = append(args, len(records), m.historyLimitValue())
|
||||
histories := make([][]byte, 0, len(records))
|
||||
now := m.nowUTC()
|
||||
for _, record := range records {
|
||||
history, err := m.historyData(action, definition.ID, &record, nil, now, details)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args = append(args, record.OwnerID, history)
|
||||
histories = append(histories, history)
|
||||
}
|
||||
_, err := m.redisEvalInt(ctx, redisDeleteRecords, []string{
|
||||
m.recordsKey(definition.ID),
|
||||
m.zsetKey(definition.ID),
|
||||
m.historyKey(definition.ID),
|
||||
}, args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return m.appendHistoryData(ctx, histories...)
|
||||
}
|
||||
|
||||
func (m *RedisManager) appendHistoryPayload(ctx context.Context, leaderboardID string, history []byte) error {
|
||||
if len(history) == 0 {
|
||||
return nil
|
||||
}
|
||||
historyLimit := m.historyLimitValue()
|
||||
if historyLimit > 0 {
|
||||
if _, err := m.do(ctx, "RPUSH", m.historyKey(leaderboardID), history); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := m.do(ctx, "LTRIM", m.historyKey(leaderboardID), -historyLimit, -1); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return m.appendHistoryData(ctx, history)
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidSettlement = fmt.Errorf("%w: settlement is invalid", ErrInvalidSubmission)
|
||||
ErrNilRewardOutbox = errors.New("leaderboard reward outbox is required")
|
||||
)
|
||||
|
||||
const RewardOutboxPending = "pending"
|
||||
|
||||
type RewardRule struct {
|
||||
MinRank int `json:"min_rank"`
|
||||
MaxRank int `json:"max_rank"`
|
||||
Payload json.RawMessage `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
type RewardOptions struct {
|
||||
SettlementID string `json:"settlement_id"`
|
||||
Rules []RewardRule `json:"rules"`
|
||||
}
|
||||
|
||||
type RewardGrant struct {
|
||||
SettlementID string `json:"settlement_id"`
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Rank int `json:"rank"`
|
||||
Score int64 `json:"score"`
|
||||
Subscore int64 `json:"subscore,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
Payload json.RawMessage `json:"payload,omitempty"`
|
||||
}
|
||||
|
||||
type RewardOutboxEvent struct {
|
||||
ID string `json:"id"`
|
||||
Grant RewardGrant `json:"grant"`
|
||||
Status string `json:"status"`
|
||||
Attempts int `json:"attempts,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type RewardOutbox interface {
|
||||
EnqueueRewardGrants(context.Context, []RewardGrant) ([]RewardOutboxEvent, error)
|
||||
PendingRewardGrants(context.Context, int) ([]RewardOutboxEvent, error)
|
||||
}
|
||||
|
||||
type MemoryRewardOutbox struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
order []string
|
||||
events map[string]RewardOutboxEvent
|
||||
}
|
||||
|
||||
type SeasonSettlementOptions struct {
|
||||
SettlementID string `json:"settlement_id"`
|
||||
RewardRules []RewardRule `json:"reward_rules,omitempty"`
|
||||
RewardOutbox RewardOutbox `json:"-"`
|
||||
CaptureLimit int `json:"capture_limit,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
type SeasonSettlement struct {
|
||||
SettlementID string `json:"settlement_id"`
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
Capture Capture `json:"capture"`
|
||||
Grants []RewardGrant `json:"grants"`
|
||||
OutboxEvents []RewardOutboxEvent `json:"outbox_events,omitempty"`
|
||||
Reset Definition `json:"reset"`
|
||||
}
|
||||
|
||||
func NewMemoryRewardOutbox(now func() time.Time) *MemoryRewardOutbox {
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return &MemoryRewardOutbox{
|
||||
now: now,
|
||||
events: make(map[string]RewardOutboxEvent),
|
||||
}
|
||||
}
|
||||
|
||||
func (o *MemoryRewardOutbox) EnqueueRewardGrants(ctx context.Context, grants []RewardGrant) ([]RewardOutboxEvent, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o == nil {
|
||||
return nil, ErrNilRewardOutbox
|
||||
}
|
||||
o.mu.Lock()
|
||||
defer o.mu.Unlock()
|
||||
|
||||
// 内存 outbox 以 grant.IdempotencyKey 去重;同 key 不重复追加奖励事件,方便结算任务安全重跑。
|
||||
events := make([]RewardOutboxEvent, 0, len(grants))
|
||||
for _, grant := range grants {
|
||||
normalized := cloneRewardGrant(grant)
|
||||
normalized.IdempotencyKey = strings.TrimSpace(normalized.IdempotencyKey)
|
||||
if normalized.IdempotencyKey == "" {
|
||||
return nil, ErrInvalidSettlement
|
||||
}
|
||||
if existing, ok := o.events[normalized.IdempotencyKey]; ok {
|
||||
events = append(events, cloneRewardOutbox(existing))
|
||||
continue
|
||||
}
|
||||
now := o.now().UTC()
|
||||
event := RewardOutboxEvent{
|
||||
ID: normalized.IdempotencyKey,
|
||||
Grant: normalized,
|
||||
Status: RewardOutboxPending,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
o.order = append(o.order, event.ID)
|
||||
o.events[event.ID] = event
|
||||
events = append(events, cloneRewardOutbox(event))
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (o *MemoryRewardOutbox) PendingRewardGrants(ctx context.Context, limit int) ([]RewardOutboxEvent, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if o == nil {
|
||||
return nil, ErrNilRewardOutbox
|
||||
}
|
||||
if limit <= 0 || limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
o.mu.Lock()
|
||||
defer o.mu.Unlock()
|
||||
|
||||
events := make([]RewardOutboxEvent, 0, limit)
|
||||
for _, id := range o.order {
|
||||
event := o.events[id]
|
||||
if event.Status != RewardOutboxPending {
|
||||
continue
|
||||
}
|
||||
events = append(events, cloneRewardOutbox(event))
|
||||
if len(events) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func CloseSeason(ctx context.Context, runtime Runtime, leaderboardID string, options SeasonSettlementOptions) (SeasonSettlement, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return SeasonSettlement{}, err
|
||||
}
|
||||
if runtime == nil {
|
||||
return SeasonSettlement{}, ErrLeaderboardNotFound
|
||||
}
|
||||
settlementID := strings.TrimSpace(options.SettlementID)
|
||||
if settlementID == "" {
|
||||
return SeasonSettlement{}, ErrInvalidSettlement
|
||||
}
|
||||
// 结算顺序固定为:截榜快照 -> 生成奖励幂等键 -> 写奖励 outbox -> 可选清榜,避免清榜后丢奖励证据。
|
||||
capture, err := runtime.Capture(ctx, leaderboardID, CaptureOptions{
|
||||
Limit: options.CaptureLimit,
|
||||
Reason: options.Reason,
|
||||
OperatorID: options.OperatorID,
|
||||
RequestID: options.RequestID,
|
||||
Metadata: map[string]string{"settlement_id": settlementID},
|
||||
})
|
||||
if err != nil {
|
||||
return SeasonSettlement{}, err
|
||||
}
|
||||
grants, err := BuildRewardGrants(capture, RewardOptions{SettlementID: settlementID, Rules: options.RewardRules})
|
||||
if err != nil {
|
||||
return SeasonSettlement{}, err
|
||||
}
|
||||
var events []RewardOutboxEvent
|
||||
if len(grants) > 0 {
|
||||
if options.RewardOutbox == nil {
|
||||
return SeasonSettlement{}, ErrNilRewardOutbox
|
||||
}
|
||||
events, err = options.RewardOutbox.EnqueueRewardGrants(ctx, grants)
|
||||
if err != nil {
|
||||
return SeasonSettlement{}, err
|
||||
}
|
||||
}
|
||||
reset, err := runtime.Reset(ctx, leaderboardID)
|
||||
if err != nil {
|
||||
return SeasonSettlement{}, err
|
||||
}
|
||||
return SeasonSettlement{
|
||||
SettlementID: settlementID,
|
||||
LeaderboardID: strings.TrimSpace(leaderboardID),
|
||||
Capture: cloneCapture(capture),
|
||||
Grants: cloneRewardGrants(grants),
|
||||
OutboxEvents: cloneRewardEvents(events),
|
||||
Reset: cloneDefinition(reset),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func BuildRewardGrants(capture Capture, options RewardOptions) ([]RewardGrant, error) {
|
||||
settlementID := strings.TrimSpace(options.SettlementID)
|
||||
if settlementID == "" || strings.TrimSpace(capture.LeaderboardID) == "" {
|
||||
return nil, ErrInvalidSettlement
|
||||
}
|
||||
rules, err := normalizeRewardRules(options.Rules)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rules) == 0 || len(capture.Records) == 0 {
|
||||
return []RewardGrant{}, nil
|
||||
}
|
||||
// 奖励幂等键由 settlement/leaderboard/player/rank 组成,同一次结算重放不会重复发奖。
|
||||
grants := make([]RewardGrant, 0, len(capture.Records))
|
||||
for _, record := range capture.Records {
|
||||
if strings.TrimSpace(record.OwnerID) == "" || record.Rank <= 0 {
|
||||
continue
|
||||
}
|
||||
rule, ok := rewardRuleForRank(rules, record.Rank)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
grants = append(grants, RewardGrant{
|
||||
SettlementID: settlementID,
|
||||
LeaderboardID: strings.TrimSpace(capture.LeaderboardID),
|
||||
OwnerID: strings.TrimSpace(record.OwnerID),
|
||||
Rank: record.Rank,
|
||||
Score: record.Score,
|
||||
Subscore: record.Subscore,
|
||||
IdempotencyKey: rewardGrantKey(settlementID, capture.LeaderboardID, record.OwnerID, record.Rank),
|
||||
Payload: cloneRewardPayload(rule.Payload),
|
||||
})
|
||||
}
|
||||
return grants, nil
|
||||
}
|
||||
|
||||
func normalizeRewardRules(rules []RewardRule) ([]RewardRule, error) {
|
||||
out := make([]RewardRule, 0, len(rules))
|
||||
for _, rule := range rules {
|
||||
if rule.MinRank <= 0 {
|
||||
return nil, ErrInvalidSettlement
|
||||
}
|
||||
if rule.MaxRank <= 0 {
|
||||
rule.MaxRank = rule.MinRank
|
||||
}
|
||||
if rule.MaxRank < rule.MinRank {
|
||||
return nil, ErrInvalidSettlement
|
||||
}
|
||||
if len(rule.Payload) > 0 && !json.Valid(rule.Payload) {
|
||||
return nil, ErrInvalidSettlement
|
||||
}
|
||||
rule.Payload = cloneRewardPayload(rule.Payload)
|
||||
out = append(out, rule)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func rewardRuleForRank(rules []RewardRule, rank int) (RewardRule, bool) {
|
||||
for _, rule := range rules {
|
||||
if rank >= rule.MinRank && rank <= rule.MaxRank {
|
||||
return rule, true
|
||||
}
|
||||
}
|
||||
return RewardRule{}, false
|
||||
}
|
||||
|
||||
func rewardGrantKey(settlementID, leaderboardID, ownerID string, rank int) string {
|
||||
return fmt.Sprintf("leaderboard:settlement:v1:%s:%s:%s:%d",
|
||||
encodeKeyPart(settlementID),
|
||||
encodeKeyPart(leaderboardID),
|
||||
encodeKeyPart(ownerID),
|
||||
rank,
|
||||
)
|
||||
}
|
||||
|
||||
func cloneRewardPayload(payload json.RawMessage) json.RawMessage {
|
||||
if payload == nil {
|
||||
return nil
|
||||
}
|
||||
return append(json.RawMessage(nil), payload...)
|
||||
}
|
||||
|
||||
func cloneRewardGrant(grant RewardGrant) RewardGrant {
|
||||
grant.Payload = cloneRewardPayload(grant.Payload)
|
||||
return grant
|
||||
}
|
||||
|
||||
func cloneRewardGrants(grants []RewardGrant) []RewardGrant {
|
||||
out := make([]RewardGrant, len(grants))
|
||||
for i, grant := range grants {
|
||||
out[i] = cloneRewardGrant(grant)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneRewardOutbox(event RewardOutboxEvent) RewardOutboxEvent {
|
||||
event.Grant = cloneRewardGrant(event.Grant)
|
||||
return event
|
||||
}
|
||||
|
||||
func cloneRewardEvents(events []RewardOutboxEvent) []RewardOutboxEvent {
|
||||
out := make([]RewardOutboxEvent, len(events))
|
||||
for i, event := range events {
|
||||
out[i] = cloneRewardOutbox(event)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
package leaderboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLeaderboardExists = errors.New("leaderboard already exists")
|
||||
ErrLeaderboardNotFound = errors.New("leaderboard not found")
|
||||
ErrRecordNotFound = errors.New("leaderboard record not found")
|
||||
ErrRecordTrimmed = errors.New("leaderboard record was trimmed")
|
||||
ErrInvalidDefinition = errors.New("leaderboard definition is invalid")
|
||||
ErrInvalidSubmission = errors.New("leaderboard submission is invalid")
|
||||
ErrScoreOverflow = errors.New("leaderboard score overflow")
|
||||
ErrManagerRequired = errors.New("leaderboard manager is required")
|
||||
)
|
||||
|
||||
const (
|
||||
SortDescending = "desc"
|
||||
SortAscending = "asc"
|
||||
|
||||
OperatorBest = "best"
|
||||
OperatorSet = "set"
|
||||
OperatorIncrement = "increment"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultListLimit = 50
|
||||
defaultAroundLimit = 5
|
||||
maxListLimit = 500
|
||||
defaultHistoryLimit = 1000
|
||||
)
|
||||
|
||||
type Runtime interface {
|
||||
Create(context.Context, Definition) (Definition, error)
|
||||
Delete(context.Context, string) (Definition, error)
|
||||
Definition(string) (Definition, bool)
|
||||
Definitions() []Definition
|
||||
Submit(context.Context, string, Submission) (Record, error)
|
||||
DeleteRecord(context.Context, string, string) (Record, error)
|
||||
Repair(context.Context, string, RepairRequest) (RepairReceipt, error)
|
||||
Record(string, string) (Record, bool)
|
||||
List(context.Context, string, ListOptions) ([]Record, error)
|
||||
MergedView(context.Context, MergedViewOptions) (MergedView, error)
|
||||
AroundOwner(context.Context, string, string, int) ([]Record, error)
|
||||
Rank(context.Context, string, string) (int, bool, error)
|
||||
Reset(context.Context, string) (Definition, error)
|
||||
Capture(context.Context, string, CaptureOptions) (Capture, error)
|
||||
History(context.Context, string, int) ([]HistoryEntry, error)
|
||||
Snapshot() Snapshot
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Name string
|
||||
Now func() time.Time
|
||||
IDGenerator func(prefix string) string
|
||||
HistoryLimit int
|
||||
HistoryStore HistoryStore
|
||||
HistoryStrict bool
|
||||
}
|
||||
|
||||
type Definition struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title,omitempty"`
|
||||
SortOrder string `json:"sort_order"`
|
||||
Operator string `json:"operator"`
|
||||
MaxSize int `json:"max_size,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
OwnerID string `json:"owner_id"`
|
||||
Score int64 `json:"score"`
|
||||
Subscore int64 `json:"subscore,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Score int64 `json:"score"`
|
||||
Subscore int64 `json:"subscore,omitempty"`
|
||||
Rank int `json:"rank,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
const (
|
||||
HistoryActionCreate = "create"
|
||||
HistoryActionSubmit = "submit"
|
||||
HistoryActionDeleteRecord = "delete_record"
|
||||
HistoryActionTrimRecord = "trim_record"
|
||||
HistoryActionReset = "reset"
|
||||
HistoryDeleteBoard = "delete_leaderboard"
|
||||
HistoryActionRepairRecord = "repair_record"
|
||||
HistoryActionCapture = "capture"
|
||||
)
|
||||
|
||||
type HistoryEntry struct {
|
||||
Action string `json:"action"`
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
OwnerID string `json:"owner_id,omitempty"`
|
||||
Record *Record `json:"record,omitempty"`
|
||||
Records []Record `json:"records,omitempty"`
|
||||
Definition *Definition `json:"definition,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
type HistoryDetails struct {
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
Records []Record `json:"records,omitempty"`
|
||||
}
|
||||
|
||||
type HistoryStore interface {
|
||||
Append(context.Context, HistoryEntry) error
|
||||
List(context.Context, string, int) ([]HistoryEntry, error)
|
||||
}
|
||||
|
||||
type ListOptions struct {
|
||||
Offset int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type MergedViewOptions struct {
|
||||
IDs []string `json:"ids"`
|
||||
SortOrder string `json:"sort_order,omitempty"`
|
||||
Offset int `json:"offset,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
DedupeOwner bool `json:"dedupe_owner,omitempty"`
|
||||
}
|
||||
|
||||
type MergedView struct {
|
||||
IDs []string `json:"ids"`
|
||||
SortOrder string `json:"sort_order"`
|
||||
Records []Record `json:"records"`
|
||||
RecordCount int `json:"record_count"`
|
||||
GeneratedAt time.Time `json:"generated_at"`
|
||||
}
|
||||
|
||||
type RepairRequest struct {
|
||||
OwnerID string `json:"owner_id"`
|
||||
Score int64 `json:"score,omitempty"`
|
||||
Subscore int64 `json:"subscore,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
Delete bool `json:"delete,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
ResetCreatedAt bool `json:"reset_created_at,omitempty"`
|
||||
}
|
||||
|
||||
type RepairReceipt struct {
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Action string `json:"action"`
|
||||
Before *Record `json:"before,omitempty"`
|
||||
After *Record `json:"after,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
|
||||
type CaptureOptions struct {
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
type Capture struct {
|
||||
LeaderboardID string `json:"leaderboard_id"`
|
||||
Records []Record `json:"records"`
|
||||
RecordCount int `json:"record_count"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
OperatorID string `json:"operator_id,omitempty"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
Metadata map[string]string `json:"metadata,omitempty"`
|
||||
CapturedAt time.Time `json:"captured_at"`
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Name string `json:"name"`
|
||||
LeaderboardCount int `json:"leaderboard_count"`
|
||||
RecordCount int `json:"record_count"`
|
||||
RecordsByBoard map[string]int `json:"records_by_board,omitempty"`
|
||||
HistoryStoreErrors int `json:"history_store_errors,omitempty"`
|
||||
EventLogErrors int `json:"eventlog_errors,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"longheng.io/server/internal/platform/httpx"
|
||||
)
|
||||
|
||||
var ErrAdminMuxRequired = errors.New("moderation admin mux is required")
|
||||
|
||||
const maxAdminJSONBytes = 1 << 20
|
||||
|
||||
type AdminOptions struct {
|
||||
Prefix string
|
||||
Wrap func(http.HandlerFunc) http.HandlerFunc
|
||||
}
|
||||
|
||||
func RegisterAdminRoutes(mux *http.ServeMux, engine *Engine, options AdminOptions) error {
|
||||
return RegisterAdminRoutesWithEvaluator(mux, engine, engine, options)
|
||||
}
|
||||
|
||||
func RegisterAdminRoutesWithEvaluator(mux *http.ServeMux, engine *Engine, evaluator Evaluator, options AdminOptions) error {
|
||||
if mux == nil {
|
||||
return ErrAdminMuxRequired
|
||||
}
|
||||
prefix := normalizeAdminPrefix(options.Prefix)
|
||||
wrap := options.Wrap
|
||||
if wrap == nil {
|
||||
wrap = func(next http.HandlerFunc) http.HandlerFunc { return next }
|
||||
}
|
||||
mux.HandleFunc(prefix, wrap(SnapshotHandler(engine)))
|
||||
mux.HandleFunc(prefix+"/snapshot", wrap(SnapshotHandler(engine)))
|
||||
mux.HandleFunc(prefix+"/evaluate", wrap(EvaluateWithHandler(evaluator)))
|
||||
mux.HandleFunc(prefix+"/sanctions", wrap(SanctionsHandler(engine)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func SnapshotHandler(engine *Engine) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
httpx.WriteJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
if engine == nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "moderation engine is required"})
|
||||
return
|
||||
}
|
||||
var filter FilterSnapshot
|
||||
if engine.Filter() != nil {
|
||||
filter = engine.Filter().Snapshot()
|
||||
}
|
||||
var sanctions SanctionSnapshot
|
||||
var err error
|
||||
if store := engine.Sanctions(); store != nil {
|
||||
sanctions, err = store.Snapshot(r.Context())
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{
|
||||
"filter": filter,
|
||||
"sanctions": sanctions,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func EvaluateHandler(engine *Engine) http.HandlerFunc {
|
||||
return EvaluateWithHandler(engine)
|
||||
}
|
||||
|
||||
func EvaluateWithHandler(evaluator Evaluator) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
httpx.WriteJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
return
|
||||
}
|
||||
if evaluator == nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "moderation evaluator is required"})
|
||||
return
|
||||
}
|
||||
var request Request
|
||||
if err := decodeModAdminJSON(w, r, &request); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
decision, err := evaluator.Evaluate(r.Context(), request)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, decision)
|
||||
}
|
||||
}
|
||||
|
||||
func SanctionsHandler(engine *Engine) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if engine == nil || engine.Sanctions() == nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "moderation sanction store is required"})
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
handleGetSanctions(w, r, engine.Sanctions())
|
||||
case http.MethodPost:
|
||||
handlePostSanction(w, r, engine.Sanctions())
|
||||
case http.MethodDelete:
|
||||
handleDeleteSanction(w, r, engine.Sanctions())
|
||||
default:
|
||||
w.Header().Set("Allow", strings.Join([]string{http.MethodGet, http.MethodPost, http.MethodDelete}, ", "))
|
||||
httpx.WriteJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetSanctions(w http.ResponseWriter, r *http.Request, store SanctionStore) {
|
||||
subject := strings.TrimSpace(r.URL.Query().Get("subject"))
|
||||
if subject != "" {
|
||||
items, err := store.Active(r.Context(), SanctionQuery{
|
||||
Subject: subject,
|
||||
Scope: r.URL.Query().Get("scope"),
|
||||
})
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{"items": items})
|
||||
return
|
||||
}
|
||||
snapshot, err := store.Snapshot(r.Context())
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusServiceUnavailable, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, snapshot)
|
||||
}
|
||||
|
||||
func handlePostSanction(w http.ResponseWriter, r *http.Request, store SanctionStore) {
|
||||
var sanction Sanction
|
||||
if err := decodeModAdminJSON(w, r, &sanction); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
item, err := store.Upsert(r.Context(), sanction)
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{"sanction": item})
|
||||
}
|
||||
|
||||
// decodeModAdminJSON 统一限制管理端 JSON 的大小、字段和单文档格式。
|
||||
func decodeModAdminJSON(w http.ResponseWriter, r *http.Request, out any) error {
|
||||
return httpx.DecodeStrictJSON(w, r, maxAdminJSONBytes, out)
|
||||
}
|
||||
|
||||
func handleDeleteSanction(w http.ResponseWriter, r *http.Request, store SanctionStore) {
|
||||
id := strings.TrimSpace(r.URL.Query().Get("id"))
|
||||
if id == "" {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": "id is required"})
|
||||
return
|
||||
}
|
||||
if err := store.Remove(r.Context(), id); err != nil {
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, ErrSanctionMissing) {
|
||||
status = http.StatusNotFound
|
||||
}
|
||||
httpx.WriteJSON(w, status, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
|
||||
}
|
||||
|
||||
func normalizeAdminPrefix(prefix string) string {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
if prefix == "" {
|
||||
return "/debug/moderation"
|
||||
}
|
||||
if !strings.HasPrefix(prefix, "/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
prefix = strings.TrimRight(prefix, "/")
|
||||
if prefix == "" {
|
||||
return "/debug/moderation"
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
Subject string `json:"subject"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Now time.Time `json:"now,omitempty"`
|
||||
}
|
||||
|
||||
type Decision struct {
|
||||
Allowed bool `json:"allowed"`
|
||||
Subject string `json:"subject"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
OriginalText string `json:"original_text,omitempty"`
|
||||
SanitizedText string `json:"sanitized_text,omitempty"`
|
||||
Text TextResult `json:"text"`
|
||||
Sanctions []Sanction `json:"sanctions,omitempty"`
|
||||
Reasons []string `json:"reasons,omitempty"`
|
||||
}
|
||||
|
||||
type Engine struct {
|
||||
mu sync.RWMutex
|
||||
filter *Filter
|
||||
store SanctionStore
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewEngine(filter *Filter, store SanctionStore) *Engine {
|
||||
if filter == nil {
|
||||
filter, _ = NewFilter()
|
||||
}
|
||||
if store == nil {
|
||||
store = NewMemorySanctionStore()
|
||||
}
|
||||
return &Engine{
|
||||
filter: filter,
|
||||
store: store,
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) SetNow(now func() time.Time) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
e.mu.Lock()
|
||||
e.now = now
|
||||
e.mu.Unlock()
|
||||
if store, ok := e.store.(*MemorySanctionStore); ok {
|
||||
store.SetNow(now)
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Engine) Evaluate(ctx context.Context, request Request) (Decision, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Decision{}, err
|
||||
}
|
||||
if e == nil {
|
||||
return Decision{}, fmt.Errorf("%w: engine is nil", ErrInvalidRequest)
|
||||
}
|
||||
subject := normalizeToken(request.Subject)
|
||||
if subject == "" {
|
||||
return Decision{}, fmt.Errorf("%w: subject is required", ErrInvalidRequest)
|
||||
}
|
||||
scope := normalizeToken(request.Scope)
|
||||
now := request.Now
|
||||
if now.IsZero() {
|
||||
now = e.nowUTC()
|
||||
} else {
|
||||
now = now.UTC()
|
||||
}
|
||||
|
||||
decision := Decision{
|
||||
Allowed: true,
|
||||
Subject: subject,
|
||||
Scope: scope,
|
||||
OriginalText: request.Text,
|
||||
SanitizedText: request.Text,
|
||||
Text: TextResult{Original: request.Text, Sanitized: request.Text},
|
||||
}
|
||||
|
||||
if e.store != nil {
|
||||
sanctions, err := e.store.Active(ctx, SanctionQuery{Subject: subject, Scope: scope, Now: now})
|
||||
if err != nil {
|
||||
return Decision{}, err
|
||||
}
|
||||
decision.Sanctions = sanctions
|
||||
for _, sanction := range sanctions {
|
||||
switch sanction.Kind {
|
||||
case SanctionBan:
|
||||
decision.Allowed = false
|
||||
decision.Reasons = append(decision.Reasons, reasonForSanction(sanction))
|
||||
case SanctionMute:
|
||||
if strings.TrimSpace(request.Text) != "" {
|
||||
decision.Allowed = false
|
||||
decision.Reasons = append(decision.Reasons, reasonForSanction(sanction))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if e.filter != nil && request.Text != "" {
|
||||
text := e.filter.Evaluate(scope, request.Text)
|
||||
decision.Text = text
|
||||
decision.SanitizedText = text.Sanitized
|
||||
if text.Rejected {
|
||||
decision.Allowed = false
|
||||
decision.Reasons = append(decision.Reasons, "text_rejected")
|
||||
}
|
||||
}
|
||||
return decision, nil
|
||||
}
|
||||
|
||||
func (e *Engine) Filter() *Filter {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.filter
|
||||
}
|
||||
|
||||
func (e *Engine) Sanctions() SanctionStore {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.store
|
||||
}
|
||||
|
||||
func reasonForSanction(sanction Sanction) string {
|
||||
if sanction.Scope == "" {
|
||||
return string(sanction.Kind)
|
||||
}
|
||||
return string(sanction.Kind) + ":" + sanction.Scope
|
||||
}
|
||||
|
||||
func (e *Engine) nowUTC() time.Time {
|
||||
if e == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
e.mu.RLock()
|
||||
now := e.now
|
||||
e.mu.RUnlock()
|
||||
if now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return now().UTC()
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidWordRule = errors.New("moderation word rule is invalid")
|
||||
ErrInvalidRequest = errors.New("moderation request is invalid")
|
||||
)
|
||||
|
||||
type Action string
|
||||
|
||||
const (
|
||||
ActionMask Action = "mask"
|
||||
ActionReject Action = "reject"
|
||||
)
|
||||
|
||||
type WordRule struct {
|
||||
ID string `json:"id"`
|
||||
Word string `json:"word"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Action Action `json:"action,omitempty"`
|
||||
Replacement string `json:"replacement,omitempty"`
|
||||
Severity int `json:"severity,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
IgnoreSeparators bool `json:"ignore_separators,omitempty"`
|
||||
WholeWord bool `json:"whole_word,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type WordSnapshot struct {
|
||||
Version string `json:"version,omitempty"`
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
||||
Rules []WordRule `json:"rules,omitempty"`
|
||||
}
|
||||
|
||||
type FilterSnapshot struct {
|
||||
Version string `json:"version,omitempty"`
|
||||
UpdatedAt time.Time `json:"updated_at,omitempty"`
|
||||
Rules []WordRule `json:"rules,omitempty"`
|
||||
}
|
||||
|
||||
type Match struct {
|
||||
RuleID string `json:"rule_id"`
|
||||
Word string `json:"word"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Action Action `json:"action"`
|
||||
Replacement string `json:"replacement,omitempty"`
|
||||
Severity int `json:"severity,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
Start int `json:"start"`
|
||||
End int `json:"end"`
|
||||
}
|
||||
|
||||
type TextResult struct {
|
||||
Original string `json:"original"`
|
||||
Sanitized string `json:"sanitized"`
|
||||
Rejected bool `json:"rejected"`
|
||||
Matches []Match `json:"matches,omitempty"`
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
mu sync.RWMutex
|
||||
version string
|
||||
updatedAt time.Time
|
||||
rules []compiledRule
|
||||
}
|
||||
|
||||
type compiledRule struct {
|
||||
rule WordRule
|
||||
normalized []rune
|
||||
}
|
||||
|
||||
type normalizedRune struct {
|
||||
value rune
|
||||
originalIndex int
|
||||
}
|
||||
|
||||
func NewFilter(rules ...WordRule) (*Filter, error) {
|
||||
filter := &Filter{}
|
||||
if len(rules) > 0 {
|
||||
if err := filter.ReplaceRules(context.Background(), WordSnapshot{Rules: rules}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
func (f *Filter) ReplaceRules(ctx context.Context, snapshot WordSnapshot) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
compiled := make([]compiledRule, 0, len(snapshot.Rules))
|
||||
seen := make(map[string]struct{}, len(snapshot.Rules))
|
||||
for _, rule := range snapshot.Rules {
|
||||
item, err := normalizeWordRule(rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, exists := seen[item.ID]; exists {
|
||||
return fmt.Errorf("%w: duplicate id %q", ErrInvalidWordRule, item.ID)
|
||||
}
|
||||
seen[item.ID] = struct{}{}
|
||||
normalized := normalizeRuleWord(item.Word, item.IgnoreSeparators)
|
||||
if len(normalized) == 0 {
|
||||
return fmt.Errorf("%w: word %q has no comparable characters", ErrInvalidWordRule, item.ID)
|
||||
}
|
||||
compiled = append(compiled, compiledRule{rule: item, normalized: normalized})
|
||||
}
|
||||
sort.Slice(compiled, func(i, j int) bool {
|
||||
if compiled[i].rule.Scope != compiled[j].rule.Scope {
|
||||
return compiled[i].rule.Scope < compiled[j].rule.Scope
|
||||
}
|
||||
if len(compiled[i].normalized) != len(compiled[j].normalized) {
|
||||
return len(compiled[i].normalized) > len(compiled[j].normalized)
|
||||
}
|
||||
return compiled[i].rule.ID < compiled[j].rule.ID
|
||||
})
|
||||
|
||||
f.mu.Lock()
|
||||
f.version = strings.TrimSpace(snapshot.Version)
|
||||
f.updatedAt = snapshot.UpdatedAt.UTC()
|
||||
if f.updatedAt.IsZero() && len(compiled) > 0 {
|
||||
f.updatedAt = time.Now().UTC()
|
||||
}
|
||||
f.rules = compiled
|
||||
f.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Filter) ApplyJSON(ctx context.Context, data []byte) error {
|
||||
if len(strings.TrimSpace(string(data))) == 0 {
|
||||
return f.ReplaceRules(ctx, WordSnapshot{})
|
||||
}
|
||||
var snapshot WordSnapshot
|
||||
if err := json.Unmarshal(data, &snapshot); err != nil {
|
||||
return fmt.Errorf("decode moderation word snapshot: %w", err)
|
||||
}
|
||||
return f.ReplaceRules(ctx, snapshot)
|
||||
}
|
||||
|
||||
func (f *Filter) Evaluate(scope, text string) TextResult {
|
||||
result := TextResult{Original: text, Sanitized: text}
|
||||
if f == nil || text == "" {
|
||||
return result
|
||||
}
|
||||
scope = normalizeToken(scope)
|
||||
runes := []rune(text)
|
||||
if len(runes) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
f.mu.RLock()
|
||||
rules := append([]compiledRule(nil), f.rules...)
|
||||
f.mu.RUnlock()
|
||||
if len(rules) == 0 {
|
||||
return result
|
||||
}
|
||||
|
||||
matches := make([]Match, 0)
|
||||
normalizedText := map[bool][]normalizedRune{
|
||||
false: normalizeText(runes, false),
|
||||
true: normalizeText(runes, true),
|
||||
}
|
||||
for _, rule := range rules {
|
||||
if !ruleApplies(rule.rule.Scope, scope) {
|
||||
continue
|
||||
}
|
||||
matches = append(matches, matchRule(rule, runes, normalizedText[rule.rule.IgnoreSeparators])...)
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return result
|
||||
}
|
||||
sortMatches(matches)
|
||||
|
||||
mask := make([]rune, len(runes))
|
||||
for _, match := range matches {
|
||||
if match.Action == ActionReject {
|
||||
result.Rejected = true
|
||||
}
|
||||
if match.Action != ActionMask {
|
||||
continue
|
||||
}
|
||||
replacement := replacementRune(match.Replacement)
|
||||
for i := match.Start; i <= match.End && i < len(mask); i++ {
|
||||
if !unicode.IsSpace(runes[i]) {
|
||||
mask[i] = replacement
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range runes {
|
||||
if mask[i] != 0 {
|
||||
runes[i] = mask[i]
|
||||
}
|
||||
}
|
||||
result.Sanitized = string(runes)
|
||||
result.Matches = matches
|
||||
return result
|
||||
}
|
||||
|
||||
func (f *Filter) Snapshot() FilterSnapshot {
|
||||
if f == nil {
|
||||
return FilterSnapshot{}
|
||||
}
|
||||
f.mu.RLock()
|
||||
rules := make([]WordRule, 0, len(f.rules))
|
||||
for _, rule := range f.rules {
|
||||
rules = append(rules, cloneWordRule(rule.rule))
|
||||
}
|
||||
version := f.version
|
||||
updatedAt := f.updatedAt
|
||||
f.mu.RUnlock()
|
||||
return FilterSnapshot{Version: version, UpdatedAt: updatedAt, Rules: rules}
|
||||
}
|
||||
|
||||
func normalizeWordRule(rule WordRule) (WordRule, error) {
|
||||
rule.ID = normalizeToken(rule.ID)
|
||||
rule.Word = strings.TrimSpace(rule.Word)
|
||||
rule.Scope = normalizeToken(rule.Scope)
|
||||
rule.Replacement = strings.TrimSpace(rule.Replacement)
|
||||
rule.Tags = normalizeList(rule.Tags)
|
||||
rule.Meta = cloneStringMap(rule.Meta)
|
||||
if rule.ID == "" {
|
||||
rule.ID = normalizeToken(rule.Word)
|
||||
}
|
||||
if rule.ID == "" || rule.Word == "" {
|
||||
return WordRule{}, fmt.Errorf("%w: id and word are required", ErrInvalidWordRule)
|
||||
}
|
||||
if rule.Action == "" {
|
||||
rule.Action = ActionMask
|
||||
}
|
||||
if rule.Action != ActionMask && rule.Action != ActionReject {
|
||||
return WordRule{}, fmt.Errorf("%w: invalid action %q", ErrInvalidWordRule, rule.Action)
|
||||
}
|
||||
if rule.Replacement == "" {
|
||||
rule.Replacement = "*"
|
||||
}
|
||||
if rule.Severity < 0 {
|
||||
return WordRule{}, fmt.Errorf("%w: severity must be >= 0", ErrInvalidWordRule)
|
||||
}
|
||||
return rule, nil
|
||||
}
|
||||
|
||||
func matchRule(rule compiledRule, text []rune, normalized []normalizedRune) []Match {
|
||||
if len(normalized) == 0 || len(normalized) < len(rule.normalized) {
|
||||
return nil
|
||||
}
|
||||
out := make([]Match, 0)
|
||||
for i := 0; i <= len(normalized)-len(rule.normalized); i++ {
|
||||
ok := true
|
||||
for j := range rule.normalized {
|
||||
if normalized[i+j].value != rule.normalized[j] {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
start := normalized[i].originalIndex
|
||||
end := normalized[i+len(rule.normalized)-1].originalIndex
|
||||
if rule.rule.WholeWord && !hasWordBoundary(text, start, end) {
|
||||
continue
|
||||
}
|
||||
out = append(out, Match{
|
||||
RuleID: rule.rule.ID,
|
||||
Word: rule.rule.Word,
|
||||
Scope: rule.rule.Scope,
|
||||
Action: rule.rule.Action,
|
||||
Replacement: rule.rule.Replacement,
|
||||
Severity: rule.rule.Severity,
|
||||
Tags: append([]string(nil), rule.rule.Tags...),
|
||||
Start: start,
|
||||
End: end,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func replacementRune(value string) rune {
|
||||
for _, r := range value {
|
||||
return r
|
||||
}
|
||||
return '*'
|
||||
}
|
||||
|
||||
func normalizeRuleWord(word string, ignoreSeparators bool) []rune {
|
||||
out := make([]rune, 0, len(word))
|
||||
for _, r := range word {
|
||||
if ignoreSeparators && isSeparator(r) {
|
||||
continue
|
||||
}
|
||||
out = append(out, unicode.ToLower(r))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeText(text []rune, ignoreSeparators bool) []normalizedRune {
|
||||
out := make([]normalizedRune, 0, len(text))
|
||||
for idx, r := range text {
|
||||
if ignoreSeparators && isSeparator(r) {
|
||||
continue
|
||||
}
|
||||
out = append(out, normalizedRune{value: unicode.ToLower(r), originalIndex: idx})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func isSeparator(r rune) bool {
|
||||
return unicode.IsSpace(r) || unicode.IsPunct(r) || unicode.IsSymbol(r)
|
||||
}
|
||||
|
||||
func hasWordBoundary(text []rune, start, end int) bool {
|
||||
if start > 0 && isWordRune(text[start-1]) {
|
||||
return false
|
||||
}
|
||||
if end+1 < len(text) && isWordRune(text[end+1]) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isWordRune(r rune) bool {
|
||||
return unicode.IsLetter(r) || unicode.IsDigit(r)
|
||||
}
|
||||
|
||||
func sortMatches(matches []Match) {
|
||||
sort.Slice(matches, func(i, j int) bool {
|
||||
if matches[i].Start != matches[j].Start {
|
||||
return matches[i].Start < matches[j].Start
|
||||
}
|
||||
leftLen := matches[i].End - matches[i].Start
|
||||
rightLen := matches[j].End - matches[j].Start
|
||||
if leftLen != rightLen {
|
||||
return leftLen > rightLen
|
||||
}
|
||||
if matches[i].Severity != matches[j].Severity {
|
||||
return matches[i].Severity > matches[j].Severity
|
||||
}
|
||||
return matches[i].RuleID < matches[j].RuleID
|
||||
})
|
||||
}
|
||||
|
||||
func ruleApplies(ruleScope, requestScope string) bool {
|
||||
ruleScope = normalizeToken(ruleScope)
|
||||
requestScope = normalizeToken(requestScope)
|
||||
return ruleScope == "" || ruleScope == "global" || ruleScope == requestScope
|
||||
}
|
||||
|
||||
func normalizeToken(value string) string {
|
||||
return strings.ToLower(strings.TrimSpace(value))
|
||||
}
|
||||
|
||||
func normalizeList(values []string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
out := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
value = normalizeToken(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[value]; exists {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
out = append(out, value)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneWordRule(rule WordRule) WordRule {
|
||||
rule.Tags = append([]string(nil), rule.Tags...)
|
||||
rule.Meta = cloneStringMap(rule.Meta)
|
||||
return rule
|
||||
}
|
||||
|
||||
func cloneStringMap(in map[string]string) map[string]string {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(in))
|
||||
for key, value := range in {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Evaluator interface {
|
||||
Evaluate(context.Context, Request) (Decision, error)
|
||||
}
|
||||
|
||||
type ExternalProvider interface {
|
||||
EvaluateText(context.Context, Request, TextResult) (ProviderDecision, error)
|
||||
}
|
||||
|
||||
type ProviderDecision struct {
|
||||
Rejected bool `json:"rejected,omitempty"`
|
||||
SanitizedText string `json:"sanitized_text,omitempty"`
|
||||
Reasons []string `json:"reasons,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
CacheTTL time.Duration `json:"-"`
|
||||
}
|
||||
|
||||
type PipelineOptions struct {
|
||||
Engine *Engine
|
||||
Provider ExternalProvider
|
||||
CacheTTL time.Duration
|
||||
FailOpen bool
|
||||
ErrorReason string
|
||||
RejectReason string
|
||||
ProviderScope string
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
type Pipeline struct {
|
||||
engine *Engine
|
||||
provider ExternalProvider
|
||||
cacheTTL time.Duration
|
||||
failOpen bool
|
||||
errorReason string
|
||||
rejectReason string
|
||||
providerScope string
|
||||
now func() time.Time
|
||||
mu sync.Mutex
|
||||
cache map[string]cachedProvDecision
|
||||
}
|
||||
|
||||
type cachedProvDecision struct {
|
||||
decision ProviderDecision
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
func NewPipeline(options PipelineOptions) *Pipeline {
|
||||
engine := options.Engine
|
||||
if engine == nil {
|
||||
engine = NewEngine(nil, nil)
|
||||
}
|
||||
errorReason := normalizeToken(options.ErrorReason)
|
||||
if errorReason == "" {
|
||||
errorReason = "external_unavailable"
|
||||
}
|
||||
rejectReason := normalizeToken(options.RejectReason)
|
||||
if rejectReason == "" {
|
||||
rejectReason = "external_rejected"
|
||||
}
|
||||
now := options.Now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return &Pipeline{
|
||||
engine: engine,
|
||||
provider: options.Provider,
|
||||
cacheTTL: options.CacheTTL,
|
||||
failOpen: options.FailOpen,
|
||||
errorReason: errorReason,
|
||||
rejectReason: rejectReason,
|
||||
providerScope: normalizeToken(options.ProviderScope),
|
||||
now: now,
|
||||
cache: make(map[string]cachedProvDecision),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pipeline) Evaluate(ctx context.Context, request Request) (Decision, error) {
|
||||
if p == nil {
|
||||
return Decision{}, fmt.Errorf("%w: pipeline is nil", ErrInvalidRequest)
|
||||
}
|
||||
if p.engine == nil {
|
||||
return Decision{}, fmt.Errorf("%w: engine is required", ErrInvalidRequest)
|
||||
}
|
||||
decision, err := p.engine.Evaluate(ctx, request)
|
||||
if err != nil {
|
||||
return Decision{}, err
|
||||
}
|
||||
if p.provider == nil || !decision.Allowed || strings.TrimSpace(request.Text) == "" {
|
||||
return decision, nil
|
||||
}
|
||||
providerDecision, err := p.evaluateProvider(ctx, request, decision.Text)
|
||||
if err != nil {
|
||||
if p.failOpen {
|
||||
return decision, nil
|
||||
}
|
||||
decision.Allowed = false
|
||||
decision.Reasons = appendReasons(decision.Reasons, p.errorReason)
|
||||
return decision, nil
|
||||
}
|
||||
return applyProviderChoice(decision, providerDecision, p.rejectReason), nil
|
||||
}
|
||||
|
||||
func (p *Pipeline) ClearCache() {
|
||||
if p == nil {
|
||||
return
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.cache = make(map[string]cachedProvDecision)
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
func (p *Pipeline) Engine() *Engine {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
return p.engine
|
||||
}
|
||||
|
||||
func (p *Pipeline) evaluateProvider(ctx context.Context, request Request, local TextResult) (ProviderDecision, error) {
|
||||
key := p.cacheKey(request)
|
||||
now := p.nowUTC()
|
||||
if key != "" {
|
||||
if decision, ok := p.cachedDecision(key, now); ok {
|
||||
return decision, nil
|
||||
}
|
||||
}
|
||||
decision, err := p.provider.EvaluateText(ctx, request, local)
|
||||
if err != nil {
|
||||
return ProviderDecision{}, err
|
||||
}
|
||||
decision.Reasons = normalizeList(decision.Reasons)
|
||||
decision.Tags = normalizeList(decision.Tags)
|
||||
if key != "" {
|
||||
p.storeCachedDecision(key, decision, now)
|
||||
}
|
||||
return cloneProviderChoice(decision), nil
|
||||
}
|
||||
|
||||
func (p *Pipeline) cachedDecision(key string, now time.Time) (ProviderDecision, bool) {
|
||||
p.mu.Lock()
|
||||
item, ok := p.cache[key]
|
||||
if !ok {
|
||||
p.mu.Unlock()
|
||||
return ProviderDecision{}, false
|
||||
}
|
||||
if !item.expiresAt.IsZero() && !item.expiresAt.After(now) {
|
||||
delete(p.cache, key)
|
||||
p.mu.Unlock()
|
||||
return ProviderDecision{}, false
|
||||
}
|
||||
p.mu.Unlock()
|
||||
return cloneProviderChoice(item.decision), true
|
||||
}
|
||||
|
||||
func (p *Pipeline) storeCachedDecision(key string, decision ProviderDecision, now time.Time) {
|
||||
ttl := decision.CacheTTL
|
||||
if ttl <= 0 {
|
||||
ttl = p.cacheTTL
|
||||
}
|
||||
if ttl <= 0 {
|
||||
return
|
||||
}
|
||||
item := cachedProvDecision{
|
||||
decision: cloneProviderChoice(decision),
|
||||
expiresAt: now.Add(ttl),
|
||||
}
|
||||
p.mu.Lock()
|
||||
p.cache[key] = item
|
||||
p.mu.Unlock()
|
||||
}
|
||||
|
||||
func (p *Pipeline) cacheKey(request Request) string {
|
||||
text := strings.TrimSpace(request.Text)
|
||||
if text == "" {
|
||||
return ""
|
||||
}
|
||||
scope := normalizeToken(request.Scope)
|
||||
if p.providerScope != "" {
|
||||
scope = encodeModerationKey(p.providerScope, scope)
|
||||
}
|
||||
return encodeModerationKey(scope, text)
|
||||
}
|
||||
|
||||
func (p *Pipeline) nowUTC() time.Time {
|
||||
if p == nil || p.now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return p.now().UTC()
|
||||
}
|
||||
|
||||
func applyProviderChoice(decision Decision, provider ProviderDecision, rejectReason string) Decision {
|
||||
if provider.SanitizedText != "" {
|
||||
decision.SanitizedText = provider.SanitizedText
|
||||
decision.Text.Sanitized = provider.SanitizedText
|
||||
}
|
||||
if provider.Rejected {
|
||||
decision.Allowed = false
|
||||
reasons := provider.Reasons
|
||||
if len(reasons) == 0 {
|
||||
reasons = []string{rejectReason}
|
||||
}
|
||||
decision.Reasons = appendReasons(decision.Reasons, reasons...)
|
||||
}
|
||||
return decision
|
||||
}
|
||||
|
||||
func appendReasons(existing []string, values ...string) []string {
|
||||
if len(values) == 0 {
|
||||
return existing
|
||||
}
|
||||
seen := make(map[string]struct{}, len(existing)+len(values))
|
||||
out := make([]string, 0, len(existing)+len(values))
|
||||
for _, value := range existing {
|
||||
key := normalizeToken(value)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
out = append(out, key)
|
||||
}
|
||||
for _, value := range values {
|
||||
key := normalizeToken(value)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
seen[key] = struct{}{}
|
||||
out = append(out, key)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneProviderChoice(decision ProviderDecision) ProviderDecision {
|
||||
decision.Reasons = append([]string(nil), decision.Reasons...)
|
||||
decision.Tags = append([]string(nil), decision.Tags...)
|
||||
return decision
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/db"
|
||||
)
|
||||
|
||||
var ErrNilRedisStoreExecutor = errors.New("moderation redis executor is required")
|
||||
|
||||
const defRedisSanctionPref = "longheng:moderation"
|
||||
|
||||
const sanctionUpsertLua = `
|
||||
local old_subject_key = KEYS[4]
|
||||
redis.call("HSET", KEYS[1], ARGV[1], ARGV[2])
|
||||
redis.call("SADD", KEYS[2], ARGV[1])
|
||||
redis.call("SADD", KEYS[3], ARGV[1])
|
||||
if old_subject_key ~= "" and old_subject_key ~= KEYS[3] then
|
||||
redis.call("SREM", old_subject_key, ARGV[1])
|
||||
end
|
||||
return 1
|
||||
`
|
||||
|
||||
const sanctionRmScript = `
|
||||
local current = redis.call("HGET", KEYS[1], ARGV[1])
|
||||
if not current then
|
||||
return 0
|
||||
end
|
||||
if current ~= ARGV[2] then
|
||||
return 2
|
||||
end
|
||||
redis.call("HDEL", KEYS[1], ARGV[1])
|
||||
redis.call("SREM", KEYS[2], ARGV[1])
|
||||
redis.call("SREM", KEYS[3], ARGV[1])
|
||||
return 1
|
||||
`
|
||||
|
||||
const sanctionRmRetries = 3
|
||||
|
||||
type RedisSanctionStoreOptions struct {
|
||||
Executor db.RedisExecutor
|
||||
KeyPrefix string
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
type RedisSanctionStore struct {
|
||||
executor db.RedisExecutor
|
||||
prefix string
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewRedisSanctionStore(options RedisSanctionStoreOptions) *RedisSanctionStore {
|
||||
now := options.Now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return &RedisSanctionStore{
|
||||
executor: options.Executor,
|
||||
prefix: normSanctionPref(options.KeyPrefix),
|
||||
now: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RedisSanctionStore) Upsert(ctx context.Context, sanction Sanction) (Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
executor, prefix, now, err := s.ready()
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
item, err := normalizeSanction(sanction, now().UTC())
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
data, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
itemsKey := sanctionItemsKey(prefix)
|
||||
oldSubjectKey := sanctionSubjectKey(prefix, item.Subject)
|
||||
if existing, ok, err := redisSanctionByID(ctx, executor, itemsKey, item.ID); err != nil {
|
||||
return Sanction{}, err
|
||||
} else if ok {
|
||||
oldSubjectKey = sanctionSubjectKey(prefix, existing.Subject)
|
||||
}
|
||||
if _, err := redisDo(ctx, executor, "EVAL",
|
||||
sanctionUpsertLua,
|
||||
4,
|
||||
itemsKey,
|
||||
sanctionIndexKey(prefix),
|
||||
sanctionSubjectKey(prefix, item.Subject),
|
||||
oldSubjectKey,
|
||||
item.ID,
|
||||
data,
|
||||
); err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *RedisSanctionStore) Remove(ctx context.Context, id string) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
id = normalizeToken(id)
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id is required", ErrInvalidSanction)
|
||||
}
|
||||
executor, prefix, _, err := s.ready()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
itemsKey := sanctionItemsKey(prefix)
|
||||
for attempt := 0; attempt < sanctionRmRetries; attempt++ {
|
||||
item, raw, ok, err := redisSanctionRaw(ctx, executor, itemsKey, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ok {
|
||||
return ErrSanctionMissing
|
||||
}
|
||||
status, err := redisInt64Value(redisDo(ctx, executor, "EVAL",
|
||||
sanctionRmScript,
|
||||
3,
|
||||
itemsKey,
|
||||
sanctionIndexKey(prefix),
|
||||
sanctionSubjectKey(prefix, item.Subject),
|
||||
id,
|
||||
raw,
|
||||
))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch status {
|
||||
case 0:
|
||||
return ErrSanctionMissing
|
||||
case 1:
|
||||
return nil
|
||||
case 2:
|
||||
continue
|
||||
default:
|
||||
return fmt.Errorf("unexpected moderation redis remove status %d", status)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%w: concurrent remove conflict", ErrInvalidSanction)
|
||||
}
|
||||
|
||||
func (s *RedisSanctionStore) Active(ctx context.Context, query SanctionQuery) ([]Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
executor, prefix, now, err := s.ready()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.Subject = normalizeToken(query.Subject)
|
||||
query.Scope = normalizeToken(query.Scope)
|
||||
if query.Subject == "" {
|
||||
return nil, fmt.Errorf("%w: subject is required", ErrInvalidSanction)
|
||||
}
|
||||
if query.Now.IsZero() {
|
||||
query.Now = now().UTC()
|
||||
} else {
|
||||
query.Now = query.Now.UTC()
|
||||
}
|
||||
ids, err := redisStrings(ctx, executor, "SMEMBERS", sanctionSubjectKey(prefix, query.Subject))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kindSet := sanctionKindSet(query.Kinds)
|
||||
out := make([]Sanction, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
item, ok, err := redisSanctionByID(ctx, executor, sanctionItemsKey(prefix), id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok || item.Subject != query.Subject {
|
||||
_, _ = redisDo(ctx, executor, "SREM", sanctionSubjectKey(prefix, query.Subject), id)
|
||||
continue
|
||||
}
|
||||
if !sanctionScopeApplies(item.Scope, query.Scope) || !item.ActiveAt(query.Now) {
|
||||
continue
|
||||
}
|
||||
if len(kindSet) > 0 {
|
||||
if _, ok := kindSet[item.Kind]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
out = append(out, cloneSanction(item))
|
||||
}
|
||||
sortSanctions(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *RedisSanctionStore) Snapshot(ctx context.Context) (SanctionSnapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
executor, prefix, _, err := s.ready()
|
||||
if err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
ids, err := redisStrings(ctx, executor, "SMEMBERS", sanctionIndexKey(prefix))
|
||||
if err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
items := make([]Sanction, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
item, ok, err := redisSanctionByID(ctx, executor, sanctionItemsKey(prefix), id)
|
||||
if err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
if !ok {
|
||||
_, _ = redisDo(ctx, executor, "SREM", sanctionIndexKey(prefix), id)
|
||||
continue
|
||||
}
|
||||
items = append(items, cloneSanction(item))
|
||||
}
|
||||
sortSanctions(items)
|
||||
return SanctionSnapshot{Items: items}, nil
|
||||
}
|
||||
|
||||
func redisSanctionByID(ctx context.Context, executor db.RedisExecutor, itemsKey, id string) (Sanction, bool, error) {
|
||||
item, _, ok, err := redisSanctionRaw(ctx, executor, itemsKey, id)
|
||||
return item, ok, err
|
||||
}
|
||||
|
||||
func redisSanctionRaw(ctx context.Context, executor db.RedisExecutor, itemsKey, id string) (Sanction, []byte, bool, error) {
|
||||
value, err := redisDo(ctx, executor, "HGET", itemsKey, normalizeToken(id))
|
||||
if err != nil {
|
||||
return Sanction{}, nil, false, err
|
||||
}
|
||||
data, ok, err := redisBulkBytes(value)
|
||||
if err != nil || !ok {
|
||||
return Sanction{}, nil, ok, err
|
||||
}
|
||||
var item Sanction
|
||||
if err := json.Unmarshal(data, &item); err != nil {
|
||||
return Sanction{}, nil, false, fmt.Errorf("decode moderation redis sanction: %w", err)
|
||||
}
|
||||
return cloneSanction(item), data, true, nil
|
||||
}
|
||||
|
||||
func redisStrings(ctx context.Context, executor db.RedisExecutor, command string, args ...any) ([]string, error) {
|
||||
value, err := redisDo(ctx, executor, command, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return redisStringSlice(value)
|
||||
}
|
||||
|
||||
func redisDo(ctx context.Context, executor db.RedisExecutor, command string, args ...any) (any, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if executor == nil {
|
||||
return nil, ErrNilRedisStoreExecutor
|
||||
}
|
||||
return executor.Do(ctx, command, args...)
|
||||
}
|
||||
|
||||
func (s *RedisSanctionStore) ready() (db.RedisExecutor, string, func() time.Time, error) {
|
||||
if s == nil || s.executor == nil {
|
||||
return nil, "", nil, ErrNilRedisStoreExecutor
|
||||
}
|
||||
now := s.now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return s.executor, normSanctionPref(s.prefix), now, nil
|
||||
}
|
||||
|
||||
func normSanctionPref(prefix string) string {
|
||||
prefix = strings.Trim(strings.TrimSpace(prefix), ":")
|
||||
if prefix == "" {
|
||||
return defRedisSanctionPref
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
|
||||
func sanctionItemsKey(prefix string) string {
|
||||
return prefix + ":sanctions"
|
||||
}
|
||||
|
||||
func sanctionIndexKey(prefix string) string {
|
||||
return prefix + ":sanction_index"
|
||||
}
|
||||
|
||||
func sanctionSubjectKey(prefix, subject string) string {
|
||||
return prefix + ":subject:" + normalizeToken(subject)
|
||||
}
|
||||
|
||||
func redisStringSlice(value any) ([]string, error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return nil, nil
|
||||
case []string:
|
||||
return append([]string(nil), v...), nil
|
||||
case [][]byte:
|
||||
out := make([]string, len(v))
|
||||
for i := range v {
|
||||
out[i] = string(v[i])
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
case []any:
|
||||
out := make([]string, 0, len(v))
|
||||
for _, item := range v {
|
||||
switch x := item.(type) {
|
||||
case string:
|
||||
out = append(out, x)
|
||||
case []byte:
|
||||
out = append(out, string(x))
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected redis string item %T", item)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected redis string slice type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func redisInt64Value(value any, err error) (int64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
switch v := value.(type) {
|
||||
case int:
|
||||
return int64(v), nil
|
||||
case int64:
|
||||
return v, nil
|
||||
case uint64:
|
||||
const maxInt64 = uint64(1<<63 - 1)
|
||||
if v > maxInt64 {
|
||||
return 0, fmt.Errorf("redis integer overflows int64: %d", v)
|
||||
}
|
||||
return int64(v), nil
|
||||
case []byte:
|
||||
return strconv.ParseInt(string(v), 10, 64)
|
||||
case string:
|
||||
return strconv.ParseInt(v, 10, 64)
|
||||
default:
|
||||
return 0, fmt.Errorf("unexpected redis integer type %T", value)
|
||||
}
|
||||
}
|
||||
|
||||
func redisBulkBytes(value any) ([]byte, bool, error) {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return nil, false, nil
|
||||
case []byte:
|
||||
return append([]byte(nil), v...), true, nil
|
||||
case string:
|
||||
return []byte(v), true, nil
|
||||
default:
|
||||
return nil, false, fmt.Errorf("unexpected redis bulk type %T", value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidSanction = errors.New("moderation sanction is invalid")
|
||||
ErrSanctionMissing = errors.New("moderation sanction not found")
|
||||
)
|
||||
|
||||
type SanctionKind string
|
||||
|
||||
const (
|
||||
SanctionMute SanctionKind = "mute"
|
||||
SanctionBan SanctionKind = "ban"
|
||||
)
|
||||
|
||||
type Sanction struct {
|
||||
ID string `json:"id"`
|
||||
Subject string `json:"subject"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Kind SanctionKind `json:"kind"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Until time.Time `json:"until,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type SanctionQuery struct {
|
||||
Subject string `json:"subject"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
Kinds []SanctionKind `json:"kinds,omitempty"`
|
||||
Now time.Time `json:"now,omitempty"`
|
||||
}
|
||||
|
||||
type SanctionSnapshot struct {
|
||||
Items []Sanction `json:"items,omitempty"`
|
||||
}
|
||||
|
||||
type SanctionStore interface {
|
||||
Upsert(context.Context, Sanction) (Sanction, error)
|
||||
Remove(context.Context, string) error
|
||||
Active(context.Context, SanctionQuery) ([]Sanction, error)
|
||||
Snapshot(context.Context) (SanctionSnapshot, error)
|
||||
}
|
||||
|
||||
type MemorySanctionStore struct {
|
||||
mu sync.RWMutex
|
||||
items map[string]Sanction
|
||||
bySubject map[string]map[string]struct{}
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewMemorySanctionStore() *MemorySanctionStore {
|
||||
return &MemorySanctionStore{
|
||||
items: make(map[string]Sanction),
|
||||
bySubject: make(map[string]map[string]struct{}),
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) SetNow(now func() time.Time) {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.now = now
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) Upsert(ctx context.Context, sanction Sanction) (Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return Sanction{}, ErrSanctionMissing
|
||||
}
|
||||
item, err := normalizeSanction(sanction, s.nowUTC())
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
if s.items == nil {
|
||||
s.items = make(map[string]Sanction)
|
||||
}
|
||||
if s.bySubject == nil {
|
||||
s.bySubject = make(map[string]map[string]struct{})
|
||||
}
|
||||
if existing, ok := s.items[item.ID]; ok && existing.Subject != item.Subject {
|
||||
s.removeSubjectIdx(existing.Subject, item.ID)
|
||||
}
|
||||
s.items[item.ID] = cloneSanction(item)
|
||||
s.addSubjectIndex(item.Subject, item.ID)
|
||||
s.mu.Unlock()
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) Remove(ctx context.Context, id string) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil {
|
||||
return ErrSanctionMissing
|
||||
}
|
||||
id = normalizeToken(id)
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id is required", ErrInvalidSanction)
|
||||
}
|
||||
s.mu.Lock()
|
||||
if _, ok := s.items[id]; !ok {
|
||||
s.mu.Unlock()
|
||||
return ErrSanctionMissing
|
||||
}
|
||||
s.removeSubjectIdx(s.items[id].Subject, id)
|
||||
delete(s.items, id)
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) Active(ctx context.Context, query SanctionQuery) ([]Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil {
|
||||
return nil, nil
|
||||
}
|
||||
query.Subject = normalizeToken(query.Subject)
|
||||
query.Scope = normalizeToken(query.Scope)
|
||||
if query.Now.IsZero() {
|
||||
query.Now = s.nowUTC()
|
||||
} else {
|
||||
query.Now = query.Now.UTC()
|
||||
}
|
||||
kindSet := sanctionKindSet(query.Kinds)
|
||||
|
||||
s.mu.RLock()
|
||||
out := make([]Sanction, 0)
|
||||
ids := make([]string, 0, len(s.bySubject[query.Subject]))
|
||||
for id := range s.bySubject[query.Subject] {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
for _, id := range ids {
|
||||
item, ok := s.items[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if !sanctionScopeApplies(item.Scope, query.Scope) {
|
||||
continue
|
||||
}
|
||||
if len(kindSet) > 0 {
|
||||
if _, ok := kindSet[item.Kind]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !item.ActiveAt(query.Now) {
|
||||
continue
|
||||
}
|
||||
out = append(out, cloneSanction(item))
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
sortSanctions(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) Snapshot(ctx context.Context) (SanctionSnapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return SanctionSnapshot{}, nil
|
||||
}
|
||||
s.mu.RLock()
|
||||
items := make([]Sanction, 0, len(s.items))
|
||||
for _, item := range s.items {
|
||||
items = append(items, cloneSanction(item))
|
||||
}
|
||||
s.mu.RUnlock()
|
||||
sortSanctions(items)
|
||||
return SanctionSnapshot{Items: items}, nil
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) addSubjectIndex(subject, id string) {
|
||||
if s.bySubject[subject] == nil {
|
||||
s.bySubject[subject] = make(map[string]struct{})
|
||||
}
|
||||
s.bySubject[subject][id] = struct{}{}
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) removeSubjectIdx(subject, id string) {
|
||||
if ids := s.bySubject[subject]; ids != nil {
|
||||
delete(ids, id)
|
||||
if len(ids) == 0 {
|
||||
delete(s.bySubject, subject)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s Sanction) ActiveAt(now time.Time) bool {
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
} else {
|
||||
now = now.UTC()
|
||||
}
|
||||
return s.Until.IsZero() || s.Until.After(now)
|
||||
}
|
||||
|
||||
func normalizeSanction(sanction Sanction, now time.Time) (Sanction, error) {
|
||||
sanction.ID = normalizeToken(sanction.ID)
|
||||
sanction.Subject = normalizeToken(sanction.Subject)
|
||||
sanction.Scope = normalizeToken(sanction.Scope)
|
||||
sanction.Reason = strings.TrimSpace(sanction.Reason)
|
||||
sanction.Source = strings.TrimSpace(sanction.Source)
|
||||
sanction.Meta = cloneStringMap(sanction.Meta)
|
||||
if sanction.Subject == "" {
|
||||
return Sanction{}, fmt.Errorf("%w: subject is required", ErrInvalidSanction)
|
||||
}
|
||||
if sanction.Kind != SanctionMute && sanction.Kind != SanctionBan {
|
||||
return Sanction{}, fmt.Errorf("%w: invalid kind %q", ErrInvalidSanction, sanction.Kind)
|
||||
}
|
||||
if sanction.ID == "" {
|
||||
if hasDefaultIDDelim(sanction.Subject) || hasDefaultIDDelim(sanction.Scope) {
|
||||
return Sanction{}, fmt.Errorf("%w: subject or scope contains reserved delimiter", ErrInvalidSanction)
|
||||
}
|
||||
sanction.ID = sanction.Subject + ":" + string(sanction.Kind) + ":" + sanction.Scope
|
||||
}
|
||||
if sanction.CreatedAt.IsZero() {
|
||||
sanction.CreatedAt = now.UTC()
|
||||
} else {
|
||||
sanction.CreatedAt = sanction.CreatedAt.UTC()
|
||||
}
|
||||
if !sanction.Until.IsZero() {
|
||||
sanction.Until = sanction.Until.UTC()
|
||||
}
|
||||
return sanction, nil
|
||||
}
|
||||
|
||||
func sanctionKindSet(kinds []SanctionKind) map[SanctionKind]struct{} {
|
||||
if len(kinds) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[SanctionKind]struct{}, len(kinds))
|
||||
for _, kind := range kinds {
|
||||
if kind == "" {
|
||||
continue
|
||||
}
|
||||
out[kind] = struct{}{}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func sanctionScopeApplies(sanctionScope, requestScope string) bool {
|
||||
sanctionScope = normalizeToken(sanctionScope)
|
||||
requestScope = normalizeToken(requestScope)
|
||||
return sanctionScope == "" || sanctionScope == "global" || sanctionScope == requestScope
|
||||
}
|
||||
|
||||
// hasDefaultIDDelim 拦截会让默认处罚 ID 产生歧义的保留分隔符。
|
||||
func hasDefaultIDDelim(value string) bool {
|
||||
return strings.Contains(value, ":") || strings.ContainsRune(value, '\x00')
|
||||
}
|
||||
|
||||
// encodeModerationKey 用长度前缀拼接多段键,避免字段内容自带分隔符时发生碰撞。
|
||||
func encodeModerationKey(parts ...string) string {
|
||||
var builder strings.Builder
|
||||
for _, part := range parts {
|
||||
builder.WriteString(strconv.Itoa(len(part)))
|
||||
builder.WriteByte(':')
|
||||
builder.WriteString(part)
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func sortSanctions(items []Sanction) {
|
||||
sort.Slice(items, func(i, j int) bool {
|
||||
if items[i].Subject != items[j].Subject {
|
||||
return items[i].Subject < items[j].Subject
|
||||
}
|
||||
if items[i].Scope != items[j].Scope {
|
||||
return items[i].Scope < items[j].Scope
|
||||
}
|
||||
if items[i].Kind != items[j].Kind {
|
||||
return items[i].Kind < items[j].Kind
|
||||
}
|
||||
return items[i].ID < items[j].ID
|
||||
})
|
||||
}
|
||||
|
||||
func cloneSanction(sanction Sanction) Sanction {
|
||||
sanction.Meta = cloneStringMap(sanction.Meta)
|
||||
return sanction
|
||||
}
|
||||
|
||||
func (s *MemorySanctionStore) nowUTC() time.Time {
|
||||
s.mu.RLock()
|
||||
now := s.now
|
||||
s.mu.RUnlock()
|
||||
if now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return now().UTC()
|
||||
}
|
||||
|
||||
func ctxErr(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
package moderation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNilSQLSanctionDB = errors.New("moderation sql db is required")
|
||||
ErrInvalidSanctionTable = errors.New("moderation sanction table name is invalid")
|
||||
ErrUnsupportedSQLSanction = errors.New("moderation sql operation is unsupported")
|
||||
ErrSQLSanctionRows = errors.New("moderation sql rows are required")
|
||||
)
|
||||
|
||||
const defaultSanctionTable = "moderation_sanctions"
|
||||
|
||||
type SQLSanctionRows interface {
|
||||
Next() bool
|
||||
Scan(...any) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
type SQLSanctionDB interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
QueryContext(context.Context, string, ...any) (SQLSanctionRows, error)
|
||||
QueryRowContext(context.Context, string, ...any) SQLSanctionRow
|
||||
}
|
||||
|
||||
type SQLSanctionRow interface {
|
||||
Scan(...any) error
|
||||
}
|
||||
|
||||
type SQLSanctionDBAdapter struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
func (a SQLSanctionDBAdapter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
if a.DB == nil {
|
||||
return nil, ErrNilSQLSanctionDB
|
||||
}
|
||||
return a.DB.ExecContext(ctx, query, args...) // #nosec G701 -- 适配层只执行本包已构造的 SQL,业务值仍由调用方参数绑定。
|
||||
}
|
||||
|
||||
func (a SQLSanctionDBAdapter) QueryContext(ctx context.Context, query string, args ...any) (SQLSanctionRows, error) {
|
||||
if a.DB == nil {
|
||||
return nil, ErrNilSQLSanctionDB
|
||||
}
|
||||
// #nosec G701 -- SQL 由本包白名单表名构造,业务值仍由调用方参数绑定。
|
||||
return a.DB.QueryContext(ctx, query, args...) //nolint:rowserrcheck // 适配层只返回 rows,调用方负责遍历结束后的 Err 检查。
|
||||
}
|
||||
|
||||
func (a SQLSanctionDBAdapter) QueryRowContext(ctx context.Context, query string, args ...any) SQLSanctionRow {
|
||||
if a.DB == nil {
|
||||
return errSQLRow{err: ErrNilSQLSanctionDB}
|
||||
}
|
||||
return a.DB.QueryRowContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
type SQLSanctionStoreOptions struct {
|
||||
Table string
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
type SQLSanctionStore struct {
|
||||
db SQLSanctionDB
|
||||
table string
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewSQLSanctionStore(db SQLSanctionDB, options SQLSanctionStoreOptions) (*SQLSanctionStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNilSQLSanctionDB
|
||||
}
|
||||
table, err := normSanctionTable(options.Table)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
now := options.Now
|
||||
if now == nil {
|
||||
now = time.Now
|
||||
}
|
||||
return &SQLSanctionStore{db: db, table: table, now: now}, nil
|
||||
}
|
||||
|
||||
func NewSQLSanctionStoreFromDB(db *sql.DB, options SQLSanctionStoreOptions) (*SQLSanctionStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNilSQLSanctionDB
|
||||
}
|
||||
return NewSQLSanctionStore(SQLSanctionDBAdapter{DB: db}, options)
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) EnsureSchema(ctx context.Context) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNilSQLSanctionDB
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx, s.Schema())
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) Schema() string {
|
||||
table := quoteSanctionID(s.table)
|
||||
return fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
id VARCHAR(191) NOT NULL,
|
||||
subject VARCHAR(191) NOT NULL,
|
||||
scope VARCHAR(64) NOT NULL DEFAULT '',
|
||||
kind VARCHAR(32) NOT NULL,
|
||||
reason VARCHAR(512) NOT NULL DEFAULT '',
|
||||
source VARCHAR(128) NOT NULL DEFAULT '',
|
||||
until_at DATETIME(6) NULL,
|
||||
created_at DATETIME(6) NOT NULL,
|
||||
sanction_json JSON NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_moderation_sanctions_subject_scope (subject, scope, kind),
|
||||
KEY idx_moderation_sanctions_until (until_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, table)
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) Upsert(ctx context.Context, sanction Sanction) (Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return Sanction{}, ErrNilSQLSanctionDB
|
||||
}
|
||||
item, err := normalizeSanction(sanction, s.nowUTC())
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
data, err := json.Marshal(item)
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
until := nullableTime(item.Until)
|
||||
_, err = s.db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteSanctionID(s.table)+" (id, subject, scope, kind, reason, source, until_at, created_at, sanction_json) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE subject=VALUES(subject), scope=VALUES(scope), kind=VALUES(kind), reason=VALUES(reason), source=VALUES(source), until_at=VALUES(until_at), sanction_json=VALUES(sanction_json)",
|
||||
item.ID,
|
||||
item.Subject,
|
||||
item.Scope,
|
||||
string(item.Kind),
|
||||
item.Reason,
|
||||
item.Source,
|
||||
until,
|
||||
item.CreatedAt,
|
||||
string(data),
|
||||
)
|
||||
if err != nil {
|
||||
return Sanction{}, err
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) Remove(ctx context.Context, id string) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNilSQLSanctionDB
|
||||
}
|
||||
id = normalizeToken(id)
|
||||
if id == "" {
|
||||
return fmt.Errorf("%w: id is required", ErrInvalidSanction)
|
||||
}
|
||||
result, err := s.db.ExecContext(ctx, "DELETE FROM "+quoteSanctionID(s.table)+" WHERE id = ?", id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result != nil {
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrSanctionMissing
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) Active(ctx context.Context, query SanctionQuery) ([]Sanction, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return nil, ErrNilSQLSanctionDB
|
||||
}
|
||||
query.Subject = normalizeToken(query.Subject)
|
||||
query.Scope = normalizeToken(query.Scope)
|
||||
if query.Subject == "" {
|
||||
return nil, fmt.Errorf("%w: subject is required", ErrInvalidSanction)
|
||||
}
|
||||
if query.Now.IsZero() {
|
||||
query.Now = s.nowUTC()
|
||||
} else {
|
||||
query.Now = query.Now.UTC()
|
||||
}
|
||||
sqlText := "SELECT sanction_json FROM " + quoteSanctionID(s.table) + " WHERE subject = ? AND (scope = '' OR scope = 'global' OR scope = ?) AND (until_at IS NULL OR until_at > ?)"
|
||||
args := []any{query.Subject, query.Scope, query.Now}
|
||||
if len(query.Kinds) > 0 {
|
||||
placeholders := make([]string, 0, len(query.Kinds))
|
||||
for _, kind := range query.Kinds {
|
||||
if kind == "" {
|
||||
continue
|
||||
}
|
||||
placeholders = append(placeholders, "?")
|
||||
args = append(args, string(kind))
|
||||
}
|
||||
if len(placeholders) > 0 {
|
||||
sqlText += " AND kind IN (" + strings.Join(placeholders, ",") + ")"
|
||||
}
|
||||
}
|
||||
sqlText += " ORDER BY subject, scope, kind, id"
|
||||
rows, err := s.db.QueryContext(ctx, sqlText, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if rows == nil {
|
||||
return nil, ErrSQLSanctionRows
|
||||
}
|
||||
items, err := scanSanctionRows(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Sanction, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item.ActiveAt(query.Now) {
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
sortSanctions(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) Snapshot(ctx context.Context) (SanctionSnapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
if s == nil || s.db == nil {
|
||||
return SanctionSnapshot{}, ErrNilSQLSanctionDB
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, "SELECT sanction_json FROM "+quoteSanctionID(s.table)+" ORDER BY subject, scope, kind, id")
|
||||
if err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
if rows == nil {
|
||||
return SanctionSnapshot{}, ErrSQLSanctionRows
|
||||
}
|
||||
items, err := scanSanctionRows(rows)
|
||||
if err != nil {
|
||||
return SanctionSnapshot{}, err
|
||||
}
|
||||
sortSanctions(items)
|
||||
return SanctionSnapshot{Items: items}, nil
|
||||
}
|
||||
|
||||
func scanSanctionRows(rows SQLSanctionRows) (items []Sanction, err error) {
|
||||
if rows == nil {
|
||||
return nil, ErrSQLSanctionRows
|
||||
}
|
||||
defer closeSanctionRowsErr(rows, &err)
|
||||
items = make([]Sanction, 0)
|
||||
for rows.Next() {
|
||||
var data []byte
|
||||
if err := rows.Scan(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var item Sanction
|
||||
if err := json.Unmarshal(data, &item); err != nil {
|
||||
return nil, fmt.Errorf("decode moderation sql sanction: %w", err)
|
||||
}
|
||||
items = append(items, cloneSanction(item))
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func closeSanctionRowsErr(rows SQLSanctionRows, err *error) {
|
||||
if rows == nil || err == nil {
|
||||
return
|
||||
}
|
||||
if closeErr := rows.Close(); closeErr != nil && *err == nil {
|
||||
*err = closeErr
|
||||
}
|
||||
}
|
||||
|
||||
func normSanctionTable(table string) (string, error) {
|
||||
table = strings.TrimSpace(table)
|
||||
if table == "" {
|
||||
table = defaultSanctionTable
|
||||
}
|
||||
if !sanctionTablePattern.MatchString(table) {
|
||||
return "", ErrInvalidSanctionTable
|
||||
}
|
||||
return table, nil
|
||||
}
|
||||
|
||||
func quoteSanctionID(identifier string) string {
|
||||
return "`" + identifier + "`"
|
||||
}
|
||||
|
||||
func nullableTime(value time.Time) any {
|
||||
if value.IsZero() {
|
||||
return nil
|
||||
}
|
||||
return value.UTC()
|
||||
}
|
||||
|
||||
func (s *SQLSanctionStore) nowUTC() time.Time {
|
||||
if s == nil || s.now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return s.now().UTC()
|
||||
}
|
||||
|
||||
type errSQLRow struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (r errSQLRow) Scan(...any) error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
var sanctionTablePattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
@@ -0,0 +1,406 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/admincmd"
|
||||
"longheng.io/server/internal/platform/httpx"
|
||||
)
|
||||
|
||||
var ErrAdminMuxRequired = errors.New("notice admin mux is required")
|
||||
|
||||
const maxNoticeJSONBytes = 1 << 20
|
||||
|
||||
type AdminOptions struct {
|
||||
Prefix string
|
||||
Wrap func(http.HandlerFunc) http.HandlerFunc
|
||||
MutateWrap func(string, http.HandlerFunc) http.HandlerFunc
|
||||
CommandHooks AdminCommandHooks
|
||||
}
|
||||
|
||||
type AdminCommandHooks struct {
|
||||
Submit func(*http.Request, string, string, string, any) (admincmd.Receipt, bool, bool, error)
|
||||
MarkSucceeded func(context.Context, admincmd.Receipt) admincmd.Receipt
|
||||
MarkFailed func(context.Context, admincmd.Receipt, error)
|
||||
WriteError func(http.ResponseWriter, error) bool
|
||||
WriteDuplicate func(http.ResponseWriter, admincmd.Receipt)
|
||||
WriteJSONWithReceipt func(http.ResponseWriter, int, map[string]any, admincmd.Receipt)
|
||||
}
|
||||
|
||||
func RegisterAdminRoutes(mux *http.ServeMux, service Service, options AdminOptions) error {
|
||||
if mux == nil {
|
||||
return ErrAdminMuxRequired
|
||||
}
|
||||
prefix := normalizeAdminPrefix(options.Prefix)
|
||||
wrap := options.Wrap
|
||||
if wrap == nil {
|
||||
wrap = func(next http.HandlerFunc) http.HandlerFunc { return next }
|
||||
}
|
||||
mutateWrap := options.MutateWrap
|
||||
if mutateWrap == nil {
|
||||
mutateWrap = func(_ string, next http.HandlerFunc) http.HandlerFunc {
|
||||
return wrap(next)
|
||||
}
|
||||
}
|
||||
mux.HandleFunc(prefix, wrap(SnapshotHandler(service)))
|
||||
mux.HandleFunc(prefix+"/snapshot", wrap(SnapshotHandler(service)))
|
||||
mux.HandleFunc(prefix+"/publish", mutateWrap(httpx.AdminOperationID(http.MethodPost, prefix+"/publish"), PublishHandlerWithCommands(service, options.CommandHooks)))
|
||||
mux.HandleFunc(prefix+"/acknowledge", mutateWrap(httpx.AdminOperationID(http.MethodPost, prefix+"/acknowledge"), AckHandlerCommands(service, options.CommandHooks)))
|
||||
mux.HandleFunc(prefix+"/delete", mutateWrap(httpx.AdminOperationID(http.MethodPost, prefix+"/delete"), DeleteHandlerWithCommands(service, options.CommandHooks)))
|
||||
mux.HandleFunc(prefix+"/account", wrap(AccountHandler(service)))
|
||||
mux.HandleFunc(prefix+"/announcements", wrap(AnnouncementsHandler(service)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func SnapshotHandler(service Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeMethodNA(w, http.MethodGet)
|
||||
return
|
||||
}
|
||||
snapshot, err := service.Snapshot(r.Context())
|
||||
if err != nil {
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func PublishHandler(service Service) http.HandlerFunc {
|
||||
return PublishHandlerWithCommands(service, AdminCommandHooks{})
|
||||
}
|
||||
|
||||
func PublishHandlerWithCommands(service Service, commands AdminCommandHooks) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeMethodNA(w, http.MethodPost)
|
||||
return
|
||||
}
|
||||
var request PublishRequest
|
||||
if err := decodeNoticeJSON(w, r, &request); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
receipt, duplicate, submitted, err := submitNoticeCmd(commands, r, "logic.notice.publish", firstNonEmpty(request.Notice.ID, "notice"), "notice publish", request)
|
||||
if writeNoticeCmdErr(commands, w, err) {
|
||||
return
|
||||
}
|
||||
if submitted && duplicate {
|
||||
writeCmdDuplicate(commands, w, receipt)
|
||||
return
|
||||
}
|
||||
if submitted {
|
||||
request.AdminCommand = nil
|
||||
}
|
||||
result, err := service.Publish(r.Context(), request)
|
||||
if err != nil {
|
||||
if submitted {
|
||||
markNoticeCmdFail(commands, r.Context(), receipt, err)
|
||||
}
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
if submitted {
|
||||
receipt = markNoticeCmdOK(commands, r.Context(), receipt)
|
||||
writeNoticeReceipt(commands, w, http.StatusOK, noticeResponseMap(result), receipt)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
func AcknowledgeHandler(service Service) http.HandlerFunc {
|
||||
return AckHandlerCommands(service, AdminCommandHooks{})
|
||||
}
|
||||
|
||||
func AckHandlerCommands(service Service, commands AdminCommandHooks) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeMethodNA(w, http.MethodPost)
|
||||
return
|
||||
}
|
||||
var request AcknowledgeRequest
|
||||
if err := decodeNoticeJSON(w, r, &request); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
target := request.DeliveryID
|
||||
if target == "" {
|
||||
target = strings.TrimSpace(request.NoticeID + ":" + request.AccountID)
|
||||
}
|
||||
receipt, duplicateCommand, submitted, err := submitNoticeCmd(commands, r, "logic.notice.acknowledge", target, "notice acknowledge", request)
|
||||
if writeNoticeCmdErr(commands, w, err) {
|
||||
return
|
||||
}
|
||||
if submitted && duplicateCommand {
|
||||
writeCmdDuplicate(commands, w, receipt)
|
||||
return
|
||||
}
|
||||
delivery, duplicate, err := service.Acknowledge(r.Context(), request)
|
||||
if err != nil {
|
||||
if submitted {
|
||||
markNoticeCmdFail(commands, r.Context(), receipt, err)
|
||||
}
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
response := map[string]any{
|
||||
"delivery": delivery,
|
||||
"duplicate": duplicate,
|
||||
}
|
||||
if submitted {
|
||||
receipt = markNoticeCmdOK(commands, r.Context(), receipt)
|
||||
writeNoticeReceipt(commands, w, http.StatusOK, response, receipt)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, response)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteHandler(service Service) http.HandlerFunc {
|
||||
return DeleteHandlerWithCommands(service, AdminCommandHooks{})
|
||||
}
|
||||
|
||||
func DeleteHandlerWithCommands(service Service, commands AdminCommandHooks) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeMethodNA(w, http.MethodPost)
|
||||
return
|
||||
}
|
||||
var request DeleteRequest
|
||||
if err := decodeNoticeJSON(w, r, &request); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
target := strings.Join(request.DeliveryIDs, ",")
|
||||
if target == "" {
|
||||
target = strings.TrimSpace(request.NoticeID + ":" + request.AccountID)
|
||||
}
|
||||
receipt, duplicateCommand, submitted, err := submitNoticeCmd(commands, r, "logic.notice.delete", target, "notice delete", request)
|
||||
if writeNoticeCmdErr(commands, w, err) {
|
||||
return
|
||||
}
|
||||
if submitted && duplicateCommand {
|
||||
writeCmdDuplicate(commands, w, receipt)
|
||||
return
|
||||
}
|
||||
result, err := service.Delete(r.Context(), request)
|
||||
if err != nil {
|
||||
if submitted {
|
||||
markNoticeCmdFail(commands, r.Context(), receipt, err)
|
||||
}
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
response := map[string]any{"result": result}
|
||||
if submitted {
|
||||
receipt = markNoticeCmdOK(commands, r.Context(), receipt)
|
||||
writeNoticeReceipt(commands, w, http.StatusOK, response, receipt)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, response)
|
||||
}
|
||||
}
|
||||
|
||||
func submitNoticeCmd(commands AdminCommandHooks, r *http.Request, operation, target, reason string, params any) (admincmd.Receipt, bool, bool, error) {
|
||||
if commands.Submit == nil {
|
||||
return admincmd.Receipt{}, false, false, nil
|
||||
}
|
||||
return commands.Submit(r, operation, target, reason, params)
|
||||
}
|
||||
|
||||
// decodeNoticeJSON 统一限制管理端写请求的大小、字段和单文档格式。
|
||||
func decodeNoticeJSON(w http.ResponseWriter, r *http.Request, out any) error {
|
||||
return httpx.DecodeStrictJSON(w, r, maxNoticeJSONBytes, out)
|
||||
}
|
||||
|
||||
func markNoticeCmdOK(commands AdminCommandHooks, ctx context.Context, receipt admincmd.Receipt) admincmd.Receipt {
|
||||
if commands.MarkSucceeded == nil || receipt.ID == "" {
|
||||
return receipt
|
||||
}
|
||||
return commands.MarkSucceeded(ctx, receipt)
|
||||
}
|
||||
|
||||
func markNoticeCmdFail(commands AdminCommandHooks, ctx context.Context, receipt admincmd.Receipt, err error) {
|
||||
if commands.MarkFailed == nil || receipt.ID == "" || err == nil {
|
||||
return
|
||||
}
|
||||
commands.MarkFailed(ctx, receipt, err)
|
||||
}
|
||||
|
||||
func writeNoticeCmdErr(commands AdminCommandHooks, w http.ResponseWriter, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if commands.WriteError != nil {
|
||||
return commands.WriteError(w, err)
|
||||
}
|
||||
writeNoticeError(w, err)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeCmdDuplicate(commands AdminCommandHooks, w http.ResponseWriter, receipt admincmd.Receipt) {
|
||||
if commands.WriteDuplicate != nil {
|
||||
commands.WriteDuplicate(w, receipt)
|
||||
return
|
||||
}
|
||||
if receipt.ID != "" {
|
||||
w.Header().Set(httpx.AdminReceiptHeader, receipt.ID)
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{
|
||||
"status": "duplicate",
|
||||
"admin_receipt": receipt,
|
||||
})
|
||||
}
|
||||
|
||||
func writeNoticeReceipt(commands AdminCommandHooks, w http.ResponseWriter, status int, response map[string]any, receipt admincmd.Receipt) {
|
||||
if commands.WriteJSONWithReceipt != nil {
|
||||
commands.WriteJSONWithReceipt(w, status, response, receipt)
|
||||
return
|
||||
}
|
||||
if response == nil {
|
||||
response = make(map[string]any)
|
||||
}
|
||||
if receipt.ID != "" {
|
||||
w.Header().Set(httpx.AdminReceiptHeader, receipt.ID)
|
||||
response["admin_receipt"] = receipt
|
||||
}
|
||||
httpx.WriteJSON(w, status, response)
|
||||
}
|
||||
|
||||
func noticeResponseMap(value any) map[string]any {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return map[string]any{"marshal_error": err.Error()}
|
||||
}
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(data, &out); err != nil {
|
||||
return map[string]any{"marshal_error": err.Error()}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func AccountHandler(service Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeMethodNA(w, http.MethodGet)
|
||||
return
|
||||
}
|
||||
query := Query{
|
||||
AccountID: r.URL.Query().Get("account_id"),
|
||||
ShardID: r.URL.Query().Get("shard_id"),
|
||||
Cursor: r.URL.Query().Get("cursor"),
|
||||
}
|
||||
if limit, err := parseQueryInt(r.URL.Query().Get("limit")); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
} else {
|
||||
query.Limit = limit
|
||||
}
|
||||
now, err := parseQueryTime(r.URL.Query().Get("now"))
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
query.Now = now
|
||||
result, err := service.ListPageForAccount(r.Context(), query)
|
||||
if err != nil {
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
func AnnouncementsHandler(service Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeMethodNA(w, http.MethodGet)
|
||||
return
|
||||
}
|
||||
query := Query{
|
||||
ShardID: r.URL.Query().Get("shard_id"),
|
||||
}
|
||||
now, err := parseQueryTime(r.URL.Query().Get("now"))
|
||||
if err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
query.Now = now
|
||||
items, err := service.ActiveAnnouncements(r.Context(), query)
|
||||
if err != nil {
|
||||
writeNoticeError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{"items": items})
|
||||
}
|
||||
}
|
||||
|
||||
func writeNoticeError(w http.ResponseWriter, err error) {
|
||||
status := http.StatusBadRequest
|
||||
if errors.Is(err, ErrStoreRequired) {
|
||||
status = http.StatusServiceUnavailable
|
||||
}
|
||||
if errors.Is(err, ErrNoticeNotFound) || errors.Is(err, ErrDeliveryNotFound) {
|
||||
status = http.StatusNotFound
|
||||
}
|
||||
if errors.Is(err, ErrIdempotencyConflict) {
|
||||
status = http.StatusConflict
|
||||
}
|
||||
httpx.WriteJSON(w, status, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
func writeMethodNA(w http.ResponseWriter, methods ...string) {
|
||||
w.Header().Set("Allow", strings.Join(methods, ", "))
|
||||
httpx.WriteJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
}
|
||||
|
||||
func parseQueryTime(value string) (time.Time, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
parsed, err := time.Parse(time.RFC3339, value)
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("invalid now query time: %w", err)
|
||||
}
|
||||
return parsed.UTC(), nil
|
||||
}
|
||||
|
||||
func parseQueryInt(value string) (int, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return 0, nil
|
||||
}
|
||||
out, err := strconv.Atoi(value)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("invalid limit query value: %w", err)
|
||||
}
|
||||
if out < 0 {
|
||||
return 0, fmt.Errorf("invalid limit query value: %d", out)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func normalizeAdminPrefix(prefix string) string {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
if prefix == "" {
|
||||
return "/debug/notice"
|
||||
}
|
||||
if !strings.HasPrefix(prefix, "/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
prefix = strings.TrimRight(prefix, "/")
|
||||
if prefix == "" {
|
||||
return "/debug/notice"
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
liveQueueMax = 4096
|
||||
liveFlushTimeout = 3 * time.Second
|
||||
liveRetryInterval = 200 * time.Millisecond
|
||||
)
|
||||
|
||||
var (
|
||||
errLiveClosed = errors.New("notice live publisher queue is closed")
|
||||
errLiveFull = errors.New("notice live publisher queue is full")
|
||||
)
|
||||
|
||||
// AsyncLivePublisher 把公告在线推送移出发布热路径。
|
||||
// Store 和 EventLog 仍由 Service.Publish 同步完成,后台只处理可丢弃的 live fanout。
|
||||
type AsyncLivePublisher struct {
|
||||
next LivePublisher
|
||||
max int
|
||||
timeout time.Duration
|
||||
|
||||
mu sync.Mutex
|
||||
pending []PublishResult
|
||||
closed bool
|
||||
flushing bool
|
||||
flushDone chan struct{}
|
||||
errs uint64
|
||||
|
||||
wake chan struct{}
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewAsyncLivePublisher 创建异步 live publisher。
|
||||
func NewAsyncLivePublisher(next LivePublisher) *AsyncLivePublisher {
|
||||
publisher := &AsyncLivePublisher{
|
||||
next: next,
|
||||
max: liveQueueMax,
|
||||
timeout: liveFlushTimeout,
|
||||
wake: make(chan struct{}, 1),
|
||||
stop: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go publisher.run()
|
||||
return publisher
|
||||
}
|
||||
|
||||
// WrapAsyncLivePublisher 按需把同步 live publisher 包成异步队列。
|
||||
func WrapAsyncLivePublisher(publisher LivePublisher) LivePublisher {
|
||||
switch publisher.(type) {
|
||||
case nil, *AsyncLivePublisher:
|
||||
return publisher
|
||||
default:
|
||||
return NewAsyncLivePublisher(publisher)
|
||||
}
|
||||
}
|
||||
|
||||
// PublishNotice 只做入队,后台 worker 负责真正推送。
|
||||
func (p *AsyncLivePublisher) PublishNotice(ctx context.Context, result PublishResult) error {
|
||||
if p == nil || p.next == nil {
|
||||
return ErrLivePublisherMissing
|
||||
}
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
result = clonePublishResult(result)
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.closed {
|
||||
return errLiveClosed
|
||||
}
|
||||
if len(p.pending) >= p.max {
|
||||
atomic.AddUint64(&p.errs, 1)
|
||||
return errLiveFull
|
||||
}
|
||||
p.pending = append(p.pending, result)
|
||||
p.wakeLocked()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Flush 同步排空当前待推送队列,主要给测试、管理面和服务停止使用。
|
||||
func (p *AsyncLivePublisher) Flush(ctx context.Context) error {
|
||||
if p == nil || p.next == nil {
|
||||
return ErrLivePublisherMissing
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
for {
|
||||
batch, wait := p.beginFlush()
|
||||
if wait != nil {
|
||||
if err := waitLiveFlush(ctx, wait); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
if len(batch) == 0 {
|
||||
return nil
|
||||
}
|
||||
if err := p.writeBatch(ctx, batch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close 停止后台 worker,并在返回前尽量排空 live 队列。
|
||||
func (p *AsyncLivePublisher) Close(ctx context.Context) error {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
closeCtx, cancel := p.closeContext(ctx)
|
||||
defer cancel()
|
||||
p.once.Do(func() {
|
||||
p.mu.Lock()
|
||||
p.closed = true
|
||||
p.mu.Unlock()
|
||||
close(p.stop)
|
||||
})
|
||||
select {
|
||||
case <-p.done:
|
||||
case <-closeCtx.Done():
|
||||
return closeCtx.Err()
|
||||
}
|
||||
return errors.Join(p.Flush(closeCtx), closeLivePublisher(closeCtx, p.next))
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) closeContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if _, ok := ctx.Deadline(); ok {
|
||||
return ctx, func() {}
|
||||
}
|
||||
timeout := liveFlushTimeout
|
||||
if p.timeout > 0 {
|
||||
timeout = p.timeout
|
||||
}
|
||||
return context.WithTimeout(ctx, timeout)
|
||||
}
|
||||
|
||||
// Errors 返回后台 live fanout 失败次数。
|
||||
func (p *AsyncLivePublisher) Errors() uint64 {
|
||||
if p == nil {
|
||||
return 0
|
||||
}
|
||||
return atomic.LoadUint64(&p.errs)
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) run() {
|
||||
defer close(p.done)
|
||||
retry := time.NewTimer(time.Hour)
|
||||
stopLiveTimer(retry)
|
||||
var retryC <-chan time.Time
|
||||
defer stopLiveTimer(retry)
|
||||
for {
|
||||
select {
|
||||
case <-p.wake:
|
||||
if err := p.flushBackground(); err != nil {
|
||||
resetLiveTimer(retry, liveRetryInterval)
|
||||
retryC = retry.C
|
||||
} else {
|
||||
retryC = nil
|
||||
}
|
||||
case <-retryC:
|
||||
retryC = nil
|
||||
if err := p.flushBackground(); err != nil {
|
||||
resetLiveTimer(retry, liveRetryInterval)
|
||||
retryC = retry.C
|
||||
}
|
||||
case <-p.stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) flushBackground() error {
|
||||
timeout := p.timeout
|
||||
if timeout <= 0 {
|
||||
timeout = liveFlushTimeout
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
err := p.Flush(ctx)
|
||||
cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) beginFlush() ([]PublishResult, <-chan struct{}) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.flushing {
|
||||
return nil, p.flushDone
|
||||
}
|
||||
if len(p.pending) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
batch := clonePublishResults(p.pending)
|
||||
p.pending = nil
|
||||
p.flushing = true
|
||||
p.flushDone = make(chan struct{})
|
||||
return batch, nil
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) writeBatch(ctx context.Context, batch []PublishResult) error {
|
||||
for idx, result := range batch {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
p.finishFlush(batch[idx:], err)
|
||||
return err
|
||||
}
|
||||
if err := p.next.PublishNotice(ctx, result); err != nil {
|
||||
atomic.AddUint64(&p.errs, 1)
|
||||
p.finishFlush(batch[idx:], err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
p.finishFlush(nil, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) finishFlush(failed []PublishResult, _ error) {
|
||||
p.mu.Lock()
|
||||
if len(failed) > 0 {
|
||||
restored := clonePublishResults(failed)
|
||||
restored = append(restored, p.pending...)
|
||||
p.pending = restored
|
||||
}
|
||||
done := p.flushDone
|
||||
p.flushing = false
|
||||
p.flushDone = nil
|
||||
p.mu.Unlock()
|
||||
if done != nil {
|
||||
close(done)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *AsyncLivePublisher) wakeLocked() {
|
||||
select {
|
||||
case p.wake <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func clonePublishResults(in []PublishResult) []PublishResult {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]PublishResult, len(in))
|
||||
for idx, result := range in {
|
||||
out[idx] = clonePublishResult(result)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func waitLiveFlush(ctx context.Context, done <-chan struct{}) error {
|
||||
select {
|
||||
case <-done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func closeLivePublisher(ctx context.Context, publisher LivePublisher) error {
|
||||
if closer, ok := publisher.(interface {
|
||||
Close(context.Context) error
|
||||
}); ok {
|
||||
return closer.Close(ctx)
|
||||
}
|
||||
if flusher, ok := publisher.(interface {
|
||||
Flush(context.Context) error
|
||||
}); ok {
|
||||
return flusher.Flush(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopLiveTimer(timer *time.Timer) {
|
||||
if timer == nil {
|
||||
return
|
||||
}
|
||||
if !timer.Stop() {
|
||||
select {
|
||||
case <-timer.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resetLiveTimer(timer *time.Timer, d time.Duration) {
|
||||
if d <= 0 {
|
||||
d = liveRetryInterval
|
||||
}
|
||||
stopLiveTimer(timer)
|
||||
timer.Reset(d)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package notice
|
||||
|
||||
import "longheng.io/server/internal/platform/runtimeguard"
|
||||
|
||||
func (*MemoryStore) BackendKind() runtimeguard.Backend {
|
||||
return runtimeguard.BackendMemory
|
||||
}
|
||||
|
||||
func (*SQLStore) BackendKind() runtimeguard.Backend {
|
||||
return runtimeguard.BackendMySQL
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
buskit "longheng.io/server/internal/platform/bus"
|
||||
"longheng.io/server/pkg/contracts"
|
||||
"longheng.io/server/pkg/protocol"
|
||||
)
|
||||
|
||||
type BusPublisher struct {
|
||||
Bus buskit.Bus
|
||||
Topic string
|
||||
}
|
||||
|
||||
func NewBusPublisher(bus buskit.Bus) *BusPublisher {
|
||||
return &BusPublisher{Bus: bus, Topic: contracts.TopicNoticePublished}
|
||||
}
|
||||
|
||||
func (p *BusPublisher) PublishNotice(ctx context.Context, result PublishResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil || p.Bus == nil {
|
||||
return ErrLivePublisherMissing
|
||||
}
|
||||
topic := strings.TrimSpace(p.Topic)
|
||||
if topic == "" {
|
||||
topic = contracts.TopicNoticePublished
|
||||
}
|
||||
return p.Bus.Publish(ctx, topic, noticePublishedEvent(result))
|
||||
}
|
||||
|
||||
func noticePublishedEvent(result PublishResult) contracts.NoticePublished {
|
||||
recipients := make([]string, 0, len(result.Deliveries))
|
||||
deliveryIDs := make([]string, 0, len(result.Deliveries))
|
||||
for _, delivery := range result.Deliveries {
|
||||
recipients = append(recipients, delivery.AccountID)
|
||||
deliveryIDs = append(deliveryIDs, delivery.ID)
|
||||
}
|
||||
return contracts.NoticePublished{
|
||||
NoticeID: result.Notice.ID,
|
||||
Kind: result.Notice.Kind,
|
||||
Scope: result.Notice.Scope,
|
||||
ShardID: result.Notice.ShardID,
|
||||
Title: result.Notice.Title,
|
||||
Body: result.Notice.Body,
|
||||
AttachmentRef: result.Notice.AttachmentRef,
|
||||
Recipients: recipients,
|
||||
DeliveryIDs: deliveryIDs,
|
||||
Meta: cloneStringMap(result.Meta),
|
||||
PublishedAt: result.PublishedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type GatewayPushEncoder func(PublishResult, Delivery) ([]byte, error)
|
||||
|
||||
type PushPublisherOptions struct {
|
||||
Bus buskit.Bus
|
||||
Topic string
|
||||
PacketID int32
|
||||
MsgID uint32
|
||||
WireFormat string
|
||||
Encode GatewayPushEncoder
|
||||
BroadcastAnnouncements bool
|
||||
}
|
||||
|
||||
type GatewayPushPublisher struct {
|
||||
options PushPublisherOptions
|
||||
}
|
||||
|
||||
func NewGatewayPushPublisher(bus buskit.Bus) *GatewayPushPublisher {
|
||||
return NewGatewayPushPublisherWithOptions(PushPublisherOptions{Bus: bus})
|
||||
}
|
||||
|
||||
func NewGatewayPushPublisherWithOptions(options PushPublisherOptions) *GatewayPushPublisher {
|
||||
if options.Topic == "" {
|
||||
options.Topic = contracts.TopicGatewayPush
|
||||
}
|
||||
if options.PacketID == 0 {
|
||||
options.PacketID = protocol.PacketIDContent
|
||||
}
|
||||
if options.MsgID == 0 {
|
||||
options.MsgID = protocol.MessageNoticePush
|
||||
}
|
||||
if options.Encode == nil {
|
||||
options.Encode = EncodeGatewayNoticePush
|
||||
}
|
||||
return &GatewayPushPublisher{options: options}
|
||||
}
|
||||
|
||||
func (p *GatewayPushPublisher) PublishNotice(ctx context.Context, result PublishResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil || p.options.Bus == nil {
|
||||
return ErrLivePublisherMissing
|
||||
}
|
||||
if result.Notice.Kind == KindAnnouncement && len(result.Deliveries) == 0 && p.options.BroadcastAnnouncements {
|
||||
body, err := p.options.Encode(result, Delivery{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return p.publish(ctx, contracts.GatewayPush{
|
||||
Broadcast: true,
|
||||
PacketID: p.options.PacketID,
|
||||
MsgID: p.options.MsgID,
|
||||
WireFormat: p.options.WireFormat,
|
||||
Body: body,
|
||||
Metadata: gwNoticeMeta(result, Delivery{}),
|
||||
Note: "notice-announcement-push",
|
||||
CreatedAt: result.PublishedAt,
|
||||
})
|
||||
}
|
||||
var errs []error
|
||||
for _, delivery := range result.Deliveries {
|
||||
body, err := p.options.Encode(result, delivery)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
err = p.publish(ctx, contracts.GatewayPush{
|
||||
AccountID: delivery.AccountID,
|
||||
PacketID: p.options.PacketID,
|
||||
MsgID: p.options.MsgID,
|
||||
WireFormat: p.options.WireFormat,
|
||||
Body: body,
|
||||
Metadata: gwNoticeMeta(result, delivery),
|
||||
Note: "notice-direct-push",
|
||||
CreatedAt: result.PublishedAt,
|
||||
})
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (p *GatewayPushPublisher) publish(ctx context.Context, push contracts.GatewayPush) error {
|
||||
topic := strings.TrimSpace(p.options.Topic)
|
||||
if topic == "" {
|
||||
topic = contracts.TopicGatewayPush
|
||||
}
|
||||
return p.options.Bus.Publish(ctx, topic, push)
|
||||
}
|
||||
|
||||
func EncodeGatewayNoticePush(result PublishResult, delivery Delivery) ([]byte, error) {
|
||||
return protocol.EncodeNoticePush(protocol.NoticePush{
|
||||
NoticeID: result.Notice.ID,
|
||||
DeliveryID: delivery.ID,
|
||||
Title: result.Notice.Title,
|
||||
Body: result.Notice.Body,
|
||||
AttachmentRef: result.Notice.AttachmentRef,
|
||||
CreatedAt: result.PublishedAt.Unix(),
|
||||
}), nil
|
||||
}
|
||||
|
||||
func gwNoticeMeta(result PublishResult, delivery Delivery) map[string]string {
|
||||
meta := map[string]string{
|
||||
"notice_id": result.Notice.ID,
|
||||
"kind": result.Notice.Kind,
|
||||
}
|
||||
if result.Notice.ShardID != "" {
|
||||
meta["shard_id"] = result.Notice.ShardID
|
||||
}
|
||||
if delivery.ID != "" {
|
||||
meta["delivery_id"] = delivery.ID
|
||||
}
|
||||
if delivery.AccountID != "" {
|
||||
meta["account_id"] = delivery.AccountID
|
||||
}
|
||||
return meta
|
||||
}
|
||||
|
||||
type FanoutPublisher struct {
|
||||
Publishers []LivePublisher
|
||||
}
|
||||
|
||||
func NewFanoutPublisher(publishers ...LivePublisher) FanoutPublisher {
|
||||
return FanoutPublisher{Publishers: publishers}
|
||||
}
|
||||
|
||||
func (p FanoutPublisher) PublishNotice(ctx context.Context, result PublishResult) error {
|
||||
var errs []error
|
||||
for _, publisher := range p.Publishers {
|
||||
if publisher == nil {
|
||||
continue
|
||||
}
|
||||
if err := publisher.PublishNotice(ctx, result); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPublishStream = "notice.publish"
|
||||
defaultPubEventType = "notice.published"
|
||||
)
|
||||
|
||||
type publishEventPayload struct {
|
||||
Notice Notice `json:"notice"`
|
||||
Deliveries []Delivery `json:"deliveries,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
AdminReceiptID string `json:"admin_receipt_id,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
PublishedAt time.Time `json:"published_at"`
|
||||
}
|
||||
|
||||
func (s Service) lookupPublishEvent(ctx context.Context, request PublishRequest) (PublishResult, bool, error) {
|
||||
if s.EventLog == nil {
|
||||
return PublishResult{}, false, nil
|
||||
}
|
||||
event, ok, err := s.EventLog.GetByIdempotencyKey(ctx, publishEventKey(request.Notice.ID, request.IdempotencyKey))
|
||||
if err != nil || !ok {
|
||||
return PublishResult{}, false, err
|
||||
}
|
||||
result, err := publishResultEvent(event)
|
||||
if err != nil {
|
||||
return PublishResult{}, false, err
|
||||
}
|
||||
if publishReplayClash(result, request) {
|
||||
return PublishResult{}, false, ErrIdempotencyConflict
|
||||
}
|
||||
result.Duplicate = true
|
||||
return result, true, nil
|
||||
}
|
||||
|
||||
func (s Service) appendPublishEvent(ctx context.Context, result PublishResult) error {
|
||||
if s.EventLog == nil {
|
||||
return nil
|
||||
}
|
||||
payload, err := json.Marshal(payloadFromResult(result))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.EventLog.Append(ctx, eventlog.Event{
|
||||
ID: publishEventID(result.Notice.ID, result.IdempotencyKey),
|
||||
Stream: s.publishEventStream(),
|
||||
Type: s.publishEventType(),
|
||||
AggregateID: result.Notice.ID,
|
||||
IdempotencyKey: publishEventKey(result.Notice.ID, result.IdempotencyKey),
|
||||
Payload: payload,
|
||||
Headers: map[string]string{
|
||||
"notice_id": result.Notice.ID,
|
||||
"kind": result.Notice.Kind,
|
||||
"recipient_count": strconv.Itoa(len(result.Deliveries)),
|
||||
"admin_receipt_id": result.AdminReceiptID,
|
||||
},
|
||||
})
|
||||
if errors.Is(err, eventlog.ErrIdempotencyConflict) {
|
||||
return ErrIdempotencyConflict
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s Service) publishEventStream() string {
|
||||
if stream := strings.TrimSpace(s.EventLogStream); stream != "" {
|
||||
return stream
|
||||
}
|
||||
return defaultPublishStream
|
||||
}
|
||||
|
||||
func (s Service) publishEventType() string {
|
||||
if typ := strings.TrimSpace(s.EventLogType); typ != "" {
|
||||
return typ
|
||||
}
|
||||
return defaultPubEventType
|
||||
}
|
||||
|
||||
func payloadFromResult(result PublishResult) publishEventPayload {
|
||||
return publishEventPayload{
|
||||
Notice: cloneNotice(result.Notice),
|
||||
Deliveries: cloneDeliveries(result.Deliveries),
|
||||
IdempotencyKey: result.IdempotencyKey,
|
||||
AdminReceiptID: result.AdminReceiptID,
|
||||
Meta: cloneStringMap(result.Meta),
|
||||
PublishedAt: result.PublishedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func publishResultEvent(event eventlog.Event) (PublishResult, error) {
|
||||
var payload publishEventPayload
|
||||
if err := json.Unmarshal(event.Payload, &payload); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
return PublishResult{
|
||||
Accepted: true,
|
||||
Notice: cloneNotice(payload.Notice),
|
||||
Deliveries: cloneDeliveries(payload.Deliveries),
|
||||
IdempotencyKey: firstNonEmpty(payload.IdempotencyKey, event.IdempotencyKey),
|
||||
AdminReceiptID: payload.AdminReceiptID,
|
||||
Meta: cloneStringMap(payload.Meta),
|
||||
PublishedAt: payload.PublishedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func publishEventKey(noticeID, key string) string {
|
||||
noticeID = strings.TrimSpace(noticeID)
|
||||
key = strings.TrimSpace(key)
|
||||
if noticeID == "" || key == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join([]string{"notice", "publish", "v1", encodeEventKeyPart(noticeID), encodeEventKeyPart(key)}, ":")
|
||||
}
|
||||
|
||||
func encodeEventKeyPart(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "_"
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(value))
|
||||
}
|
||||
|
||||
func publishEventID(noticeID, key string) string {
|
||||
sum := sha256.Sum256([]byte(publishEventKey(noticeID, key)))
|
||||
return "notice-publish-" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
@@ -0,0 +1,949 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/admincmd"
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrStoreRequired = errors.New("notice store is required")
|
||||
ErrNoticeIDRequired = errors.New("notice id is required")
|
||||
ErrNoticeContentEmpty = errors.New("notice title or body is required")
|
||||
ErrRecipientRequired = errors.New("notice recipient is required")
|
||||
ErrIdempotencyRequired = errors.New("notice idempotency key is required")
|
||||
ErrIdempotencyConflict = errors.New("notice idempotency conflict")
|
||||
ErrNoticeExists = errors.New("notice already exists")
|
||||
ErrNoticeNotFound = errors.New("notice not found")
|
||||
ErrDeliveryNotFound = errors.New("notice delivery not found")
|
||||
ErrNoticeInactive = errors.New("notice is inactive")
|
||||
ErrInvalidNotice = errors.New("notice is invalid")
|
||||
ErrInvalidNoticeKind = errors.New("notice kind is invalid")
|
||||
ErrInvalidNoticeRequest = errors.New("notice request is invalid")
|
||||
ErrLivePublisherMissing = errors.New("notice live publisher is missing")
|
||||
)
|
||||
|
||||
const (
|
||||
KindDirect = "direct"
|
||||
KindAnnouncement = "announcement"
|
||||
|
||||
StatusPending = "pending"
|
||||
StatusAcknowledged = "acknowledged"
|
||||
StatusDeleted = "deleted"
|
||||
|
||||
defaultListLimit = 100
|
||||
maxListLimit = 100
|
||||
rollbackTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type Notice struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Scope string `json:"scope,omitempty"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Body string `json:"body,omitempty"`
|
||||
AttachmentRef string `json:"attachment_ref,omitempty"`
|
||||
StartsAt time.Time `json:"starts_at,omitempty"`
|
||||
EndsAt time.Time `json:"ends_at,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type PublishRequest struct {
|
||||
Notice Notice `json:"notice"`
|
||||
Recipients []string `json:"recipients,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
AdminCommand *admincmd.Command `json:"admin_command,omitempty"`
|
||||
}
|
||||
|
||||
type PublishResult struct {
|
||||
Accepted bool `json:"accepted"`
|
||||
Duplicate bool `json:"duplicate,omitempty"`
|
||||
Notice Notice `json:"notice"`
|
||||
Deliveries []Delivery `json:"deliveries,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
AdminReceiptID string `json:"admin_receipt_id,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
PublishedAt time.Time `json:"published_at"`
|
||||
Live LiveStatus `json:"live,omitempty"`
|
||||
}
|
||||
|
||||
type LiveStatus struct {
|
||||
Attempted bool `json:"attempted,omitempty"`
|
||||
Delivered bool `json:"delivered,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type Delivery struct {
|
||||
ID string `json:"id"`
|
||||
NoticeID string `json:"notice_id"`
|
||||
AccountID string `json:"account_id"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Status string `json:"status"`
|
||||
AttachmentRef string `json:"attachment_ref,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
AcknowledgedAt time.Time `json:"acknowledged_at,omitempty"`
|
||||
}
|
||||
|
||||
type AcknowledgeRequest struct {
|
||||
DeliveryID string `json:"delivery_id,omitempty"`
|
||||
NoticeID string `json:"notice_id,omitempty"`
|
||||
AccountID string `json:"account_id,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
}
|
||||
|
||||
type DeleteRequest struct {
|
||||
DeliveryIDs []string `json:"delivery_ids,omitempty"`
|
||||
NoticeID string `json:"notice_id,omitempty"`
|
||||
AccountID string `json:"account_id,omitempty"`
|
||||
}
|
||||
|
||||
type DeleteResult struct {
|
||||
DeletedIDs []string `json:"deleted_ids,omitempty"`
|
||||
MissingIDs []string `json:"missing_ids,omitempty"`
|
||||
}
|
||||
|
||||
type Query struct {
|
||||
AccountID string `json:"account_id,omitempty"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Now time.Time `json:"now,omitempty"`
|
||||
Limit int `json:"limit,omitempty"`
|
||||
Cursor string `json:"cursor,omitempty"`
|
||||
}
|
||||
|
||||
type ListResult struct {
|
||||
Items []Delivery `json:"items"`
|
||||
NextCursor string `json:"next_cursor,omitempty"`
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Notices int `json:"notices"`
|
||||
Deliveries int `json:"deliveries"`
|
||||
ByKind map[string]int `json:"by_kind,omitempty"`
|
||||
ByStatus map[string]int `json:"by_status,omitempty"`
|
||||
PublishKeys int `json:"publish_keys"`
|
||||
AcknowledgeKeys int `json:"acknowledge_keys"`
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
Publish(context.Context, PublishRequest, time.Time) (PublishResult, error)
|
||||
Acknowledge(context.Context, AcknowledgeRequest, time.Time) (Delivery, bool, error)
|
||||
Delete(context.Context, DeleteRequest) (DeleteResult, error)
|
||||
ListForAccount(context.Context, Query) ([]Delivery, error)
|
||||
ActiveAnnouncements(context.Context, Query) ([]Notice, error)
|
||||
Snapshot(context.Context) (Snapshot, error)
|
||||
}
|
||||
|
||||
type LivePublisher interface {
|
||||
PublishNotice(context.Context, PublishResult) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Store Store
|
||||
Publisher LivePublisher
|
||||
EventLog *eventlog.Log
|
||||
EventLogStream string
|
||||
EventLogType string
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (s Service) Publish(ctx context.Context, request PublishRequest) (PublishResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return PublishResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normPublishReq(request)
|
||||
if err := validPublishReq(request); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if request.AdminCommand != nil {
|
||||
command := *request.AdminCommand
|
||||
command.Operation = firstNonEmpty(command.Operation, "notice.publish")
|
||||
command.Scope = firstNonEmpty(command.Scope, "notice")
|
||||
command.Target = firstNonEmpty(command.Target, request.Notice.ID)
|
||||
command.Params = mergeCommandParams(command.Params, request)
|
||||
if err := admincmd.Validate(command, admincmd.DangerousPolicy()); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
receipt, err := admincmd.NewReceipt(command, "accepted", s.now())
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
request.AdminCommand = &command
|
||||
request.Meta = mergeStringMap(request.Meta, map[string]string{"admin_receipt_id": receipt.ID})
|
||||
}
|
||||
if result, ok, err := s.lookupPublishEvent(ctx, request); err != nil || ok {
|
||||
return result, err
|
||||
}
|
||||
result, err := s.Store.Publish(ctx, request, s.now())
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if err := s.appendPublishEvent(ctx, result); err != nil {
|
||||
return result, errors.Join(err, s.rollbackPublish(ctx, result))
|
||||
}
|
||||
if result.Duplicate || s.Publisher == nil {
|
||||
return result, err
|
||||
}
|
||||
result.Live.Attempted = true
|
||||
publishErr := s.Publisher.PublishNotice(ctx, result)
|
||||
if publishErr != nil {
|
||||
result.Live.Error = publishErr.Error()
|
||||
} else {
|
||||
result.Live.Delivered = true
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type publishRollbackStore interface {
|
||||
RollbackPublish(context.Context, PublishResult) error
|
||||
}
|
||||
|
||||
func (s Service) rollbackPublish(ctx context.Context, result PublishResult) error {
|
||||
if result.Duplicate || s.Store == nil {
|
||||
return nil
|
||||
}
|
||||
store, ok := s.Store.(publishRollbackStore)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
// 可靠性补偿不能继承已取消的请求 ctx,但要保留 trace/value 方便排障。
|
||||
base := context.Background()
|
||||
if ctx != nil {
|
||||
base = context.WithoutCancel(ctx)
|
||||
}
|
||||
rollbackCtx, cancel := context.WithTimeout(base, rollbackTimeout)
|
||||
defer cancel()
|
||||
return store.RollbackPublish(rollbackCtx, result)
|
||||
}
|
||||
|
||||
func (s Service) Acknowledge(ctx context.Context, request AcknowledgeRequest) (Delivery, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return Delivery{}, false, ErrStoreRequired
|
||||
}
|
||||
request = normalizeAckRequest(request)
|
||||
if request.IdempotencyKey == "" {
|
||||
return Delivery{}, false, ErrIdempotencyRequired
|
||||
}
|
||||
if request.DeliveryID == "" && (request.NoticeID == "" || request.AccountID == "") {
|
||||
return Delivery{}, false, ErrInvalidNoticeRequest
|
||||
}
|
||||
return s.Store.Acknowledge(ctx, request, s.now())
|
||||
}
|
||||
|
||||
func (s Service) Delete(ctx context.Context, request DeleteRequest) (DeleteResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return DeleteResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normDeleteReq(request)
|
||||
return s.Store.Delete(ctx, request)
|
||||
}
|
||||
|
||||
func (s Service) ListForAccount(ctx context.Context, query Query) ([]Delivery, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return nil, ErrStoreRequired
|
||||
}
|
||||
query.AccountID = strings.TrimSpace(query.AccountID)
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
if query.AccountID == "" {
|
||||
return nil, ErrRecipientRequired
|
||||
}
|
||||
if query.Now.IsZero() {
|
||||
query.Now = s.now()
|
||||
} else {
|
||||
query.Now = query.Now.UTC()
|
||||
}
|
||||
return s.Store.ListForAccount(ctx, query)
|
||||
}
|
||||
|
||||
func (s Service) ListPageForAccount(ctx context.Context, query Query) (ListResult, error) {
|
||||
deliveries, err := s.ListForAccount(ctx, query)
|
||||
if err != nil {
|
||||
return ListResult{}, err
|
||||
}
|
||||
return paginateDeliveries(deliveries, query), nil
|
||||
}
|
||||
|
||||
func (s Service) ActiveAnnouncements(ctx context.Context, query Query) ([]Notice, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return nil, ErrStoreRequired
|
||||
}
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
if query.Now.IsZero() {
|
||||
query.Now = s.now()
|
||||
} else {
|
||||
query.Now = query.Now.UTC()
|
||||
}
|
||||
return s.Store.ActiveAnnouncements(ctx, query)
|
||||
}
|
||||
|
||||
func (s Service) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return Snapshot{}, ErrStoreRequired
|
||||
}
|
||||
return s.Store.Snapshot(ctx)
|
||||
}
|
||||
|
||||
func (s Service) now() time.Time {
|
||||
if s.Now != nil {
|
||||
return s.Now().UTC()
|
||||
}
|
||||
return time.Now().UTC()
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.Mutex
|
||||
notices map[string]Notice
|
||||
deliveries map[string]Delivery
|
||||
deliveriesByAcc map[string][]string
|
||||
publishByKey map[string]PublishResult
|
||||
ackByKey map[string]string
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
notices: make(map[string]Notice),
|
||||
deliveries: make(map[string]Delivery),
|
||||
deliveriesByAcc: make(map[string][]string),
|
||||
publishByKey: make(map[string]PublishResult),
|
||||
ackByKey: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Publish(ctx context.Context, request PublishRequest, now time.Time) (PublishResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return PublishResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normPublishReq(request)
|
||||
if err := validPublishReq(request); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
now = normalizeNow(now)
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
lookupKey := publishLookupKey(request)
|
||||
if existing, ok := s.publishByKey[lookupKey]; ok {
|
||||
if publishReplayClash(existing, request) {
|
||||
return PublishResult{}, ErrIdempotencyConflict
|
||||
}
|
||||
existing.Duplicate = true
|
||||
return clonePublishResult(existing), nil
|
||||
}
|
||||
if _, exists := s.notices[request.Notice.ID]; exists {
|
||||
return PublishResult{}, ErrNoticeExists
|
||||
}
|
||||
|
||||
notice := request.Notice
|
||||
s.notices[notice.ID] = cloneNotice(notice)
|
||||
deliveries := make([]Delivery, 0, len(request.Recipients))
|
||||
for _, accountID := range request.Recipients {
|
||||
delivery := Delivery{
|
||||
ID: DeliveryID(notice.ID, accountID),
|
||||
NoticeID: notice.ID,
|
||||
AccountID: accountID,
|
||||
ShardID: notice.ShardID,
|
||||
Status: StatusPending,
|
||||
AttachmentRef: notice.AttachmentRef,
|
||||
IdempotencyKey: request.IdempotencyKey,
|
||||
Meta: mergeStringMap(notice.Meta, request.Meta),
|
||||
CreatedAt: now,
|
||||
}
|
||||
if _, exists := s.deliveries[delivery.ID]; !exists {
|
||||
s.deliveriesByAcc[accountID] = appendUnique(s.deliveriesByAcc[accountID], delivery.ID)
|
||||
}
|
||||
s.deliveries[delivery.ID] = cloneDelivery(delivery)
|
||||
deliveries = append(deliveries, cloneDelivery(delivery))
|
||||
}
|
||||
sortDeliveries(deliveries)
|
||||
result := PublishResult{
|
||||
Accepted: true,
|
||||
Notice: cloneNotice(notice),
|
||||
Deliveries: deliveries,
|
||||
IdempotencyKey: request.IdempotencyKey,
|
||||
AdminReceiptID: request.Meta["admin_receipt_id"],
|
||||
Meta: cloneStringMap(request.Meta),
|
||||
PublishedAt: now,
|
||||
}
|
||||
s.publishByKey[lookupKey] = clonePublishResult(result)
|
||||
return clonePublishResult(result), nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) RollbackPublish(ctx context.Context, result PublishResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
if result.Notice.ID == "" || result.IdempotencyKey == "" {
|
||||
return nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
lookupKey := publishLookupKey(PublishRequest{Notice: result.Notice, IdempotencyKey: result.IdempotencyKey})
|
||||
existing, ok := s.publishByKey[lookupKey]
|
||||
if !ok || existing.Notice.ID != result.Notice.ID {
|
||||
return nil
|
||||
}
|
||||
delete(s.publishByKey, lookupKey)
|
||||
rolledBackDeliveries := make(map[string]struct{}, len(result.Deliveries))
|
||||
for _, delivery := range result.Deliveries {
|
||||
rolledBackDeliveries[delivery.ID] = struct{}{}
|
||||
delete(s.deliveries, delivery.ID)
|
||||
s.deliveriesByAcc[delivery.AccountID] = removeString(s.deliveriesByAcc[delivery.AccountID], delivery.ID)
|
||||
if len(s.deliveriesByAcc[delivery.AccountID]) == 0 {
|
||||
delete(s.deliveriesByAcc, delivery.AccountID)
|
||||
}
|
||||
}
|
||||
for key, deliveryID := range s.ackByKey {
|
||||
if _, ok := rolledBackDeliveries[deliveryID]; ok {
|
||||
delete(s.ackByKey, key)
|
||||
}
|
||||
}
|
||||
delete(s.notices, result.Notice.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Acknowledge(ctx context.Context, request AcknowledgeRequest, now time.Time) (Delivery, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
if s == nil {
|
||||
return Delivery{}, false, ErrStoreRequired
|
||||
}
|
||||
request = normalizeAckRequest(request)
|
||||
if request.IdempotencyKey == "" {
|
||||
return Delivery{}, false, ErrIdempotencyRequired
|
||||
}
|
||||
now = normalizeNow(now)
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
deliveryID := request.DeliveryID
|
||||
if deliveryID == "" {
|
||||
deliveryID = DeliveryID(request.NoticeID, request.AccountID)
|
||||
}
|
||||
lookupKey := acknowledgeLookupKey(deliveryID, request.IdempotencyKey)
|
||||
if deliveryID == "" {
|
||||
return Delivery{}, false, ErrInvalidNoticeRequest
|
||||
}
|
||||
if knownDeliveryID, ok := s.ackByKey[lookupKey]; ok {
|
||||
deliveryID = knownDeliveryID
|
||||
delivery, exists := s.deliveries[deliveryID]
|
||||
if !exists {
|
||||
return Delivery{}, true, ErrDeliveryNotFound
|
||||
}
|
||||
return cloneDelivery(delivery), true, nil
|
||||
}
|
||||
delivery, ok := s.deliveries[deliveryID]
|
||||
if !ok {
|
||||
return Delivery{}, false, ErrDeliveryNotFound
|
||||
}
|
||||
if delivery.Status == StatusDeleted {
|
||||
return Delivery{}, false, ErrDeliveryNotFound
|
||||
}
|
||||
if delivery.Status == StatusAcknowledged {
|
||||
s.ackByKey[lookupKey] = delivery.ID
|
||||
return cloneDelivery(delivery), true, nil
|
||||
}
|
||||
delivery.Status = StatusAcknowledged
|
||||
delivery.AcknowledgedAt = now
|
||||
s.deliveries[delivery.ID] = cloneDelivery(delivery)
|
||||
s.ackByKey[lookupKey] = delivery.ID
|
||||
return cloneDelivery(delivery), false, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Delete(ctx context.Context, request DeleteRequest) (DeleteResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return DeleteResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normDeleteReq(request)
|
||||
if len(request.DeliveryIDs) == 0 {
|
||||
return DeleteResult{}, nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
result := DeleteResult{}
|
||||
for _, id := range request.DeliveryIDs {
|
||||
delivery, ok := s.deliveries[id]
|
||||
if !ok || delivery.Status == StatusDeleted || (request.AccountID != "" && delivery.AccountID != request.AccountID) {
|
||||
result.MissingIDs = append(result.MissingIDs, id)
|
||||
continue
|
||||
}
|
||||
delivery.Status = StatusDeleted
|
||||
s.deliveries[id] = cloneDelivery(delivery)
|
||||
result.DeletedIDs = append(result.DeletedIDs, id)
|
||||
}
|
||||
sort.Strings(result.DeletedIDs)
|
||||
sort.Strings(result.MissingIDs)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ListForAccount(ctx context.Context, query Query) ([]Delivery, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil {
|
||||
return nil, ErrStoreRequired
|
||||
}
|
||||
query.AccountID = strings.TrimSpace(query.AccountID)
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
if query.AccountID == "" {
|
||||
return nil, ErrRecipientRequired
|
||||
}
|
||||
now := normalizeNow(query.Now)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
ids := append([]string(nil), s.deliveriesByAcc[query.AccountID]...)
|
||||
out := make([]Delivery, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
delivery, ok := s.deliveries[id]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
notice, ok := s.notices[delivery.NoticeID]
|
||||
if !ok || !noticeVisible(notice, query.ShardID, now) {
|
||||
continue
|
||||
}
|
||||
if delivery.Status == StatusDeleted {
|
||||
continue
|
||||
}
|
||||
out = append(out, cloneDelivery(delivery))
|
||||
}
|
||||
sortAcctDeliveries(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) ActiveAnnouncements(ctx context.Context, query Query) ([]Notice, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s == nil {
|
||||
return nil, ErrStoreRequired
|
||||
}
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
now := normalizeNow(query.Now)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
out := make([]Notice, 0)
|
||||
for _, notice := range s.notices {
|
||||
if notice.Kind != KindAnnouncement || !noticeVisible(notice, query.ShardID, now) {
|
||||
continue
|
||||
}
|
||||
out = append(out, cloneNotice(notice))
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return Snapshot{}, ErrStoreRequired
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
snapshot := Snapshot{
|
||||
Notices: len(s.notices),
|
||||
Deliveries: len(s.deliveries),
|
||||
ByKind: make(map[string]int),
|
||||
ByStatus: make(map[string]int),
|
||||
PublishKeys: len(s.publishByKey),
|
||||
AcknowledgeKeys: len(s.ackByKey),
|
||||
}
|
||||
for _, notice := range s.notices {
|
||||
snapshot.ByKind[notice.Kind]++
|
||||
}
|
||||
for _, delivery := range s.deliveries {
|
||||
snapshot.ByStatus[delivery.Status]++
|
||||
}
|
||||
if len(snapshot.ByKind) == 0 {
|
||||
snapshot.ByKind = nil
|
||||
}
|
||||
if len(snapshot.ByStatus) == 0 {
|
||||
snapshot.ByStatus = nil
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func validPublishReq(request PublishRequest) error {
|
||||
notice := request.Notice
|
||||
if notice.ID == "" {
|
||||
return ErrNoticeIDRequired
|
||||
}
|
||||
if notice.Title == "" && notice.Body == "" {
|
||||
return ErrNoticeContentEmpty
|
||||
}
|
||||
if request.IdempotencyKey == "" {
|
||||
return ErrIdempotencyRequired
|
||||
}
|
||||
switch notice.Kind {
|
||||
case KindDirect:
|
||||
if len(request.Recipients) == 0 {
|
||||
return ErrRecipientRequired
|
||||
}
|
||||
case KindAnnouncement:
|
||||
default:
|
||||
return fmt.Errorf("%w: %s", ErrInvalidNoticeKind, notice.Kind)
|
||||
}
|
||||
if !notice.StartsAt.IsZero() && !notice.EndsAt.IsZero() && notice.EndsAt.Before(notice.StartsAt) {
|
||||
return fmt.Errorf("%w: ends_at is before starts_at", ErrInvalidNotice)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func normPublishReq(request PublishRequest) PublishRequest {
|
||||
request.Notice = normalizeNotice(request.Notice)
|
||||
request.Recipients = normalizeIDs(request.Recipients)
|
||||
request.IdempotencyKey = strings.TrimSpace(request.IdempotencyKey)
|
||||
request.Meta = normalizeStringMap(request.Meta)
|
||||
return request
|
||||
}
|
||||
|
||||
func normalizeNotice(notice Notice) Notice {
|
||||
notice.ID = strings.TrimSpace(notice.ID)
|
||||
notice.Kind = strings.ToLower(strings.TrimSpace(notice.Kind))
|
||||
if notice.Kind == "" {
|
||||
notice.Kind = KindDirect
|
||||
}
|
||||
notice.Scope = strings.TrimSpace(notice.Scope)
|
||||
notice.ShardID = strings.TrimSpace(notice.ShardID)
|
||||
notice.Title = strings.TrimSpace(notice.Title)
|
||||
notice.Body = strings.TrimSpace(notice.Body)
|
||||
notice.AttachmentRef = strings.TrimSpace(notice.AttachmentRef)
|
||||
notice.StartsAt = normalizeTime(notice.StartsAt)
|
||||
notice.EndsAt = normalizeTime(notice.EndsAt)
|
||||
notice.Meta = normalizeStringMap(notice.Meta)
|
||||
return notice
|
||||
}
|
||||
|
||||
func normalizeAckRequest(request AcknowledgeRequest) AcknowledgeRequest {
|
||||
request.DeliveryID = strings.TrimSpace(request.DeliveryID)
|
||||
request.NoticeID = strings.TrimSpace(request.NoticeID)
|
||||
request.AccountID = strings.TrimSpace(request.AccountID)
|
||||
request.IdempotencyKey = strings.TrimSpace(request.IdempotencyKey)
|
||||
return request
|
||||
}
|
||||
|
||||
func normDeleteReq(request DeleteRequest) DeleteRequest {
|
||||
request.NoticeID = strings.TrimSpace(request.NoticeID)
|
||||
request.AccountID = strings.TrimSpace(request.AccountID)
|
||||
request.DeliveryIDs = normalizeIDs(request.DeliveryIDs)
|
||||
if len(request.DeliveryIDs) == 0 && request.NoticeID != "" && request.AccountID != "" {
|
||||
request.DeliveryIDs = []string{DeliveryID(request.NoticeID, request.AccountID)}
|
||||
}
|
||||
return request
|
||||
}
|
||||
|
||||
func noticeVisible(notice Notice, shardID string, now time.Time) bool {
|
||||
if notice.Disabled {
|
||||
return false
|
||||
}
|
||||
if notice.ShardID != "" && shardID != "" && notice.ShardID != shardID {
|
||||
return false
|
||||
}
|
||||
if !notice.StartsAt.IsZero() && now.Before(notice.StartsAt) {
|
||||
return false
|
||||
}
|
||||
if !notice.EndsAt.IsZero() && now.After(notice.EndsAt) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func DeliveryID(noticeID, accountID string) string {
|
||||
seed := encodeNoticeKey(strings.TrimSpace(noticeID), strings.TrimSpace(accountID))
|
||||
sum := sha256.Sum256([]byte(seed))
|
||||
return hex.EncodeToString(sum[:16])
|
||||
}
|
||||
|
||||
func publishLookupKey(request PublishRequest) string {
|
||||
return encodeNoticeKey(strings.TrimSpace(request.Notice.ID), strings.TrimSpace(request.IdempotencyKey))
|
||||
}
|
||||
|
||||
func publishReplayClash(existing PublishResult, request PublishRequest) bool {
|
||||
if !reflect.DeepEqual(existing.Notice, request.Notice) {
|
||||
return true
|
||||
}
|
||||
if !reflect.DeepEqual(publishRecipients(existing.Deliveries), request.Recipients) {
|
||||
return true
|
||||
}
|
||||
if !reflect.DeepEqual(existing.Meta, request.Meta) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func publishRecipients(deliveries []Delivery) []string {
|
||||
if len(deliveries) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(deliveries))
|
||||
for _, delivery := range deliveries {
|
||||
out = append(out, delivery.AccountID)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func acknowledgeLookupKey(deliveryID, key string) string {
|
||||
return encodeNoticeKey(strings.TrimSpace(deliveryID), strings.TrimSpace(key))
|
||||
}
|
||||
|
||||
// encodeNoticeKey 用长度前缀编码多段内部键,避免字段自带分隔符或控制字符时发生碰撞。
|
||||
func encodeNoticeKey(parts ...string) string {
|
||||
var builder strings.Builder
|
||||
for _, part := range parts {
|
||||
builder.WriteString(strconv.Itoa(len(part)))
|
||||
builder.WriteByte(':')
|
||||
builder.WriteString(part)
|
||||
}
|
||||
return builder.String()
|
||||
}
|
||||
|
||||
func mergeCommandParams(params map[string]any, request PublishRequest) map[string]any {
|
||||
out := make(map[string]any, len(params)+5)
|
||||
for key, value := range params {
|
||||
out[key] = value
|
||||
}
|
||||
out["notice_id"] = request.Notice.ID
|
||||
out["kind"] = request.Notice.Kind
|
||||
out["shard_id"] = request.Notice.ShardID
|
||||
out["recipient_count"] = len(request.Recipients)
|
||||
out["idempotency_key"] = request.IdempotencyKey
|
||||
return out
|
||||
}
|
||||
|
||||
func normalizeIDs(values []string) []string {
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
out := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
out = append(out, value)
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
func appendUnique(values []string, value string) []string {
|
||||
for _, existing := range values {
|
||||
if existing == value {
|
||||
return values
|
||||
}
|
||||
}
|
||||
return append(values, value)
|
||||
}
|
||||
|
||||
func removeString(values []string, value string) []string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := values[:0]
|
||||
for _, existing := range values {
|
||||
if existing != value {
|
||||
out = append(out, existing)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func normalizeNow(value time.Time) time.Time {
|
||||
if value.IsZero() {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return value.UTC()
|
||||
}
|
||||
|
||||
func normalizeTime(value time.Time) time.Time {
|
||||
if value.IsZero() {
|
||||
return time.Time{}
|
||||
}
|
||||
return value.UTC()
|
||||
}
|
||||
|
||||
func normalizeStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
out[key] = strings.TrimSpace(value)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mergeStringMap(base map[string]string, overlays ...map[string]string) map[string]string {
|
||||
out := cloneStringMap(base)
|
||||
for _, overlay := range overlays {
|
||||
for key, value := range overlay {
|
||||
if out == nil {
|
||||
out = make(map[string]string)
|
||||
}
|
||||
out[key] = value
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneNotice(notice Notice) Notice {
|
||||
notice.Meta = cloneStringMap(notice.Meta)
|
||||
return notice
|
||||
}
|
||||
|
||||
func cloneDelivery(delivery Delivery) Delivery {
|
||||
delivery.Meta = cloneStringMap(delivery.Meta)
|
||||
return delivery
|
||||
}
|
||||
|
||||
func clonePublishResult(result PublishResult) PublishResult {
|
||||
result.Notice = cloneNotice(result.Notice)
|
||||
result.Deliveries = cloneDeliveries(result.Deliveries)
|
||||
result.Meta = cloneStringMap(result.Meta)
|
||||
return result
|
||||
}
|
||||
|
||||
func cloneDeliveries(in []Delivery) []Delivery {
|
||||
if len(in) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make([]Delivery, len(in))
|
||||
for idx, delivery := range in {
|
||||
out[idx] = cloneDelivery(delivery)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func sortDeliveries(deliveries []Delivery) {
|
||||
sort.Slice(deliveries, func(i, j int) bool {
|
||||
if deliveries[i].AccountID != deliveries[j].AccountID {
|
||||
return deliveries[i].AccountID < deliveries[j].AccountID
|
||||
}
|
||||
return deliveries[i].ID < deliveries[j].ID
|
||||
})
|
||||
}
|
||||
|
||||
func sortAcctDeliveries(deliveries []Delivery) {
|
||||
sort.Slice(deliveries, func(i, j int) bool {
|
||||
if !deliveries[i].CreatedAt.Equal(deliveries[j].CreatedAt) {
|
||||
return deliveries[i].CreatedAt.Before(deliveries[j].CreatedAt)
|
||||
}
|
||||
return deliveries[i].ID < deliveries[j].ID
|
||||
})
|
||||
}
|
||||
|
||||
func paginateDeliveries(deliveries []Delivery, query Query) ListResult {
|
||||
sortAcctDeliveries(deliveries)
|
||||
limit := query.Limit
|
||||
if limit <= 0 {
|
||||
limit = defaultListLimit
|
||||
}
|
||||
if limit > maxListLimit {
|
||||
limit = maxListLimit
|
||||
}
|
||||
cursor := strings.TrimSpace(query.Cursor)
|
||||
start := 0
|
||||
if cursor != "" {
|
||||
for idx, delivery := range deliveries {
|
||||
if delivery.ID == cursor {
|
||||
start = idx + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(deliveries) {
|
||||
end = len(deliveries)
|
||||
}
|
||||
items := cloneDeliveries(deliveries[start:end])
|
||||
nextCursor := ""
|
||||
if end < len(deliveries) && len(items) > 0 {
|
||||
nextCursor = items[len(items)-1].ID
|
||||
}
|
||||
return ListResult{Items: items, NextCursor: nextCursor}
|
||||
}
|
||||
|
||||
func ctxErr(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"longheng.io/server/internal/platform/onlinepush"
|
||||
"longheng.io/server/pkg/protocol"
|
||||
)
|
||||
|
||||
type OnlinePushSender interface {
|
||||
Send(context.Context, onlinepush.Request) (onlinepush.Receipt, error)
|
||||
}
|
||||
|
||||
type OnlinePushPublisherOptions struct {
|
||||
Sender OnlinePushSender
|
||||
PacketID int32
|
||||
MsgID uint32
|
||||
WireFormat string
|
||||
Encode GatewayPushEncoder
|
||||
BroadcastAnnouncements bool
|
||||
OfflinePolicy string
|
||||
IdempotencyPrefix string
|
||||
}
|
||||
|
||||
type OnlinePushPublisher struct {
|
||||
options OnlinePushPublisherOptions
|
||||
}
|
||||
|
||||
func NewOnlinePushPublisher(sender OnlinePushSender) *OnlinePushPublisher {
|
||||
return NewOnlinePushPublisherWithOptions(OnlinePushPublisherOptions{Sender: sender})
|
||||
}
|
||||
|
||||
func NewOnlinePushPublisherWithOptions(options OnlinePushPublisherOptions) *OnlinePushPublisher {
|
||||
if options.PacketID == 0 {
|
||||
options.PacketID = protocol.PacketIDContent
|
||||
}
|
||||
if options.MsgID == 0 {
|
||||
options.MsgID = protocol.MessageNoticePush
|
||||
}
|
||||
if options.Encode == nil {
|
||||
options.Encode = EncodeGatewayNoticePush
|
||||
}
|
||||
if strings.TrimSpace(options.OfflinePolicy) == "" {
|
||||
options.OfflinePolicy = onlinepush.OfflineStore
|
||||
}
|
||||
if strings.TrimSpace(options.IdempotencyPrefix) == "" {
|
||||
options.IdempotencyPrefix = "notice-live"
|
||||
}
|
||||
return &OnlinePushPublisher{options: options}
|
||||
}
|
||||
|
||||
func (p *OnlinePushPublisher) PublishNotice(ctx context.Context, result PublishResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if p == nil || p.options.Sender == nil {
|
||||
return ErrLivePublisherMissing
|
||||
}
|
||||
if result.Notice.Kind == KindAnnouncement && len(result.Deliveries) == 0 && p.options.BroadcastAnnouncements {
|
||||
body, err := p.options.Encode(result, Delivery{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = p.options.Sender.Send(ctx, onlinepush.Request{
|
||||
IdempotencyKey: p.idempotencyKey(result, Delivery{}, "announcement"),
|
||||
Broadcast: true,
|
||||
PacketID: p.options.PacketID,
|
||||
MsgID: p.options.MsgID,
|
||||
WireFormat: p.options.WireFormat,
|
||||
Body: body,
|
||||
Metadata: gwNoticeMeta(result, Delivery{}),
|
||||
Note: "notice-announcement-push",
|
||||
OfflinePolicy: onlinepush.OfflineDrop,
|
||||
CreatedAt: result.PublishedAt,
|
||||
})
|
||||
return err
|
||||
}
|
||||
var errs []error
|
||||
for _, delivery := range result.Deliveries {
|
||||
body, err := p.options.Encode(result, delivery)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
_, err = p.options.Sender.Send(ctx, onlinepush.Request{
|
||||
IdempotencyKey: p.idempotencyKey(result, delivery, "direct"),
|
||||
AccountID: delivery.AccountID,
|
||||
PacketID: p.options.PacketID,
|
||||
MsgID: p.options.MsgID,
|
||||
WireFormat: p.options.WireFormat,
|
||||
Body: body,
|
||||
Metadata: gwNoticeMeta(result, delivery),
|
||||
Note: "notice-direct-push",
|
||||
OfflinePolicy: p.options.OfflinePolicy,
|
||||
CreatedAt: result.PublishedAt,
|
||||
})
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func (p *OnlinePushPublisher) idempotencyKey(result PublishResult, delivery Delivery, suffix string) string {
|
||||
parts := []string{
|
||||
encodePushKeyPart(p.options.IdempotencyPrefix),
|
||||
encodePushKeyPart(result.IdempotencyKey),
|
||||
encodePushKeyPart(result.Notice.ID),
|
||||
encodePushKeyPart(delivery.ID),
|
||||
encodePushKeyPart(suffix),
|
||||
}
|
||||
out := make([]string, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
if part != "" {
|
||||
out = append(out, part)
|
||||
}
|
||||
}
|
||||
return strings.Join(out, ":")
|
||||
}
|
||||
|
||||
func encodePushKeyPart(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(value))
|
||||
}
|
||||
@@ -0,0 +1,691 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNoticeSQLDBRequired = errors.New("notice sql db is required")
|
||||
ErrNoticeInvalidTable = errors.New("notice sql table name is invalid")
|
||||
ErrNoticeInvalidRows = errors.New("notice sql rows are invalid")
|
||||
)
|
||||
|
||||
const defSQLTablePrefix = "notice"
|
||||
|
||||
type SQLRows interface {
|
||||
Next() bool
|
||||
Scan(...any) error
|
||||
Close() error
|
||||
Err() error
|
||||
}
|
||||
|
||||
type SQLTx interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
QueryContext(context.Context, string, ...any) (SQLRows, error)
|
||||
Commit() error
|
||||
Rollback() error
|
||||
}
|
||||
|
||||
type SQLDB interface {
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
QueryContext(context.Context, string, ...any) (SQLRows, error)
|
||||
BeginTx(context.Context, *sql.TxOptions) (SQLTx, error)
|
||||
}
|
||||
|
||||
type SQLDBAdapter struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
type SQLTxAdapter struct {
|
||||
Tx *sql.Tx
|
||||
}
|
||||
|
||||
var (
|
||||
_ SQLDB = SQLDBAdapter{}
|
||||
_ SQLTx = SQLTxAdapter{}
|
||||
)
|
||||
|
||||
func (a SQLDBAdapter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
if a.DB == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.DB.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (a SQLDBAdapter) QueryContext(ctx context.Context, query string, args ...any) (SQLRows, error) { //nolint:rowserrcheck // 适配层只返回 rows,调用方负责遍历结束后的 Err 检查。
|
||||
if a.DB == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.DB.QueryContext(ctx, query, args...) //nolint:rowserrcheck // rows 返回后由上层统一关闭并检查 Err。
|
||||
}
|
||||
|
||||
func (a SQLDBAdapter) BeginTx(ctx context.Context, options *sql.TxOptions) (SQLTx, error) {
|
||||
if a.DB == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
tx, err := a.DB.BeginTx(ctx, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return SQLTxAdapter{Tx: tx}, nil
|
||||
}
|
||||
|
||||
func (a SQLTxAdapter) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) {
|
||||
if a.Tx == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.Tx.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (a SQLTxAdapter) QueryContext(ctx context.Context, query string, args ...any) (SQLRows, error) { //nolint:rowserrcheck // 事务适配层只透传 rows,调用方负责遍历结束后的 Err 检查。
|
||||
if a.Tx == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.Tx.QueryContext(ctx, query, args...) //nolint:rowserrcheck // rows 返回后由上层统一关闭并检查 Err。
|
||||
}
|
||||
|
||||
func (a SQLTxAdapter) Commit() error {
|
||||
if a.Tx == nil {
|
||||
return ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.Tx.Commit()
|
||||
}
|
||||
|
||||
func (a SQLTxAdapter) Rollback() error {
|
||||
if a.Tx == nil {
|
||||
return ErrNoticeSQLDBRequired
|
||||
}
|
||||
return a.Tx.Rollback()
|
||||
}
|
||||
|
||||
type SQLStoreOptions struct {
|
||||
Name string
|
||||
TablePrefix string
|
||||
NoticeTable string
|
||||
DeliveryTable string
|
||||
PublishKeyTable string
|
||||
AcknowledgeKeyTable string
|
||||
}
|
||||
|
||||
type SQLStore struct {
|
||||
name string
|
||||
db SQLDB
|
||||
notices string
|
||||
deliveries string
|
||||
publish string
|
||||
ack string
|
||||
closer interface{ Close() error }
|
||||
}
|
||||
|
||||
func NewSQLStore(db SQLDB, options SQLStoreOptions) (*SQLStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
tables, err := normSQLStoreTables(options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
name := strings.TrimSpace(options.Name)
|
||||
if name == "" {
|
||||
name = "sql-notice"
|
||||
}
|
||||
return &SQLStore{
|
||||
name: name,
|
||||
db: db,
|
||||
notices: tables.notices,
|
||||
deliveries: tables.deliveries,
|
||||
publish: tables.publish,
|
||||
ack: tables.ack,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func NewSQLStoreFromDB(db *sql.DB, options SQLStoreOptions) (*SQLStore, error) {
|
||||
if db == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
store, err := NewSQLStore(SQLDBAdapter{DB: db}, options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
store.closer = db
|
||||
return store, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) Close() error {
|
||||
if s == nil || s.closer == nil {
|
||||
return nil
|
||||
}
|
||||
return s.closer.Close()
|
||||
}
|
||||
|
||||
func (s *SQLStore) EnsureSchema(ctx context.Context) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNoticeSQLDBRequired
|
||||
}
|
||||
for _, schema := range s.Schema() {
|
||||
if _, err := s.db.ExecContext(ctx, schema); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) Schema() []string {
|
||||
noticeTable := quoteSQLIdentifier(s.notices)
|
||||
deliveryTable := quoteSQLIdentifier(s.deliveries)
|
||||
publishTable := quoteSQLIdentifier(s.publish)
|
||||
ackTable := quoteSQLIdentifier(s.ack)
|
||||
return []string{
|
||||
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
id VARCHAR(128) NOT NULL,
|
||||
kind VARCHAR(32) NOT NULL,
|
||||
scope VARCHAR(128) NOT NULL DEFAULT '',
|
||||
shard_id VARCHAR(128) NOT NULL DEFAULT '',
|
||||
title VARCHAR(255) NOT NULL DEFAULT '',
|
||||
body TEXT NULL,
|
||||
attachment_ref VARCHAR(255) NOT NULL DEFAULT '',
|
||||
starts_at DATETIME(6) NULL,
|
||||
ends_at DATETIME(6) NULL,
|
||||
disabled BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
meta_json JSON NULL,
|
||||
published_at DATETIME(6) NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_notice_kind (kind, shard_id, starts_at, ends_at)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, noticeTable),
|
||||
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
id VARCHAR(64) NOT NULL,
|
||||
notice_id VARCHAR(128) NOT NULL,
|
||||
account_id VARCHAR(128) NOT NULL,
|
||||
shard_id VARCHAR(128) NOT NULL DEFAULT '',
|
||||
status VARCHAR(32) NOT NULL,
|
||||
attachment_ref VARCHAR(255) NOT NULL DEFAULT '',
|
||||
idempotency_key VARCHAR(191) NOT NULL DEFAULT '',
|
||||
meta_json JSON NULL,
|
||||
created_at DATETIME(6) NOT NULL,
|
||||
acknowledged_at DATETIME(6) NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_notice_delivery_account (account_id, status, created_at),
|
||||
KEY idx_notice_delivery_notice (notice_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, deliveryTable),
|
||||
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
lookup_key CHAR(64) NOT NULL,
|
||||
notice_id VARCHAR(128) NOT NULL,
|
||||
idempotency_key VARCHAR(191) NOT NULL,
|
||||
meta_json JSON NULL,
|
||||
created_at DATETIME(6) NOT NULL,
|
||||
PRIMARY KEY (lookup_key),
|
||||
KEY idx_notice_publish_notice (notice_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, publishTable),
|
||||
fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
|
||||
lookup_key CHAR(64) NOT NULL,
|
||||
delivery_id VARCHAR(64) NOT NULL,
|
||||
idempotency_key VARCHAR(191) NOT NULL,
|
||||
created_at DATETIME(6) NOT NULL,
|
||||
PRIMARY KEY (lookup_key),
|
||||
KEY idx_notice_ack_delivery (delivery_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`, ackTable),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SQLStore) Publish(ctx context.Context, request PublishRequest, now time.Time) (PublishResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return PublishResult{}, ErrNoticeSQLDBRequired
|
||||
}
|
||||
request = normPublishReq(request)
|
||||
if err := validPublishReq(request); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
now = normalizeNow(now)
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
lookupKey := sqlLookupKey(publishLookupKey(request))
|
||||
if existingNoticeID, ok, err := s.lookupPublishKey(ctx, tx, lookupKey); err != nil {
|
||||
return PublishResult{}, err
|
||||
} else if ok {
|
||||
result, err := s.publishNoticeResult(ctx, tx, existingNoticeID, request.IdempotencyKey, true)
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if publishReplayClash(result, request) {
|
||||
return PublishResult{}, ErrIdempotencyConflict
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
committed = true
|
||||
return result, nil
|
||||
}
|
||||
if _, ok, err := s.getNotice(ctx, tx, request.Notice.ID); err != nil {
|
||||
return PublishResult{}, err
|
||||
} else if ok {
|
||||
return PublishResult{}, ErrNoticeExists
|
||||
}
|
||||
if err := s.insertNotice(ctx, tx, request.Notice, now); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
deliveries := make([]Delivery, 0, len(request.Recipients))
|
||||
for _, accountID := range request.Recipients {
|
||||
delivery := Delivery{
|
||||
ID: DeliveryID(request.Notice.ID, accountID),
|
||||
NoticeID: request.Notice.ID,
|
||||
AccountID: accountID,
|
||||
ShardID: request.Notice.ShardID,
|
||||
Status: StatusPending,
|
||||
AttachmentRef: request.Notice.AttachmentRef,
|
||||
IdempotencyKey: request.IdempotencyKey,
|
||||
Meta: mergeStringMap(request.Notice.Meta, request.Meta),
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err := s.insertDelivery(ctx, tx, delivery); err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
deliveries = append(deliveries, cloneDelivery(delivery))
|
||||
}
|
||||
sortDeliveries(deliveries)
|
||||
if err := s.insertPublishKey(ctx, tx, lookupKey, request.Notice.ID, request.IdempotencyKey, request.Meta, now); err != nil {
|
||||
if isNoticeSQLDuplicate(err) {
|
||||
_ = tx.Rollback()
|
||||
committed = true
|
||||
existingNoticeID, ok, lookupErr := s.lookupPublishKey(ctx, s.db, lookupKey)
|
||||
if lookupErr != nil {
|
||||
return PublishResult{}, lookupErr
|
||||
}
|
||||
if !ok {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
result, getErr := s.publishNoticeResult(ctx, s.db, existingNoticeID, request.IdempotencyKey, true)
|
||||
if getErr != nil {
|
||||
return PublishResult{}, getErr
|
||||
}
|
||||
if publishReplayClash(result, request) {
|
||||
return PublishResult{}, ErrIdempotencyConflict
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
if recovered, ok := s.recoverPublishCommit(ctx, request); ok {
|
||||
committed = true
|
||||
return recovered, nil
|
||||
}
|
||||
return PublishResult{}, err
|
||||
}
|
||||
committed = true
|
||||
result := PublishResult{
|
||||
Accepted: true,
|
||||
Notice: cloneNotice(request.Notice),
|
||||
Deliveries: deliveries,
|
||||
IdempotencyKey: request.IdempotencyKey,
|
||||
AdminReceiptID: request.Meta["admin_receipt_id"],
|
||||
Meta: cloneStringMap(request.Meta),
|
||||
PublishedAt: now,
|
||||
}
|
||||
return clonePublishResult(result), nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) RollbackPublish(ctx context.Context, result PublishResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return ErrNoticeSQLDBRequired
|
||||
}
|
||||
if result.Notice.ID == "" || result.IdempotencyKey == "" {
|
||||
return nil
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
lookupKey := sqlLookupKey(publishLookupKey(PublishRequest{Notice: result.Notice, IdempotencyKey: result.IdempotencyKey}))
|
||||
if _, err := tx.ExecContext(ctx, "DELETE FROM "+quoteSQLIdentifier(s.publish)+" WHERE lookup_key = ?", lookupKey); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "DELETE FROM "+quoteSQLIdentifier(s.ack)+" WHERE delivery_id IN (SELECT id FROM "+quoteSQLIdentifier(s.deliveries)+" WHERE notice_id = ?)", result.Notice.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "DELETE FROM "+quoteSQLIdentifier(s.deliveries)+" WHERE notice_id = ?", result.Notice.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "DELETE FROM "+quoteSQLIdentifier(s.notices)+" WHERE id = ?", result.Notice.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
committed = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) Acknowledge(ctx context.Context, request AcknowledgeRequest, now time.Time) (Delivery, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return Delivery{}, false, ErrNoticeSQLDBRequired
|
||||
}
|
||||
request = normalizeAckRequest(request)
|
||||
if request.IdempotencyKey == "" {
|
||||
return Delivery{}, false, ErrIdempotencyRequired
|
||||
}
|
||||
now = normalizeNow(now)
|
||||
|
||||
deliveryID := request.DeliveryID
|
||||
if deliveryID == "" {
|
||||
deliveryID = DeliveryID(request.NoticeID, request.AccountID)
|
||||
}
|
||||
if deliveryID == "" {
|
||||
return Delivery{}, false, ErrInvalidNoticeRequest
|
||||
}
|
||||
lookupKey := sqlLookupKey(acknowledgeLookupKey(deliveryID, request.IdempotencyKey))
|
||||
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
if knownDeliveryID, ok, err := s.lookupAcknowledgeKey(ctx, tx, lookupKey); err != nil {
|
||||
return Delivery{}, false, err
|
||||
} else if ok {
|
||||
delivery, ok, err := s.getDelivery(ctx, tx, knownDeliveryID)
|
||||
if err != nil {
|
||||
return Delivery{}, true, err
|
||||
}
|
||||
if !ok {
|
||||
return Delivery{}, true, ErrDeliveryNotFound
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return Delivery{}, true, err
|
||||
}
|
||||
committed = true
|
||||
return cloneDelivery(delivery), true, nil
|
||||
}
|
||||
delivery, ok, err := s.getDelivery(ctx, tx, deliveryID)
|
||||
if err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
if !ok {
|
||||
return Delivery{}, false, ErrDeliveryNotFound
|
||||
}
|
||||
if delivery.Status == StatusDeleted {
|
||||
return Delivery{}, false, ErrDeliveryNotFound
|
||||
}
|
||||
duplicate := false
|
||||
if delivery.Status == StatusAcknowledged {
|
||||
duplicate = true
|
||||
} else {
|
||||
delivery.Status = StatusAcknowledged
|
||||
delivery.AcknowledgedAt = now
|
||||
if err := s.updateDeliveryAck(ctx, tx, delivery); err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
}
|
||||
if err := s.insertAcknowledgeKey(ctx, tx, lookupKey, delivery.ID, request.IdempotencyKey, now); err != nil && !isNoticeSQLDuplicate(err) {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
if recovered, ok := s.recoverAckCommit(ctx, lookupKey, delivery.ID); ok {
|
||||
committed = true
|
||||
return recovered, duplicate, nil
|
||||
}
|
||||
return Delivery{}, duplicate, err
|
||||
}
|
||||
committed = true
|
||||
return cloneDelivery(delivery), duplicate, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) Delete(ctx context.Context, request DeleteRequest) (DeleteResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return DeleteResult{}, ErrNoticeSQLDBRequired
|
||||
}
|
||||
request = normDeleteReq(request)
|
||||
if len(request.DeliveryIDs) == 0 {
|
||||
return DeleteResult{}, nil
|
||||
}
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
committed := false
|
||||
defer func() {
|
||||
if !committed {
|
||||
_ = tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
result := DeleteResult{}
|
||||
for _, id := range request.DeliveryIDs {
|
||||
delivery, ok, err := s.getDelivery(ctx, tx, id)
|
||||
if err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
if !ok || delivery.Status == StatusDeleted || (request.AccountID != "" && delivery.AccountID != request.AccountID) {
|
||||
result.MissingIDs = append(result.MissingIDs, id)
|
||||
continue
|
||||
}
|
||||
delivery.Status = StatusDeleted
|
||||
if err := s.updateDeliveryStatus(ctx, tx, delivery.ID, StatusDeleted); err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
result.DeletedIDs = append(result.DeletedIDs, id)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return DeleteResult{}, err
|
||||
}
|
||||
committed = true
|
||||
sort.Strings(result.DeletedIDs)
|
||||
sort.Strings(result.MissingIDs)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) ListForAccount(ctx context.Context, query Query) ([]Delivery, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
query.AccountID = strings.TrimSpace(query.AccountID)
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
if query.AccountID == "" {
|
||||
return nil, ErrRecipientRequired
|
||||
}
|
||||
now := normalizeNow(query.Now)
|
||||
rows, err := s.db.QueryContext(ctx, "SELECT "+deliveryColumns()+" FROM "+quoteSQLIdentifier(s.deliveries)+" WHERE account_id = ? AND status <> ? ORDER BY created_at ASC, id ASC", query.AccountID, StatusDeleted)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deliveries, err := scanSQLDeliveries(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Delivery, 0, len(deliveries))
|
||||
for _, delivery := range deliveries {
|
||||
notice, ok, err := s.getNotice(ctx, s.db, delivery.NoticeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !ok || !noticeVisible(notice, query.ShardID, now) {
|
||||
continue
|
||||
}
|
||||
out = append(out, cloneDelivery(delivery))
|
||||
}
|
||||
sortAcctDeliveries(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) ActiveAnnouncements(ctx context.Context, query Query) ([]Notice, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return nil, ErrNoticeSQLDBRequired
|
||||
}
|
||||
query.ShardID = strings.TrimSpace(query.ShardID)
|
||||
now := normalizeNow(query.Now)
|
||||
rows, err := s.db.QueryContext(ctx, "SELECT "+noticeColumns()+" FROM "+quoteSQLIdentifier(s.notices)+" WHERE kind = ? ORDER BY id ASC", KindAnnouncement)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
notices, err := scanSQLNotices(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]Notice, 0, len(notices))
|
||||
for _, notice := range notices {
|
||||
if noticeVisible(notice, query.ShardID, now) {
|
||||
out = append(out, cloneNotice(notice))
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].ID < out[j].ID })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
ctx = sqlContext(ctx)
|
||||
if s == nil || s.db == nil {
|
||||
return Snapshot{}, ErrNoticeSQLDBRequired
|
||||
}
|
||||
notices, err := s.queryStringColumn(ctx, "SELECT kind FROM "+quoteSQLIdentifier(s.notices))
|
||||
if err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
statuses, err := s.queryStringColumn(ctx, "SELECT status FROM "+quoteSQLIdentifier(s.deliveries))
|
||||
if err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
publishKeys, err := s.queryStringColumn(ctx, "SELECT lookup_key FROM "+quoteSQLIdentifier(s.publish))
|
||||
if err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
ackKeys, err := s.queryStringColumn(ctx, "SELECT lookup_key FROM "+quoteSQLIdentifier(s.ack))
|
||||
if err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
snapshot := Snapshot{
|
||||
Notices: len(notices),
|
||||
Deliveries: len(statuses),
|
||||
ByKind: make(map[string]int),
|
||||
ByStatus: make(map[string]int),
|
||||
PublishKeys: len(publishKeys),
|
||||
AcknowledgeKeys: len(ackKeys),
|
||||
}
|
||||
for _, kind := range notices {
|
||||
snapshot.ByKind[kind]++
|
||||
}
|
||||
for _, status := range statuses {
|
||||
snapshot.ByStatus[status]++
|
||||
}
|
||||
if len(snapshot.ByKind) == 0 {
|
||||
snapshot.ByKind = nil
|
||||
}
|
||||
if len(snapshot.ByStatus) == 0 {
|
||||
snapshot.ByStatus = nil
|
||||
}
|
||||
return snapshot, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) publishNoticeResult(ctx context.Context, db sqlNoticeQuerier, noticeID string, idempotencyKey string, duplicate bool) (PublishResult, error) {
|
||||
notice, publishedAt, ok, err := s.getNoticePubAt(ctx, db, noticeID)
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
if !ok {
|
||||
return PublishResult{}, ErrNoticeNotFound
|
||||
}
|
||||
deliveries, err := s.deliveriesForNotice(ctx, db, noticeID)
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
meta, err := s.publishMeta(ctx, db, sqlLookupKey(publishLookupKey(PublishRequest{Notice: notice, IdempotencyKey: idempotencyKey})))
|
||||
if err != nil {
|
||||
return PublishResult{}, err
|
||||
}
|
||||
result := PublishResult{
|
||||
Accepted: true,
|
||||
Duplicate: duplicate,
|
||||
Notice: notice,
|
||||
Deliveries: deliveries,
|
||||
IdempotencyKey: idempotencyKey,
|
||||
AdminReceiptID: meta["admin_receipt_id"],
|
||||
Meta: meta,
|
||||
PublishedAt: publishedAt,
|
||||
}
|
||||
return clonePublishResult(result), nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) recoverPublishCommit(parent context.Context, request PublishRequest) (PublishResult, bool) {
|
||||
ctx, cancel := sqlRecoveryCtx(parent)
|
||||
defer cancel()
|
||||
lookupKey := sqlLookupKey(publishLookupKey(request))
|
||||
existingNoticeID, ok, err := s.lookupPublishKey(ctx, s.db, lookupKey)
|
||||
if err != nil || !ok {
|
||||
return PublishResult{}, false
|
||||
}
|
||||
result, err := s.publishNoticeResult(ctx, s.db, existingNoticeID, request.IdempotencyKey, false)
|
||||
if err != nil || publishReplayClash(result, request) {
|
||||
return PublishResult{}, false
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
func (s *SQLStore) recoverAckCommit(parent context.Context, lookupKey string, deliveryID string) (Delivery, bool) {
|
||||
ctx, cancel := sqlRecoveryCtx(parent)
|
||||
defer cancel()
|
||||
knownDeliveryID, ok, err := s.lookupAcknowledgeKey(ctx, s.db, lookupKey)
|
||||
if err != nil || !ok || knownDeliveryID != deliveryID {
|
||||
return Delivery{}, false
|
||||
}
|
||||
delivery, ok, err := s.getDelivery(ctx, s.db, knownDeliveryID)
|
||||
if err != nil || !ok || delivery.Status == StatusDeleted {
|
||||
return Delivery{}, false
|
||||
}
|
||||
return cloneDelivery(delivery), true
|
||||
}
|
||||
@@ -0,0 +1,513 @@
|
||||
package notice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
mysql "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
type sqlNoticeQuerier interface {
|
||||
QueryContext(context.Context, string, ...any) (SQLRows, error)
|
||||
}
|
||||
|
||||
type sqlNoticeExecutor interface {
|
||||
sqlNoticeQuerier
|
||||
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
||||
}
|
||||
|
||||
func (s *SQLStore) lookupPublishKey(ctx context.Context, db sqlNoticeQuerier, lookupKey string) (string, bool, error) {
|
||||
return s.lookupKey(ctx, db, "SELECT notice_id FROM "+quoteSQLIdentifier(s.publish)+" WHERE lookup_key = ? LIMIT 1", lookupKey)
|
||||
}
|
||||
|
||||
func (s *SQLStore) lookupAcknowledgeKey(ctx context.Context, db sqlNoticeQuerier, lookupKey string) (string, bool, error) {
|
||||
return s.lookupKey(ctx, db, "SELECT delivery_id FROM "+quoteSQLIdentifier(s.ack)+" WHERE lookup_key = ? LIMIT 1", lookupKey)
|
||||
}
|
||||
|
||||
func (s *SQLStore) lookupKey(ctx context.Context, db sqlNoticeQuerier, query string, lookupKey string) (string, bool, error) {
|
||||
rows, err := db.QueryContext(ctx, query, lookupKey)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
values, err := scanSQLStrings(rows)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return "", false, nil
|
||||
}
|
||||
return values[0], true, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) getNotice(ctx context.Context, db sqlNoticeQuerier, id string) (Notice, bool, error) {
|
||||
notice, _, ok, err := s.getNoticePubAt(ctx, db, id)
|
||||
return notice, ok, err
|
||||
}
|
||||
|
||||
func (s *SQLStore) getNoticePubAt(ctx context.Context, db sqlNoticeQuerier, id string) (Notice, time.Time, bool, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT "+noticeColumns()+" FROM "+quoteSQLIdentifier(s.notices)+" WHERE id = ? LIMIT 1", id)
|
||||
if err != nil {
|
||||
return Notice{}, time.Time{}, false, err
|
||||
}
|
||||
notices, publishedAt, err := scanSQLPubNotices(rows)
|
||||
if err != nil {
|
||||
return Notice{}, time.Time{}, false, err
|
||||
}
|
||||
if len(notices) == 0 {
|
||||
return Notice{}, time.Time{}, false, nil
|
||||
}
|
||||
return cloneNotice(notices[0]), publishedAt[0], true, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) getDelivery(ctx context.Context, db sqlNoticeQuerier, id string) (Delivery, bool, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT "+deliveryColumns()+" FROM "+quoteSQLIdentifier(s.deliveries)+" WHERE id = ? LIMIT 1", id)
|
||||
if err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
deliveries, err := scanSQLDeliveries(rows)
|
||||
if err != nil {
|
||||
return Delivery{}, false, err
|
||||
}
|
||||
if len(deliveries) == 0 {
|
||||
return Delivery{}, false, nil
|
||||
}
|
||||
return cloneDelivery(deliveries[0]), true, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) deliveriesForNotice(ctx context.Context, db sqlNoticeQuerier, noticeID string) ([]Delivery, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT "+deliveryColumns()+" FROM "+quoteSQLIdentifier(s.deliveries)+" WHERE notice_id = ? ORDER BY account_id ASC, id ASC", noticeID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deliveries, err := scanSQLDeliveries(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sortDeliveries(deliveries)
|
||||
return deliveries, nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) publishMeta(ctx context.Context, db sqlNoticeQuerier, lookupKey string) (map[string]string, error) {
|
||||
rows, err := db.QueryContext(ctx, "SELECT meta_json FROM "+quoteSQLIdentifier(s.publish)+" WHERE lookup_key = ? LIMIT 1", lookupKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values, err := scanSQLNullStrings(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return decodeSQLStringMap(values[0])
|
||||
}
|
||||
|
||||
func (s *SQLStore) insertNotice(ctx context.Context, db sqlNoticeExecutor, notice Notice, publishedAt time.Time) error {
|
||||
meta, err := encodeSQLStringMap(notice.Meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteSQLIdentifier(s.notices)+" (id, kind, scope, shard_id, title, body, attachment_ref, starts_at, ends_at, disabled, meta_json, published_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
notice.ID,
|
||||
notice.Kind,
|
||||
notice.Scope,
|
||||
notice.ShardID,
|
||||
notice.Title,
|
||||
nullableSQLString(notice.Body),
|
||||
notice.AttachmentRef,
|
||||
nullableSQLTime(notice.StartsAt),
|
||||
nullableSQLTime(notice.EndsAt),
|
||||
notice.Disabled,
|
||||
meta,
|
||||
publishedAt.UTC(),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLStore) insertDelivery(ctx context.Context, db sqlNoticeExecutor, delivery Delivery) error {
|
||||
meta, err := encodeSQLStringMap(delivery.Meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteSQLIdentifier(s.deliveries)+" (id, notice_id, account_id, shard_id, status, attachment_ref, idempotency_key, meta_json, created_at, acknowledged_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
delivery.ID,
|
||||
delivery.NoticeID,
|
||||
delivery.AccountID,
|
||||
delivery.ShardID,
|
||||
delivery.Status,
|
||||
delivery.AttachmentRef,
|
||||
delivery.IdempotencyKey,
|
||||
meta,
|
||||
delivery.CreatedAt.UTC(),
|
||||
nullableSQLTime(delivery.AcknowledgedAt),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLStore) insertPublishKey(ctx context.Context, db sqlNoticeExecutor, lookupKey string, noticeID string, idempotencyKey string, meta map[string]string, createdAt time.Time) error {
|
||||
metaJSON, err := encodeSQLStringMap(meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteSQLIdentifier(s.publish)+" (lookup_key, notice_id, idempotency_key, meta_json, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
lookupKey,
|
||||
noticeID,
|
||||
idempotencyKey,
|
||||
metaJSON,
|
||||
createdAt.UTC(),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLStore) insertAcknowledgeKey(ctx context.Context, db sqlNoticeExecutor, lookupKey string, deliveryID string, idempotencyKey string, createdAt time.Time) error {
|
||||
_, err := db.ExecContext(ctx,
|
||||
"INSERT INTO "+quoteSQLIdentifier(s.ack)+" (lookup_key, delivery_id, idempotency_key, created_at) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE delivery_id = delivery_id",
|
||||
lookupKey,
|
||||
deliveryID,
|
||||
idempotencyKey,
|
||||
createdAt.UTC(),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *SQLStore) updateDeliveryAck(ctx context.Context, db sqlNoticeExecutor, delivery Delivery) error {
|
||||
result, err := db.ExecContext(ctx,
|
||||
"UPDATE "+quoteSQLIdentifier(s.deliveries)+" SET status = ?, acknowledged_at = ? WHERE id = ?",
|
||||
delivery.Status,
|
||||
nullableSQLTime(delivery.AcknowledgedAt),
|
||||
delivery.ID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrDeliveryNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) updateDeliveryStatus(ctx context.Context, db sqlNoticeExecutor, deliveryID string, status string) error {
|
||||
result, err := db.ExecContext(ctx,
|
||||
"UPDATE "+quoteSQLIdentifier(s.deliveries)+" SET status = ? WHERE id = ?",
|
||||
status,
|
||||
deliveryID,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if result == nil {
|
||||
return nil
|
||||
}
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if affected == 0 {
|
||||
return ErrDeliveryNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SQLStore) queryStringColumn(ctx context.Context, query string, args ...any) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scanSQLStrings(rows)
|
||||
}
|
||||
|
||||
func scanSQLNotices(rows SQLRows) ([]Notice, error) {
|
||||
notices, _, err := scanSQLPubNotices(rows)
|
||||
return notices, err
|
||||
}
|
||||
|
||||
func scanSQLPubNotices(rows SQLRows) (notices []Notice, published []time.Time, err error) {
|
||||
if rows == nil {
|
||||
return nil, nil, ErrNoticeInvalidRows
|
||||
}
|
||||
defer closeNoticeRowsErr(rows, &err)
|
||||
for rows.Next() {
|
||||
notice, publishedAt, err := scanSQLNotice(rows)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
notices = append(notices, notice)
|
||||
published = append(published, publishedAt)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return notices, published, nil
|
||||
}
|
||||
|
||||
func scanSQLNotice(rows SQLRows) (Notice, time.Time, error) {
|
||||
var notice Notice
|
||||
var scope, shardID, body, attachmentRef, meta sql.NullString
|
||||
var startsAt, endsAt sql.NullTime
|
||||
var publishedAt time.Time
|
||||
if err := rows.Scan(
|
||||
¬ice.ID,
|
||||
¬ice.Kind,
|
||||
&scope,
|
||||
&shardID,
|
||||
¬ice.Title,
|
||||
&body,
|
||||
&attachmentRef,
|
||||
&startsAt,
|
||||
&endsAt,
|
||||
¬ice.Disabled,
|
||||
&meta,
|
||||
&publishedAt,
|
||||
); err != nil {
|
||||
return Notice{}, time.Time{}, err
|
||||
}
|
||||
if scope.Valid {
|
||||
notice.Scope = scope.String
|
||||
}
|
||||
if shardID.Valid {
|
||||
notice.ShardID = shardID.String
|
||||
}
|
||||
if body.Valid {
|
||||
notice.Body = body.String
|
||||
}
|
||||
if attachmentRef.Valid {
|
||||
notice.AttachmentRef = attachmentRef.String
|
||||
}
|
||||
if startsAt.Valid {
|
||||
notice.StartsAt = startsAt.Time.UTC()
|
||||
}
|
||||
if endsAt.Valid {
|
||||
notice.EndsAt = endsAt.Time.UTC()
|
||||
}
|
||||
if meta.Valid {
|
||||
decoded, err := decodeSQLStringMap(meta.String)
|
||||
if err != nil {
|
||||
return Notice{}, time.Time{}, err
|
||||
}
|
||||
notice.Meta = decoded
|
||||
}
|
||||
return cloneNotice(notice), publishedAt.UTC(), nil
|
||||
}
|
||||
|
||||
func scanSQLDeliveries(rows SQLRows) (deliveries []Delivery, err error) {
|
||||
if rows == nil {
|
||||
return nil, ErrNoticeInvalidRows
|
||||
}
|
||||
defer closeNoticeRowsErr(rows, &err)
|
||||
for rows.Next() {
|
||||
delivery, err := scanSQLDelivery(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deliveries = append(deliveries, delivery)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deliveries, nil
|
||||
}
|
||||
|
||||
func scanSQLDelivery(rows SQLRows) (Delivery, error) {
|
||||
var delivery Delivery
|
||||
var shardID, attachmentRef, idempotencyKey, meta sql.NullString
|
||||
var acknowledgedAt sql.NullTime
|
||||
if err := rows.Scan(
|
||||
&delivery.ID,
|
||||
&delivery.NoticeID,
|
||||
&delivery.AccountID,
|
||||
&shardID,
|
||||
&delivery.Status,
|
||||
&attachmentRef,
|
||||
&idempotencyKey,
|
||||
&meta,
|
||||
&delivery.CreatedAt,
|
||||
&acknowledgedAt,
|
||||
); err != nil {
|
||||
return Delivery{}, err
|
||||
}
|
||||
if shardID.Valid {
|
||||
delivery.ShardID = shardID.String
|
||||
}
|
||||
if attachmentRef.Valid {
|
||||
delivery.AttachmentRef = attachmentRef.String
|
||||
}
|
||||
if idempotencyKey.Valid {
|
||||
delivery.IdempotencyKey = idempotencyKey.String
|
||||
}
|
||||
if meta.Valid {
|
||||
decoded, err := decodeSQLStringMap(meta.String)
|
||||
if err != nil {
|
||||
return Delivery{}, err
|
||||
}
|
||||
delivery.Meta = decoded
|
||||
}
|
||||
delivery.CreatedAt = delivery.CreatedAt.UTC()
|
||||
if acknowledgedAt.Valid {
|
||||
delivery.AcknowledgedAt = acknowledgedAt.Time.UTC()
|
||||
}
|
||||
return cloneDelivery(delivery), nil
|
||||
}
|
||||
|
||||
func scanSQLStrings(rows SQLRows) ([]string, error) {
|
||||
values, err := scanSQLNullStrings(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
out = append(out, strings.TrimSpace(value))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func scanSQLNullStrings(rows SQLRows) (values []string, err error) {
|
||||
if rows == nil {
|
||||
return nil, ErrNoticeInvalidRows
|
||||
}
|
||||
defer closeNoticeRowsErr(rows, &err)
|
||||
for rows.Next() {
|
||||
var value sql.NullString
|
||||
if err := rows.Scan(&value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if value.Valid {
|
||||
values = append(values, value.String)
|
||||
} else {
|
||||
values = append(values, "")
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// closeNoticeRowsErr 在扫描辅助函数返回前合并 Close 错误,避免查询清理失败被静默吞掉。
|
||||
func closeNoticeRowsErr(rows SQLRows, err *error) {
|
||||
if rows == nil || err == nil {
|
||||
return
|
||||
}
|
||||
if closeErr := rows.Close(); closeErr != nil && *err == nil {
|
||||
*err = closeErr
|
||||
}
|
||||
}
|
||||
|
||||
func noticeColumns() string {
|
||||
return "id, kind, scope, shard_id, title, body, attachment_ref, starts_at, ends_at, disabled, meta_json, published_at"
|
||||
}
|
||||
|
||||
func deliveryColumns() string {
|
||||
return "id, notice_id, account_id, shard_id, status, attachment_ref, idempotency_key, meta_json, created_at, acknowledged_at"
|
||||
}
|
||||
|
||||
func encodeSQLStringMap(values map[string]string) (any, error) {
|
||||
if len(values) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
data, err := json.Marshal(values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
func decodeSQLStringMap(value string) (map[string]string, error) {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return nil, nil
|
||||
}
|
||||
var out map[string]string
|
||||
if err := json.Unmarshal([]byte(value), &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return normalizeStringMap(out), nil
|
||||
}
|
||||
|
||||
func nullableSQLString(value string) any {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func nullableSQLTime(value time.Time) any {
|
||||
if value.IsZero() {
|
||||
return nil
|
||||
}
|
||||
return value.UTC()
|
||||
}
|
||||
|
||||
type sqlStoreTables struct {
|
||||
notices string
|
||||
deliveries string
|
||||
publish string
|
||||
ack string
|
||||
}
|
||||
|
||||
func normSQLStoreTables(options SQLStoreOptions) (sqlStoreTables, error) {
|
||||
prefix := strings.TrimSpace(options.TablePrefix)
|
||||
if prefix == "" {
|
||||
prefix = defSQLTablePrefix
|
||||
}
|
||||
tables := sqlStoreTables{
|
||||
notices: firstNonEmpty(options.NoticeTable, prefix+"_notices"),
|
||||
deliveries: firstNonEmpty(options.DeliveryTable, prefix+"_deliveries"),
|
||||
publish: firstNonEmpty(options.PublishKeyTable, prefix+"_publish_keys"),
|
||||
ack: firstNonEmpty(options.AcknowledgeKeyTable, prefix+"_ack_keys"),
|
||||
}
|
||||
for _, table := range []string{tables.notices, tables.deliveries, tables.publish, tables.ack} {
|
||||
if !noticeTablePattern.MatchString(table) {
|
||||
return sqlStoreTables{}, ErrNoticeInvalidTable
|
||||
}
|
||||
}
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func quoteSQLIdentifier(identifier string) string {
|
||||
return "`" + identifier + "`"
|
||||
}
|
||||
|
||||
func sqlLookupKey(raw string) string {
|
||||
sum := sha256.Sum256([]byte(raw))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func sqlContext(ctx context.Context) context.Context {
|
||||
if ctx == nil {
|
||||
return context.Background()
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func sqlRecoveryCtx(parent context.Context) (context.Context, context.CancelFunc) {
|
||||
base := context.Background()
|
||||
if parent != nil {
|
||||
base = context.WithoutCancel(parent)
|
||||
}
|
||||
return context.WithTimeout(base, rollbackTimeout)
|
||||
}
|
||||
|
||||
func isNoticeSQLDuplicate(err error) bool {
|
||||
var mysqlErr *mysql.MySQLError
|
||||
return errors.As(err, &mysqlErr) && mysqlErr.Number == 1062
|
||||
}
|
||||
|
||||
var noticeTablePattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
@@ -0,0 +1,276 @@
|
||||
package redeem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"longheng.io/server/internal/platform/admincmd"
|
||||
"longheng.io/server/internal/platform/httpx"
|
||||
)
|
||||
|
||||
var ErrAdminMuxRequired = errors.New("redeem admin mux is required")
|
||||
|
||||
const maxAdminBodyBytes = 1 << 20
|
||||
|
||||
type AdminOptions struct {
|
||||
Prefix string
|
||||
Wrap func(http.HandlerFunc) http.HandlerFunc
|
||||
MutateWrap func(string, http.HandlerFunc) http.HandlerFunc
|
||||
CommandHooks AdminCommandHooks
|
||||
}
|
||||
|
||||
type AdminCommandHooks struct {
|
||||
Submit func(*http.Request, string, string, string, any) (admincmd.Receipt, bool, bool, error)
|
||||
MarkSucceeded func(context.Context, admincmd.Receipt) admincmd.Receipt
|
||||
MarkFailed func(context.Context, admincmd.Receipt, error)
|
||||
WriteError func(http.ResponseWriter, error) bool
|
||||
WriteDuplicate func(http.ResponseWriter, admincmd.Receipt)
|
||||
WriteJSONWithReceipt func(http.ResponseWriter, int, map[string]any, admincmd.Receipt)
|
||||
}
|
||||
|
||||
func RegisterAdminRoutes(mux *http.ServeMux, service Service, options AdminOptions) error {
|
||||
if mux == nil {
|
||||
return ErrAdminMuxRequired
|
||||
}
|
||||
prefix := normalizeAdminPrefix(options.Prefix)
|
||||
wrap := options.Wrap
|
||||
if wrap == nil {
|
||||
wrap = func(next http.HandlerFunc) http.HandlerFunc { return next }
|
||||
}
|
||||
mutateWrap := options.MutateWrap
|
||||
if mutateWrap == nil {
|
||||
mutateWrap = func(_ string, next http.HandlerFunc) http.HandlerFunc {
|
||||
return wrap(next)
|
||||
}
|
||||
}
|
||||
mux.HandleFunc(prefix, wrap(SnapshotHandler(service)))
|
||||
mux.HandleFunc(prefix+"/snapshot", wrap(SnapshotHandler(service)))
|
||||
mux.HandleFunc(prefix+"/codes", mutateWrap(httpx.AdminOperationID(http.MethodPost, prefix+"/codes"), PutCodeHandlerWithCommands(service, options.CommandHooks)))
|
||||
mux.HandleFunc(prefix+"/claim", mutateWrap(httpx.AdminOperationID(http.MethodPost, prefix+"/claim"), ClaimHandlerWithCommands(service, options.CommandHooks)))
|
||||
return nil
|
||||
}
|
||||
|
||||
func SnapshotHandler(service Service) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeMethodNA(w, http.MethodGet)
|
||||
return
|
||||
}
|
||||
snapshot, err := service.Snapshot(r.Context())
|
||||
if err != nil {
|
||||
writeRedeemError(w, err)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
func PutCodeHandler(service Service) http.HandlerFunc {
|
||||
return PutCodeHandlerWithCommands(service, AdminCommandHooks{})
|
||||
}
|
||||
|
||||
func PutCodeHandlerWithCommands(service Service, commands AdminCommandHooks) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeMethodNA(w, http.MethodPost)
|
||||
return
|
||||
}
|
||||
var code Code
|
||||
if err := decodeRedeemJSON(w, r, &code); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
receipt, duplicate, submitted, err := submitRedeemCmd(commands, r, "redeem.code.put", firstNonEmpty(code.Code, "code"), "redeem code put", code)
|
||||
if writeRedeemCmdErr(commands, w, err) {
|
||||
return
|
||||
}
|
||||
if submitted && duplicate {
|
||||
// 管理命令已提交过时直接回放 receipt,不再重复写礼包码。
|
||||
writeCmdDuplicate(commands, w, receipt)
|
||||
return
|
||||
}
|
||||
if submitted {
|
||||
code.Meta = mergeStringMap(code.Meta, map[string]string{"admin_receipt_id": receipt.ID})
|
||||
}
|
||||
saved, err := service.PutCode(r.Context(), code)
|
||||
if err != nil {
|
||||
if submitted {
|
||||
markRedeemCmdFail(commands, r.Context(), receipt, err)
|
||||
}
|
||||
writeRedeemError(w, err)
|
||||
return
|
||||
}
|
||||
response := map[string]any{"code": saved}
|
||||
if submitted {
|
||||
receipt = markRedeemCmdOK(commands, r.Context(), receipt)
|
||||
writeRedeemReceipt(commands, w, http.StatusOK, response, receipt)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, response)
|
||||
}
|
||||
}
|
||||
|
||||
func ClaimHandler(service Service) http.HandlerFunc {
|
||||
return ClaimHandlerWithCommands(service, AdminCommandHooks{})
|
||||
}
|
||||
|
||||
func ClaimHandlerWithCommands(service Service, commands AdminCommandHooks) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeMethodNA(w, http.MethodPost)
|
||||
return
|
||||
}
|
||||
var request ClaimRequest
|
||||
if err := decodeRedeemJSON(w, r, &request); err != nil {
|
||||
httpx.WriteJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
request.AdminCommand = nil
|
||||
target := firstNonEmpty(request.AccountID, request.Code, "claim")
|
||||
receipt, duplicateCommand, submitted, err := submitRedeemCmd(commands, r, "redeem.claim.repair", target, "redeem claim repair", request)
|
||||
if writeRedeemCmdErr(commands, w, err) {
|
||||
return
|
||||
}
|
||||
if submitted && duplicateCommand {
|
||||
// 补领/修复类操作必须先挡住重复命令,避免再次消耗礼包码或重复发奖。
|
||||
writeCmdDuplicate(commands, w, receipt)
|
||||
return
|
||||
}
|
||||
if submitted {
|
||||
request.Meta = mergeStringMap(request.Meta, map[string]string{"admin_receipt_id": receipt.ID})
|
||||
}
|
||||
result, err := service.Claim(r.Context(), request)
|
||||
if err != nil {
|
||||
if submitted {
|
||||
markRedeemCmdFail(commands, r.Context(), receipt, err)
|
||||
}
|
||||
writeRedeemError(w, err)
|
||||
return
|
||||
}
|
||||
response := redeemResponseMap(result)
|
||||
if submitted {
|
||||
receipt = markRedeemCmdOK(commands, r.Context(), receipt)
|
||||
writeRedeemReceipt(commands, w, http.StatusOK, response, receipt)
|
||||
return
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
func submitRedeemCmd(commands AdminCommandHooks, r *http.Request, operation, target, reason string, params any) (admincmd.Receipt, bool, bool, error) {
|
||||
if commands.Submit == nil {
|
||||
return admincmd.Receipt{}, false, false, nil
|
||||
}
|
||||
return commands.Submit(r, operation, target, reason, params)
|
||||
}
|
||||
|
||||
func markRedeemCmdOK(commands AdminCommandHooks, ctx context.Context, receipt admincmd.Receipt) admincmd.Receipt {
|
||||
if commands.MarkSucceeded == nil || receipt.ID == "" {
|
||||
return receipt
|
||||
}
|
||||
return commands.MarkSucceeded(ctx, receipt)
|
||||
}
|
||||
|
||||
func markRedeemCmdFail(commands AdminCommandHooks, ctx context.Context, receipt admincmd.Receipt, err error) {
|
||||
if commands.MarkFailed == nil || receipt.ID == "" || err == nil {
|
||||
return
|
||||
}
|
||||
commands.MarkFailed(ctx, receipt, err)
|
||||
}
|
||||
|
||||
func writeRedeemCmdErr(commands AdminCommandHooks, w http.ResponseWriter, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if commands.WriteError != nil {
|
||||
return commands.WriteError(w, err)
|
||||
}
|
||||
writeRedeemError(w, err)
|
||||
return true
|
||||
}
|
||||
|
||||
func writeCmdDuplicate(commands AdminCommandHooks, w http.ResponseWriter, receipt admincmd.Receipt) {
|
||||
if commands.WriteDuplicate != nil {
|
||||
commands.WriteDuplicate(w, receipt)
|
||||
return
|
||||
}
|
||||
if receipt.ID != "" {
|
||||
w.Header().Set(httpx.AdminReceiptHeader, receipt.ID)
|
||||
}
|
||||
httpx.WriteJSON(w, http.StatusOK, map[string]any{
|
||||
"status": "duplicate",
|
||||
"admin_receipt": receipt,
|
||||
})
|
||||
}
|
||||
|
||||
func writeRedeemReceipt(commands AdminCommandHooks, w http.ResponseWriter, status int, response map[string]any, receipt admincmd.Receipt) {
|
||||
if commands.WriteJSONWithReceipt != nil {
|
||||
commands.WriteJSONWithReceipt(w, status, response, receipt)
|
||||
return
|
||||
}
|
||||
if response == nil {
|
||||
response = make(map[string]any)
|
||||
}
|
||||
if receipt.ID != "" {
|
||||
w.Header().Set(httpx.AdminReceiptHeader, receipt.ID)
|
||||
response["admin_receipt"] = receipt
|
||||
}
|
||||
httpx.WriteJSON(w, status, response)
|
||||
}
|
||||
|
||||
func redeemResponseMap(value any) map[string]any {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return map[string]any{"marshal_error": err.Error()}
|
||||
}
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(data, &out); err != nil {
|
||||
return map[string]any{"marshal_error": err.Error()}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func decodeRedeemJSON(w http.ResponseWriter, r *http.Request, out any) error {
|
||||
return httpx.DecodeStrictJSON(w, r, maxAdminBodyBytes, out)
|
||||
}
|
||||
|
||||
func writeRedeemError(w http.ResponseWriter, err error) {
|
||||
status := http.StatusBadRequest
|
||||
switch {
|
||||
case errors.Is(err, ErrStoreRequired):
|
||||
status = http.StatusServiceUnavailable
|
||||
case errors.Is(err, ErrCodeNotFound):
|
||||
status = http.StatusNotFound
|
||||
case errors.Is(err, ErrCodeInactive), errors.Is(err, ErrCodeExhausted), errors.Is(err, ErrAccountLimitExceeded), errors.Is(err, ErrClaimConflict):
|
||||
status = http.StatusConflict
|
||||
case errors.Is(err, admincmd.ErrMissingIdempotencyKey),
|
||||
errors.Is(err, admincmd.ErrMissingConfirmation),
|
||||
errors.Is(err, admincmd.ErrMissingActor),
|
||||
errors.Is(err, admincmd.ErrMissingReason):
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
httpx.WriteJSON(w, status, map[string]string{"error": err.Error()})
|
||||
}
|
||||
|
||||
func writeMethodNA(w http.ResponseWriter, methods ...string) {
|
||||
w.Header().Set("Allow", strings.Join(methods, ", "))
|
||||
httpx.WriteJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
|
||||
}
|
||||
|
||||
func normalizeAdminPrefix(prefix string) string {
|
||||
prefix = strings.TrimSpace(prefix)
|
||||
if prefix == "" {
|
||||
return "/debug/redeem"
|
||||
}
|
||||
if !strings.HasPrefix(prefix, "/") {
|
||||
prefix = "/" + prefix
|
||||
}
|
||||
prefix = strings.TrimRight(prefix, "/")
|
||||
if prefix == "" {
|
||||
return "/debug/redeem"
|
||||
}
|
||||
return prefix
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package redeem
|
||||
|
||||
import "longheng.io/server/internal/platform/runtimeguard"
|
||||
|
||||
func (*MemoryStore) BackendKind() runtimeguard.Backend {
|
||||
return runtimeguard.BackendMemory
|
||||
}
|
||||
|
||||
func (*PersistentStore) BackendKind() runtimeguard.Backend {
|
||||
return runtimeguard.BackendMemory
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package redeem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultClaimEvStream = "redeem.claim"
|
||||
defaultClaimEvType = "redeem.claimed"
|
||||
claimRollbackTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type claimEventPayload struct {
|
||||
ClaimID string `json:"claim_id"`
|
||||
Code string `json:"code"`
|
||||
AccountID string `json:"account_id"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Campaign string `json:"campaign,omitempty"`
|
||||
RewardRef string `json:"reward_ref"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
Uses int `json:"uses"`
|
||||
Remaining int `json:"remaining"`
|
||||
AdminReceiptID string `json:"admin_receipt_id,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
ClaimedAt time.Time `json:"claimed_at"`
|
||||
}
|
||||
|
||||
func (s Service) lookupClaimEvent(ctx context.Context, request ClaimRequest) (ClaimResult, bool, error) {
|
||||
if s.EventLog == nil {
|
||||
return ClaimResult{}, false, nil
|
||||
}
|
||||
event, ok, err := s.EventLog.GetByIdempotencyKey(ctx, claimEventKey(request.Code, request.AccountID, request.IdempotencyKey))
|
||||
if err != nil || !ok {
|
||||
return ClaimResult{}, false, err
|
||||
}
|
||||
result, err := claimResultFromEvent(event)
|
||||
if err != nil {
|
||||
return ClaimResult{}, false, err
|
||||
}
|
||||
if claimReplayConflict(result, request) {
|
||||
return ClaimResult{}, false, ErrClaimConflict
|
||||
}
|
||||
result.Duplicate = true
|
||||
return result, true, nil
|
||||
}
|
||||
|
||||
func (s Service) appendClaimEvent(ctx context.Context, result ClaimResult) error {
|
||||
if s.EventLog == nil {
|
||||
return nil
|
||||
}
|
||||
payload, err := json.Marshal(claimPayloadResult(result))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.EventLog.Append(ctx, eventlog.Event{
|
||||
ID: claimEventID(result.Code, result.AccountID, result.IdempotencyKey),
|
||||
Stream: s.claimEventStream(),
|
||||
Type: s.claimEventType(),
|
||||
AggregateID: result.AccountID,
|
||||
IdempotencyKey: claimEventKey(result.Code, result.AccountID, result.IdempotencyKey),
|
||||
Payload: payload,
|
||||
Headers: map[string]string{
|
||||
"code": result.Code,
|
||||
"account_id": result.AccountID,
|
||||
"campaign": result.Campaign,
|
||||
"reward_ref": result.RewardRef,
|
||||
"remaining": strconv.Itoa(result.Remaining),
|
||||
"admin_receipt_id": result.AdminReceiptID,
|
||||
},
|
||||
})
|
||||
if errors.Is(err, eventlog.ErrIdempotencyConflict) {
|
||||
return ErrClaimConflict
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s Service) claimEventStream() string {
|
||||
if stream := strings.TrimSpace(s.EventLogStream); stream != "" {
|
||||
return stream
|
||||
}
|
||||
return defaultClaimEvStream
|
||||
}
|
||||
|
||||
func (s Service) claimEventType() string {
|
||||
if typ := strings.TrimSpace(s.EventLogType); typ != "" {
|
||||
return typ
|
||||
}
|
||||
return defaultClaimEvType
|
||||
}
|
||||
|
||||
func runClaimRollback(parent context.Context, rollback func(context.Context) error) error {
|
||||
// outbox 写失败可能来自请求超时;回滚不继承取消,但保留 trace/value 方便排障。
|
||||
base := context.Background()
|
||||
if parent != nil {
|
||||
base = context.WithoutCancel(parent)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(base, claimRollbackTimeout)
|
||||
defer cancel()
|
||||
return rollback(ctx)
|
||||
}
|
||||
|
||||
func claimPayloadResult(result ClaimResult) claimEventPayload {
|
||||
return claimEventPayload{
|
||||
ClaimID: result.ClaimID,
|
||||
Code: result.Code,
|
||||
AccountID: result.AccountID,
|
||||
ShardID: result.ShardID,
|
||||
Source: result.Source,
|
||||
Campaign: result.Campaign,
|
||||
RewardRef: result.RewardRef,
|
||||
IdempotencyKey: result.IdempotencyKey,
|
||||
Uses: result.Uses,
|
||||
Remaining: result.Remaining,
|
||||
AdminReceiptID: result.AdminReceiptID,
|
||||
Meta: cloneStringMap(result.Meta),
|
||||
ClaimedAt: result.ClaimedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func claimResultFromEvent(event eventlog.Event) (ClaimResult, error) {
|
||||
var payload claimEventPayload
|
||||
if err := json.Unmarshal(event.Payload, &payload); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
return ClaimResult{
|
||||
Accepted: true,
|
||||
ClaimID: payload.ClaimID,
|
||||
Code: payload.Code,
|
||||
AccountID: payload.AccountID,
|
||||
ShardID: payload.ShardID,
|
||||
Source: payload.Source,
|
||||
Campaign: payload.Campaign,
|
||||
RewardRef: payload.RewardRef,
|
||||
IdempotencyKey: firstNonEmpty(payload.IdempotencyKey, event.IdempotencyKey),
|
||||
Uses: payload.Uses,
|
||||
Remaining: payload.Remaining,
|
||||
AdminReceiptID: payload.AdminReceiptID,
|
||||
Meta: cloneStringMap(payload.Meta),
|
||||
ClaimedAt: payload.ClaimedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func claimEventKey(code, accountID, key string) string {
|
||||
code = strings.TrimSpace(code)
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
key = strings.TrimSpace(key)
|
||||
if code == "" || accountID == "" || key == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join([]string{"redeem", "claim", "v1", encodeEventKeyPart(code), encodeEventKeyPart(accountID), encodeEventKeyPart(key)}, ":")
|
||||
}
|
||||
|
||||
func encodeEventKeyPart(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "_"
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(value))
|
||||
}
|
||||
|
||||
func claimEventID(code, accountID, key string) string {
|
||||
sum := sha256.Sum256([]byte(claimEventKey(code, accountID, key)))
|
||||
return "redeem-claim-" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
@@ -0,0 +1,527 @@
|
||||
package redeem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"longheng.io/server/internal/platform/admincmd"
|
||||
"longheng.io/server/internal/platform/eventlog"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCodeRequired = errors.New("redeem code is required")
|
||||
ErrAccountRequired = errors.New("redeem account is required")
|
||||
ErrStoreRequired = errors.New("redeem store is required")
|
||||
ErrCodeNotFound = errors.New("redeem code not found")
|
||||
ErrCodeInactive = errors.New("redeem code is inactive")
|
||||
ErrCodeExhausted = errors.New("redeem code is exhausted")
|
||||
ErrAccountLimitExceeded = errors.New("redeem account limit exceeded")
|
||||
ErrClaimConflict = errors.New("redeem claim idempotency conflict")
|
||||
ErrInvalidRedeemCode = errors.New("redeem code is invalid")
|
||||
ErrInvalidRedeemOperation = errors.New("redeem operation is invalid")
|
||||
)
|
||||
|
||||
type Code struct {
|
||||
Code string `json:"code"`
|
||||
Campaign string `json:"campaign,omitempty"`
|
||||
RewardRef string `json:"reward_ref"`
|
||||
MaxUses int `json:"max_uses,omitempty"`
|
||||
PerAccountLimit int `json:"per_account_limit,omitempty"`
|
||||
StartsAt time.Time `json:"starts_at,omitempty"`
|
||||
EndsAt time.Time `json:"ends_at,omitempty"`
|
||||
Disabled bool `json:"disabled,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type ClaimRequest struct {
|
||||
Code string `json:"code"`
|
||||
AccountID string `json:"account_id"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
IdempotencyKey string `json:"idempotency_key,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
AdminCommand *admincmd.Command `json:"admin_command,omitempty"`
|
||||
}
|
||||
|
||||
type ClaimResult struct {
|
||||
Accepted bool `json:"accepted"`
|
||||
Duplicate bool `json:"duplicate,omitempty"`
|
||||
ClaimID string `json:"claim_id"`
|
||||
Code string `json:"code"`
|
||||
AccountID string `json:"account_id"`
|
||||
ShardID string `json:"shard_id,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Campaign string `json:"campaign,omitempty"`
|
||||
RewardRef string `json:"reward_ref"`
|
||||
IdempotencyKey string `json:"idempotency_key"`
|
||||
Uses int `json:"uses"`
|
||||
Remaining int `json:"remaining"`
|
||||
AdminReceiptID string `json:"admin_receipt_id,omitempty"`
|
||||
Meta map[string]string `json:"meta,omitempty"`
|
||||
ClaimedAt time.Time `json:"claimed_at"`
|
||||
}
|
||||
|
||||
type Store interface {
|
||||
PutCode(context.Context, Code) (Code, error)
|
||||
Claim(context.Context, ClaimRequest, time.Time) (ClaimResult, error)
|
||||
Snapshot(context.Context) (Snapshot, error)
|
||||
}
|
||||
|
||||
type Snapshot struct {
|
||||
Codes int `json:"codes"`
|
||||
Claims int `json:"claims"`
|
||||
Uses map[string]int `json:"uses,omitempty"`
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
Store Store
|
||||
EventLog *eventlog.Log
|
||||
EventLogStream string
|
||||
EventLogType string
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (s Service) PutCode(ctx context.Context, code Code) (Code, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Code{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return Code{}, ErrStoreRequired
|
||||
}
|
||||
return s.Store.PutCode(ctx, code)
|
||||
}
|
||||
|
||||
func (s Service) Claim(ctx context.Context, request ClaimRequest) (ClaimResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return ClaimResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normClaimReq(request)
|
||||
if request.Code == "" {
|
||||
return ClaimResult{}, ErrCodeRequired
|
||||
}
|
||||
if request.AccountID == "" {
|
||||
return ClaimResult{}, ErrAccountRequired
|
||||
}
|
||||
if request.IdempotencyKey == "" {
|
||||
request.IdempotencyKey = IdempotencyKey(request.Code, request.AccountID)
|
||||
}
|
||||
if request.AdminCommand != nil {
|
||||
// 运营补发礼包码也走危险命令校验,确保有 reason、确认信息和幂等键。
|
||||
command := *request.AdminCommand
|
||||
command.Operation = firstNonEmpty(command.Operation, "redeem.repair")
|
||||
command.Scope = firstNonEmpty(command.Scope, "redeem")
|
||||
command.Target = firstNonEmpty(command.Target, request.AccountID)
|
||||
command.Params = mergeCommandParams(command.Params, request)
|
||||
if err := admincmd.Validate(command, admincmd.DangerousPolicy()); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
receipt, err := admincmd.NewReceipt(command, "accepted", s.now())
|
||||
if err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
request.AdminCommand = &command
|
||||
request.Meta = mergeStringMap(request.Meta, map[string]string{"admin_receipt_id": receipt.ID})
|
||||
}
|
||||
if result, ok, err := s.lookupClaimEvent(ctx, request); err != nil || ok {
|
||||
return result, err
|
||||
}
|
||||
result, err := s.Store.Claim(ctx, request, s.now())
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if err := s.appendClaimEvent(ctx, result); err != nil {
|
||||
return result, errors.Join(err, runClaimRollback(ctx, func(ctx context.Context) error {
|
||||
return s.rollbackClaim(ctx, result)
|
||||
}))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type claimRollbackStore interface {
|
||||
RollbackClaim(context.Context, ClaimResult) error
|
||||
}
|
||||
|
||||
func (s Service) rollbackClaim(ctx context.Context, result ClaimResult) error {
|
||||
if result.Duplicate || s.Store == nil {
|
||||
return nil
|
||||
}
|
||||
store, ok := s.Store.(claimRollbackStore)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return store.RollbackClaim(ctx, result)
|
||||
}
|
||||
|
||||
func (s Service) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
if s.Store == nil {
|
||||
return Snapshot{}, ErrStoreRequired
|
||||
}
|
||||
return s.Store.Snapshot(ctx)
|
||||
}
|
||||
|
||||
func (s Service) now() time.Time {
|
||||
if s.Now != nil {
|
||||
return s.Now().UTC()
|
||||
}
|
||||
return time.Now().UTC()
|
||||
}
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.Mutex
|
||||
codes map[string]Code
|
||||
claimsByKey map[string]ClaimResult
|
||||
claimsByAccount map[string]int
|
||||
uses map[string]int
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
codes: make(map[string]Code),
|
||||
claimsByKey: make(map[string]ClaimResult),
|
||||
claimsByAccount: make(map[string]int),
|
||||
uses: make(map[string]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) PutCode(ctx context.Context, code Code) (Code, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Code{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return Code{}, ErrStoreRequired
|
||||
}
|
||||
code = normalizeCode(code)
|
||||
if code.Code == "" {
|
||||
return Code{}, ErrCodeRequired
|
||||
}
|
||||
if code.RewardRef == "" {
|
||||
return Code{}, fmt.Errorf("%w: reward_ref is required", ErrInvalidRedeemCode)
|
||||
}
|
||||
if code.MaxUses < 0 || code.PerAccountLimit < 0 {
|
||||
return Code{}, fmt.Errorf("%w: limits cannot be negative", ErrInvalidRedeemCode)
|
||||
}
|
||||
if code.PerAccountLimit == 0 {
|
||||
code.PerAccountLimit = 1
|
||||
}
|
||||
if !code.StartsAt.IsZero() && !code.EndsAt.IsZero() && code.EndsAt.Before(code.StartsAt) {
|
||||
return Code{}, fmt.Errorf("%w: ends_at is before starts_at", ErrInvalidRedeemCode)
|
||||
}
|
||||
s.mu.Lock()
|
||||
s.codes[code.Code] = cloneCode(code)
|
||||
s.mu.Unlock()
|
||||
return cloneCode(code), nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Claim(ctx context.Context, request ClaimRequest, now time.Time) (ClaimResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return ClaimResult{}, ErrStoreRequired
|
||||
}
|
||||
request = normClaimReq(request)
|
||||
if request.Code == "" {
|
||||
return ClaimResult{}, ErrCodeRequired
|
||||
}
|
||||
if request.AccountID == "" {
|
||||
return ClaimResult{}, ErrAccountRequired
|
||||
}
|
||||
if request.IdempotencyKey == "" {
|
||||
request.IdempotencyKey = IdempotencyKey(request.Code, request.AccountID)
|
||||
}
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC()
|
||||
} else {
|
||||
now = now.UTC()
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
lookupKey := claimLookupKey(request)
|
||||
if existing, ok := s.claimsByKey[lookupKey]; ok {
|
||||
if claimReplayConflict(existing, request) {
|
||||
return ClaimResult{}, ErrClaimConflict
|
||||
}
|
||||
// 礼包码领取按 code+account+key 重放,防止客户端超时重试重复扣次数。
|
||||
existing.Duplicate = true
|
||||
return cloneClaimResult(existing), nil
|
||||
}
|
||||
code, ok := s.codes[request.Code]
|
||||
if !ok {
|
||||
return ClaimResult{}, ErrCodeNotFound
|
||||
}
|
||||
if !codeActive(code, now) {
|
||||
return ClaimResult{}, ErrCodeInactive
|
||||
}
|
||||
if code.MaxUses > 0 && s.uses[code.Code] >= code.MaxUses {
|
||||
return ClaimResult{}, ErrCodeExhausted
|
||||
}
|
||||
accountKey := code.Code + "\x00" + request.AccountID
|
||||
if s.claimsByAccount[accountKey] >= code.PerAccountLimit {
|
||||
return ClaimResult{}, ErrAccountLimitExceeded
|
||||
}
|
||||
|
||||
// 次数扣减和领取记录在同一把锁内完成;生产存储应使用同等原子写能力。
|
||||
s.uses[code.Code]++
|
||||
s.claimsByAccount[accountKey]++
|
||||
remaining := -1
|
||||
if code.MaxUses > 0 {
|
||||
remaining = code.MaxUses - s.uses[code.Code]
|
||||
}
|
||||
result := ClaimResult{
|
||||
Accepted: true,
|
||||
ClaimID: ClaimID(request.Code, request.AccountID, request.IdempotencyKey),
|
||||
Code: code.Code,
|
||||
AccountID: request.AccountID,
|
||||
ShardID: request.ShardID,
|
||||
Source: request.Source,
|
||||
Campaign: code.Campaign,
|
||||
RewardRef: code.RewardRef,
|
||||
IdempotencyKey: request.IdempotencyKey,
|
||||
Uses: s.uses[code.Code],
|
||||
Remaining: remaining,
|
||||
AdminReceiptID: request.Meta["admin_receipt_id"],
|
||||
Meta: mergeStringMap(code.Meta, request.Meta),
|
||||
ClaimedAt: now,
|
||||
}
|
||||
s.claimsByKey[lookupKey] = cloneClaimResult(result)
|
||||
return cloneClaimResult(result), nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) RollbackClaim(ctx context.Context, result ClaimResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
if result.Code == "" || result.AccountID == "" || result.IdempotencyKey == "" {
|
||||
return nil
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
request := ClaimRequest{Code: result.Code, AccountID: result.AccountID, IdempotencyKey: result.IdempotencyKey}
|
||||
lookupKey := claimLookupKey(request)
|
||||
existing, ok := s.claimsByKey[lookupKey]
|
||||
if !ok || existing.ClaimID != result.ClaimID {
|
||||
return nil
|
||||
}
|
||||
delete(s.claimsByKey, lookupKey)
|
||||
accountKey := result.Code + "\x00" + result.AccountID
|
||||
if s.claimsByAccount[accountKey] <= 1 {
|
||||
delete(s.claimsByAccount, accountKey)
|
||||
} else {
|
||||
s.claimsByAccount[accountKey]--
|
||||
}
|
||||
if s.uses[result.Code] <= 1 {
|
||||
delete(s.uses, result.Code)
|
||||
} else {
|
||||
s.uses[result.Code]--
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Snapshot{}, err
|
||||
}
|
||||
if s == nil {
|
||||
return Snapshot{}, ErrStoreRequired
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return Snapshot{
|
||||
Codes: len(s.codes),
|
||||
Claims: len(s.claimsByKey),
|
||||
Uses: cloneIntMap(s.uses),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeCode(code Code) Code {
|
||||
code.Code = strings.TrimSpace(code.Code)
|
||||
code.Campaign = strings.TrimSpace(code.Campaign)
|
||||
code.RewardRef = strings.TrimSpace(code.RewardRef)
|
||||
code.StartsAt = normalizeTime(code.StartsAt)
|
||||
code.EndsAt = normalizeTime(code.EndsAt)
|
||||
code.Meta = normalizeStringMap(code.Meta)
|
||||
return code
|
||||
}
|
||||
|
||||
func normClaimReq(request ClaimRequest) ClaimRequest {
|
||||
request.Code = strings.TrimSpace(request.Code)
|
||||
request.AccountID = strings.TrimSpace(request.AccountID)
|
||||
request.ShardID = strings.TrimSpace(request.ShardID)
|
||||
request.Source = strings.TrimSpace(request.Source)
|
||||
request.IdempotencyKey = strings.TrimSpace(request.IdempotencyKey)
|
||||
request.Meta = normalizeStringMap(request.Meta)
|
||||
return request
|
||||
}
|
||||
|
||||
func codeActive(code Code, now time.Time) bool {
|
||||
if code.Disabled {
|
||||
return false
|
||||
}
|
||||
if !code.StartsAt.IsZero() && now.Before(code.StartsAt) {
|
||||
return false
|
||||
}
|
||||
if !code.EndsAt.IsZero() && now.After(code.EndsAt) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func IdempotencyKey(code, accountID string) string {
|
||||
code = strings.TrimSpace(code)
|
||||
accountID = strings.TrimSpace(accountID)
|
||||
if code == "" || accountID == "" {
|
||||
return ""
|
||||
}
|
||||
return strings.Join([]string{"redeem", "v1", encodeEventKeyPart(code), encodeEventKeyPart(accountID)}, ":")
|
||||
}
|
||||
|
||||
func ClaimID(code, accountID, key string) string {
|
||||
seed := strings.Join([]string{strings.TrimSpace(code), strings.TrimSpace(accountID), strings.TrimSpace(key)}, "\x00")
|
||||
sum := sha256.Sum256([]byte(seed))
|
||||
return hex.EncodeToString(sum[:16])
|
||||
}
|
||||
|
||||
func claimLookupKey(request ClaimRequest) string {
|
||||
return strings.TrimSpace(request.Code) + "\x00" + strings.TrimSpace(request.AccountID) + "\x00" + strings.TrimSpace(request.IdempotencyKey)
|
||||
}
|
||||
|
||||
func claimReplayConflict(existing ClaimResult, request ClaimRequest) bool {
|
||||
if request.Code != "" && existing.Code != request.Code {
|
||||
return true
|
||||
}
|
||||
if request.AccountID != "" && existing.AccountID != request.AccountID {
|
||||
return true
|
||||
}
|
||||
if request.ShardID != "" && existing.ShardID != request.ShardID {
|
||||
return true
|
||||
}
|
||||
if request.Source != "" && existing.Source != request.Source {
|
||||
return true
|
||||
}
|
||||
if len(request.Meta) > 0 {
|
||||
for key, value := range request.Meta {
|
||||
if key == "admin_receipt_id" {
|
||||
continue
|
||||
}
|
||||
if existing.Meta[key] != value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func mergeCommandParams(params map[string]any, request ClaimRequest) map[string]any {
|
||||
out := make(map[string]any, len(params)+4)
|
||||
for key, value := range params {
|
||||
out[key] = value
|
||||
}
|
||||
out["code"] = request.Code
|
||||
out["account_id"] = request.AccountID
|
||||
out["shard_id"] = request.ShardID
|
||||
out["idempotency_key"] = request.IdempotencyKey
|
||||
return out
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
value = strings.TrimSpace(value)
|
||||
if value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func normalizeTime(value time.Time) time.Time {
|
||||
if value.IsZero() {
|
||||
return time.Time{}
|
||||
}
|
||||
return value.UTC()
|
||||
}
|
||||
|
||||
func normalizeStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
key = strings.TrimSpace(key)
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
out[key] = strings.TrimSpace(value)
|
||||
}
|
||||
if len(out) == 0 {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mergeStringMap(base map[string]string, overlays ...map[string]string) map[string]string {
|
||||
out := cloneStringMap(base)
|
||||
for _, overlay := range overlays {
|
||||
for key, value := range overlay {
|
||||
if out == nil {
|
||||
out = make(map[string]string)
|
||||
}
|
||||
out[key] = value
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneCode(code Code) Code {
|
||||
code.Meta = cloneStringMap(code.Meta)
|
||||
return code
|
||||
}
|
||||
|
||||
func cloneClaimResult(result ClaimResult) ClaimResult {
|
||||
result.Meta = cloneStringMap(result.Meta)
|
||||
return result
|
||||
}
|
||||
|
||||
func cloneStringMap(values map[string]string) map[string]string {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]string, len(values))
|
||||
for key, value := range values {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneIntMap(values map[string]int) map[string]int {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
out := make(map[string]int, len(values))
|
||||
for key, value := range values {
|
||||
out[key] = value
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func ctxErr(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
package redeem
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
platformdb "longheng.io/server/internal/platform/db"
|
||||
)
|
||||
|
||||
type StateStore interface {
|
||||
Load(context.Context) (State, bool, error)
|
||||
Save(context.Context, State) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type State struct {
|
||||
Codes []Code `json:"codes,omitempty"`
|
||||
Claims []ClaimResult `json:"claims,omitempty"`
|
||||
Uses map[string]int `json:"uses,omitempty"`
|
||||
}
|
||||
|
||||
type PersistentStore struct {
|
||||
mu sync.Mutex
|
||||
// saveMu 串行化完整快照保存,避免慢后端把旧快照写回覆盖新状态。
|
||||
saveMu sync.Mutex
|
||||
memory *MemoryStore
|
||||
store StateStore
|
||||
version uint64
|
||||
}
|
||||
|
||||
func NewPersistentStore(ctx context.Context, store StateStore) (*PersistentStore, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if store == nil {
|
||||
return nil, ErrStoreRequired
|
||||
}
|
||||
memory := NewMemoryStore()
|
||||
if state, ok, err := store.Load(ctx); err != nil {
|
||||
return nil, err
|
||||
} else if ok {
|
||||
memory.importState(state)
|
||||
}
|
||||
return &PersistentStore{memory: memory, store: store}, nil
|
||||
}
|
||||
|
||||
func (s *PersistentStore) PutCode(ctx context.Context, code Code) (Code, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return Code{}, err
|
||||
}
|
||||
if err := s.requireWritable(); err != nil {
|
||||
return Code{}, err
|
||||
}
|
||||
s.saveMu.Lock()
|
||||
s.mu.Lock()
|
||||
before := s.memory.exportState()
|
||||
result, err := s.memory.PutCode(ctx, code)
|
||||
if err != nil {
|
||||
s.mu.Unlock()
|
||||
s.saveMu.Unlock()
|
||||
return result, err
|
||||
}
|
||||
after := s.memory.exportState()
|
||||
s.version++
|
||||
version := s.version
|
||||
s.mu.Unlock()
|
||||
return result, s.persistAfterUnlock(ctx, before, after, version)
|
||||
}
|
||||
|
||||
func (s *PersistentStore) Claim(ctx context.Context, request ClaimRequest, now time.Time) (ClaimResult, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
if err := s.requireWritable(); err != nil {
|
||||
return ClaimResult{}, err
|
||||
}
|
||||
s.saveMu.Lock()
|
||||
s.mu.Lock()
|
||||
before := s.memory.exportState()
|
||||
result, err := s.memory.Claim(ctx, request, now)
|
||||
if err != nil || result.Duplicate {
|
||||
s.mu.Unlock()
|
||||
s.saveMu.Unlock()
|
||||
return result, err
|
||||
}
|
||||
after := s.memory.exportState()
|
||||
s.version++
|
||||
version := s.version
|
||||
s.mu.Unlock()
|
||||
return result, s.persistAfterUnlock(ctx, before, after, version)
|
||||
}
|
||||
|
||||
func (s *PersistentStore) RollbackClaim(ctx context.Context, result ClaimResult) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.memory == nil || s.store == nil {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
s.saveMu.Lock()
|
||||
s.mu.Lock()
|
||||
before := s.memory.exportState()
|
||||
if err := s.memory.RollbackClaim(ctx, result); err != nil {
|
||||
s.mu.Unlock()
|
||||
s.saveMu.Unlock()
|
||||
return err
|
||||
}
|
||||
after := s.memory.exportState()
|
||||
s.version++
|
||||
version := s.version
|
||||
s.mu.Unlock()
|
||||
return s.persistAfterUnlock(ctx, before, after, version)
|
||||
}
|
||||
|
||||
func (s *PersistentStore) Snapshot(ctx context.Context) (Snapshot, error) {
|
||||
if s == nil || s.memory == nil {
|
||||
return Snapshot{}, ErrStoreRequired
|
||||
}
|
||||
return s.memory.Snapshot(ctx)
|
||||
}
|
||||
|
||||
func (s *PersistentStore) Close() error {
|
||||
if s == nil || s.store == nil {
|
||||
return nil
|
||||
}
|
||||
s.saveMu.Lock()
|
||||
defer s.saveMu.Unlock()
|
||||
return s.store.Close()
|
||||
}
|
||||
|
||||
func (s *PersistentStore) requireWritable() error {
|
||||
if s == nil || s.memory == nil || s.store == nil {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PersistentStore) persistAfterUnlock(ctx context.Context, before, after State, version uint64) error {
|
||||
if s == nil || s.memory == nil || s.store == nil {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
defer s.saveMu.Unlock()
|
||||
// 礼包码扣次属于关键权益,仍需同步 durable;慢 I/O 不能占住内存状态锁。
|
||||
if saveErr := s.store.Save(ctx, after); saveErr != nil {
|
||||
s.mu.Lock()
|
||||
// 只回滚发起本次保存的版本,避免失败的旧保存覆盖后续成功领取或补码。
|
||||
if s.version == version {
|
||||
s.memory.importState(before)
|
||||
s.version++
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return saveErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type JSONFileStateStore struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func NewJSONFileStateStore(path string) *JSONFileStateStore {
|
||||
return &JSONFileStateStore{Path: path}
|
||||
}
|
||||
|
||||
func (s *JSONFileStateStore) Load(ctx context.Context) (State, bool, error) {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return State{}, false, err
|
||||
}
|
||||
if s == nil || s.Path == "" {
|
||||
return State{}, false, ErrStoreRequired
|
||||
}
|
||||
data, err := os.ReadFile(s.Path)
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return State{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return State{}, false, err
|
||||
}
|
||||
var state State
|
||||
if err := json.Unmarshal(data, &state); err != nil {
|
||||
return State{}, false, err
|
||||
}
|
||||
return state, true, nil
|
||||
}
|
||||
|
||||
func (s *JSONFileStateStore) Save(ctx context.Context, state State) error {
|
||||
if err := ctxErr(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s == nil || s.Path == "" {
|
||||
return ErrStoreRequired
|
||||
}
|
||||
data, err := json.MarshalIndent(state, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return platformdb.WriteFileAtomically(ctx, s.Path, data, 0o600)
|
||||
}
|
||||
|
||||
func (s *JSONFileStateStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) exportState() State {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
codes := make([]Code, 0, len(s.codes))
|
||||
for _, code := range s.codes {
|
||||
codes = append(codes, cloneCode(code))
|
||||
}
|
||||
sort.Slice(codes, func(i, j int) bool { return codes[i].Code < codes[j].Code })
|
||||
claims := make([]ClaimResult, 0, len(s.claimsByKey))
|
||||
for _, claim := range s.claimsByKey {
|
||||
claim = cloneClaimResult(claim)
|
||||
claim.Duplicate = false
|
||||
claims = append(claims, claim)
|
||||
}
|
||||
sort.Slice(claims, func(i, j int) bool {
|
||||
if claims[i].Code != claims[j].Code {
|
||||
return claims[i].Code < claims[j].Code
|
||||
}
|
||||
if claims[i].AccountID != claims[j].AccountID {
|
||||
return claims[i].AccountID < claims[j].AccountID
|
||||
}
|
||||
return claims[i].IdempotencyKey < claims[j].IdempotencyKey
|
||||
})
|
||||
return State{Codes: codes, Claims: claims, Uses: cloneIntMap(s.uses)}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) importState(state State) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.codes = make(map[string]Code, len(state.Codes))
|
||||
s.claimsByKey = make(map[string]ClaimResult, len(state.Claims))
|
||||
s.claimsByAccount = make(map[string]int, len(state.Claims))
|
||||
s.uses = cloneIntMap(state.Uses)
|
||||
if s.uses == nil {
|
||||
s.uses = make(map[string]int)
|
||||
}
|
||||
replayedUses := make(map[string]int)
|
||||
for _, code := range state.Codes {
|
||||
code = normalizeCode(code)
|
||||
if code.Code != "" {
|
||||
s.codes[code.Code] = cloneCode(code)
|
||||
}
|
||||
}
|
||||
for _, claim := range state.Claims {
|
||||
claim = cloneClaimResult(claim)
|
||||
claim.Code = strings.TrimSpace(claim.Code)
|
||||
claim.AccountID = strings.TrimSpace(claim.AccountID)
|
||||
claim.IdempotencyKey = strings.TrimSpace(claim.IdempotencyKey)
|
||||
if claim.Code == "" || claim.AccountID == "" || claim.IdempotencyKey == "" {
|
||||
continue
|
||||
}
|
||||
claim.Duplicate = false
|
||||
request := ClaimRequest{Code: claim.Code, AccountID: claim.AccountID, IdempotencyKey: claim.IdempotencyKey}
|
||||
s.claimsByKey[claimLookupKey(request)] = claim
|
||||
s.claimsByAccount[claim.Code+"\x00"+claim.AccountID]++
|
||||
replayedUses[claim.Code]++
|
||||
}
|
||||
for code, uses := range replayedUses {
|
||||
if uses > s.uses[code] {
|
||||
s.uses[code] = uses
|
||||
}
|
||||
}
|
||||
for _, claim := range state.Claims {
|
||||
if claim.Uses > s.uses[strings.TrimSpace(claim.Code)] {
|
||||
s.uses[strings.TrimSpace(claim.Code)] = claim.Uses
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user