2026-07-12 00:47:20 +08:00

33 lines
1.2 KiB
Go

// Package giftlimits defines the owner-enforced ceiling for one gift command.
// Runtime rate limits may be stricter, but no transport or service is allowed to raise these values
// independently because gift_count fan-out reaches wallet, lucky draw, Room Cell and outbox storage.
package giftlimits
import "strings"
const (
MaxCommandIDBytes = 128
MaxGiftCount = int64(999)
MaxTotalGiftUnits = int64(5_000)
)
// ValidCommandID bounds the financial idempotency key before it reaches Redis/MySQL key material.
func ValidCommandID(commandID string) bool {
commandID = strings.TrimSpace(commandID)
return commandID != "" && len(commandID) <= MaxCommandIDBytes
}
// WorkUnits calculates gift_count*unique_target_count without overflow and applies the cross-service
// hard ceiling. Callers must pass a deduplicated target count; wallet additionally rejects duplicates.
func WorkUnits(giftCount int64, uniqueTargetCount int) (int64, bool) {
if giftCount <= 0 || giftCount > MaxGiftCount || uniqueTargetCount <= 0 {
return 0, false
}
targetCount := int64(uniqueTargetCount)
if targetCount > MaxTotalGiftUnits/giftCount {
return 0, false
}
units := giftCount * targetCount
return units, units <= MaxTotalGiftUnits
}