2025-12-19 23:39:28 +08:00
|
|
|
package ports
|
|
|
|
|
|
|
|
|
|
import "context"
|
|
|
|
|
|
|
|
|
|
// ConcurrencyCache defines cache operations for concurrency service
|
2025-12-24 21:00:29 +08:00
|
|
|
// Uses independent keys per request slot with native Redis TTL for automatic cleanup
|
2025-12-19 23:39:28 +08:00
|
|
|
type ConcurrencyCache interface {
|
2025-12-24 21:00:29 +08:00
|
|
|
// Account slot management - each slot is a separate key with independent TTL
|
|
|
|
|
// Key format: concurrency:account:{accountID}:{requestID}
|
|
|
|
|
AcquireAccountSlot(ctx context.Context, accountID int64, maxConcurrency int, requestID string) (bool, error)
|
|
|
|
|
ReleaseAccountSlot(ctx context.Context, accountID int64, requestID string) error
|
2025-12-19 23:39:28 +08:00
|
|
|
GetAccountConcurrency(ctx context.Context, accountID int64) (int, error)
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
// User slot management - each slot is a separate key with independent TTL
|
|
|
|
|
// Key format: concurrency:user:{userID}:{requestID}
|
|
|
|
|
AcquireUserSlot(ctx context.Context, userID int64, maxConcurrency int, requestID string) (bool, error)
|
|
|
|
|
ReleaseUserSlot(ctx context.Context, userID int64, requestID string) error
|
2025-12-19 23:39:28 +08:00
|
|
|
GetUserConcurrency(ctx context.Context, userID int64) (int, error)
|
|
|
|
|
|
2025-12-24 21:00:29 +08:00
|
|
|
// Wait queue - uses counter with TTL set only on creation
|
2025-12-19 23:39:28 +08:00
|
|
|
IncrementWaitCount(ctx context.Context, userID int64, maxWait int) (bool, error)
|
|
|
|
|
DecrementWaitCount(ctx context.Context, userID int64) error
|
|
|
|
|
}
|