forked from mtusi2026/mtusi2026lab2
397 lines
9.7 KiB
Go
397 lines
9.7 KiB
Go
package cache
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"golang.org/x/sync/singleflight"
|
|
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/config"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
|
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
|
)
|
|
|
|
const (
|
|
ModeOff = "off"
|
|
ModeNaive = "naive"
|
|
ModeSafe = "safe"
|
|
)
|
|
|
|
type Source interface {
|
|
store.MessageStore
|
|
IsMember(ctx context.Context, chatID, userID string) (bool, error)
|
|
HistoryPage(ctx context.Context, chatID, cursor string, limit int) (domain.Page[domain.Message], error)
|
|
}
|
|
|
|
type Options struct {
|
|
Mode string
|
|
PageTTL time.Duration
|
|
LockTTL time.Duration
|
|
FillDelay time.Duration
|
|
Logger *slog.Logger
|
|
}
|
|
|
|
var _ store.MessageStore = (*Messages)(nil)
|
|
|
|
type Messages struct {
|
|
src Source
|
|
rdb *redis.Client
|
|
opt Options
|
|
group singleflight.Group
|
|
log *slog.Logger
|
|
|
|
downUntil atomic.Int64
|
|
|
|
mu sync.Mutex
|
|
pending map[string]struct{}
|
|
stop chan struct{}
|
|
done chan struct{}
|
|
}
|
|
|
|
const (
|
|
memberTTL = 24 * time.Hour
|
|
versionTTL = 24 * time.Hour
|
|
breakerPause = time.Second
|
|
retryInterval = 200 * time.Millisecond
|
|
pollInterval = 5 * time.Millisecond
|
|
defaultPageTTL = 30 * time.Second
|
|
defaultLockTTL = 500 * time.Millisecond
|
|
)
|
|
|
|
func New(src Source, rdb *redis.Client, opt Options) *Messages {
|
|
if opt.PageTTL <= 0 {
|
|
opt.PageTTL = defaultPageTTL
|
|
}
|
|
if opt.LockTTL <= 0 {
|
|
opt.LockTTL = defaultLockTTL
|
|
}
|
|
if opt.Logger == nil {
|
|
opt.Logger = slog.Default()
|
|
}
|
|
m := &Messages{
|
|
src: src,
|
|
rdb: rdb,
|
|
opt: opt,
|
|
log: opt.Logger,
|
|
pending: make(map[string]struct{}),
|
|
stop: make(chan struct{}),
|
|
done: make(chan struct{}),
|
|
}
|
|
go m.retryLoop()
|
|
return m
|
|
}
|
|
|
|
func Connect(cfg config.HistoryCache) (*redis.Client, error) {
|
|
url := cfg.URL
|
|
if cfg.URLFile != "" {
|
|
b, err := os.ReadFile(cfg.URLFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cache: read HISTORY_CACHE_URL_FILE: %w", err)
|
|
}
|
|
url = strings.TrimSpace(string(b))
|
|
}
|
|
if url == "" {
|
|
return nil, errors.New("cache: HISTORY_CACHE_URL_FILE or HISTORY_CACHE_URL is required unless HISTORY_CACHE=off")
|
|
}
|
|
opts, err := redis.ParseURL(url)
|
|
if err != nil {
|
|
return nil, errors.New("cache: invalid HISTORY_CACHE_URL")
|
|
}
|
|
timeout := time.Duration(cfg.TimeoutMs) * time.Millisecond
|
|
opts.PoolSize = cfg.PoolSize
|
|
opts.DialTimeout = timeout
|
|
opts.ReadTimeout = timeout
|
|
opts.WriteTimeout = timeout
|
|
opts.PoolTimeout = timeout
|
|
opts.MaxRetries = -1 // без повторов: промах кэша дешевле ожидания
|
|
return redis.NewClient(opts), nil
|
|
}
|
|
|
|
func (m *Messages) Close() {
|
|
close(m.stop)
|
|
<-m.done
|
|
}
|
|
|
|
func (m *Messages) AddMessage(ctx context.Context, callerID, chatID, text string) (domain.Message, error) {
|
|
msg, err := m.src.AddMessage(ctx, callerID, chatID, text)
|
|
if err != nil {
|
|
return msg, err
|
|
}
|
|
ictx := context.WithoutCancel(ctx)
|
|
switch m.opt.Mode {
|
|
case ModeNaive:
|
|
m.naiveInvalidate(ictx, chatID)
|
|
case ModeSafe:
|
|
if err := m.rdb.Del(ictx, verKey(chatID)).Err(); err != nil {
|
|
m.markDown(err)
|
|
m.addPending(chatID)
|
|
}
|
|
}
|
|
return msg, nil
|
|
}
|
|
|
|
func (m *Messages) ListMessages(ctx context.Context, callerID, chatID, cursor string, limit int) (domain.Page[domain.Message], error) {
|
|
if m.opt.Mode == ModeOff || m.bypass(chatID) {
|
|
return m.src.ListMessages(ctx, callerID, chatID, cursor, limit)
|
|
}
|
|
if err := m.checkMember(ctx, chatID, callerID); err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
if m.opt.Mode == ModeNaive {
|
|
return m.naiveList(ctx, chatID, cursor, limit)
|
|
}
|
|
return m.safeList(ctx, chatID, cursor, limit)
|
|
}
|
|
|
|
func (m *Messages) checkMember(ctx context.Context, chatID, userID string) error {
|
|
key := "hc:member:" + chatID + ":" + userID
|
|
if !m.isDown() {
|
|
v, err := m.rdb.Get(ctx, key).Result()
|
|
if err == nil && v == "1" {
|
|
return nil
|
|
}
|
|
m.markDown(err)
|
|
}
|
|
ok, err := m.src.IsMember(ctx, chatID, userID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
// Отрицательный ответ не кэшируем.
|
|
return domain.ErrNotFound
|
|
}
|
|
if !m.isDown() {
|
|
m.markDown(m.rdb.Set(ctx, key, "1", memberTTL).Err())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Messages) naiveList(ctx context.Context, chatID, cursor string, limit int) (domain.Page[domain.Message], error) {
|
|
key := pageKey("hc:naive:"+chatID, cursor, limit)
|
|
if page, ok := m.getPage(ctx, key); ok {
|
|
return page, nil
|
|
}
|
|
page, err := m.src.HistoryPage(ctx, chatID, cursor, limit)
|
|
if err != nil {
|
|
return page, err
|
|
}
|
|
m.sleepFill(ctx)
|
|
if b, err := encodePage(page); err == nil {
|
|
setKey := "hc:naive:keys:" + chatID
|
|
_, err := m.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
|
p.Set(ctx, key, b, m.opt.PageTTL)
|
|
p.SAdd(ctx, setKey, key)
|
|
p.Expire(ctx, setKey, m.opt.PageTTL)
|
|
return nil
|
|
})
|
|
m.markDown(err)
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
func (m *Messages) naiveInvalidate(ctx context.Context, chatID string) {
|
|
setKey := "hc:naive:keys:" + chatID
|
|
keys, err := m.rdb.SMembers(ctx, setKey).Result()
|
|
if err != nil {
|
|
m.markDown(err)
|
|
return
|
|
}
|
|
m.markDown(m.rdb.Del(ctx, append(keys, setKey)...).Err())
|
|
}
|
|
|
|
func (m *Messages) safeList(ctx context.Context, chatID, cursor string, limit int) (domain.Page[domain.Message], error) {
|
|
ver, err := m.version(ctx, chatID)
|
|
if err != nil {
|
|
m.markDown(err)
|
|
return m.src.HistoryPage(ctx, chatID, cursor, limit)
|
|
}
|
|
key := pageKey("hc:page:"+chatID+":"+ver, cursor, limit)
|
|
if page, ok := m.getPage(ctx, key); ok {
|
|
return page, nil
|
|
}
|
|
v, err, _ := m.group.Do(key, func() (any, error) {
|
|
return m.fill(context.WithoutCancel(ctx), key, chatID, cursor, limit)
|
|
})
|
|
if err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
return v.(domain.Page[domain.Message]), nil
|
|
}
|
|
|
|
func (m *Messages) version(ctx context.Context, chatID string) (string, error) {
|
|
key := verKey(chatID)
|
|
v, err := m.rdb.Get(ctx, key).Result()
|
|
if err == nil {
|
|
return v, nil
|
|
}
|
|
if !errors.Is(err, redis.Nil) {
|
|
return "", err
|
|
}
|
|
fresh := randomVersion()
|
|
old, err := m.rdb.SetArgs(ctx, key, fresh, redis.SetArgs{Mode: "NX", Get: true, TTL: versionTTL}).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
return fresh, nil
|
|
}
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return old, nil
|
|
}
|
|
|
|
var unlockScript = redis.NewScript(`
|
|
if redis.call("GET", KEYS[1]) == ARGV[1] then return redis.call("DEL", KEYS[1]) end
|
|
return 0`)
|
|
|
|
func (m *Messages) fill(ctx context.Context, key, chatID, cursor string, limit int) (domain.Page[domain.Message], error) {
|
|
lockKey := "hc:lock:" + key
|
|
token := randomVersion()
|
|
locked, err := m.rdb.SetNX(ctx, lockKey, token, m.opt.LockTTL).Result()
|
|
if err != nil {
|
|
m.markDown(err)
|
|
return m.src.HistoryPage(ctx, chatID, cursor, limit)
|
|
}
|
|
if !locked {
|
|
deadline := time.Now().Add(m.opt.LockTTL)
|
|
for time.Now().Before(deadline) {
|
|
time.Sleep(pollInterval)
|
|
if page, ok := m.getPage(ctx, key); ok {
|
|
return page, nil
|
|
}
|
|
}
|
|
return m.src.HistoryPage(ctx, chatID, cursor, limit)
|
|
}
|
|
defer unlockScript.Run(ctx, m.rdb, []string{lockKey}, token)
|
|
|
|
page, err := m.src.HistoryPage(ctx, chatID, cursor, limit)
|
|
if err != nil {
|
|
return page, err
|
|
}
|
|
m.sleepFill(ctx)
|
|
if b, err := encodePage(page); err == nil {
|
|
m.markDown(m.rdb.Set(ctx, key, b, m.opt.PageTTL).Err())
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
func (m *Messages) addPending(chatID string) {
|
|
m.mu.Lock()
|
|
m.pending[chatID] = struct{}{}
|
|
m.mu.Unlock()
|
|
m.log.Warn("history cache invalidation deferred", slog.String("chat_id", chatID))
|
|
}
|
|
|
|
func (m *Messages) bypass(chatID string) bool {
|
|
if m.isDown() {
|
|
return true
|
|
}
|
|
m.mu.Lock()
|
|
_, ok := m.pending[chatID]
|
|
m.mu.Unlock()
|
|
return ok
|
|
}
|
|
|
|
func (m *Messages) retryLoop() {
|
|
defer close(m.done)
|
|
t := time.NewTicker(retryInterval)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-m.stop:
|
|
return
|
|
case <-t.C:
|
|
}
|
|
m.mu.Lock()
|
|
chats := make([]string, 0, len(m.pending))
|
|
for id := range m.pending {
|
|
chats = append(chats, id)
|
|
}
|
|
m.mu.Unlock()
|
|
for _, id := range chats {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
err := m.rdb.Del(ctx, verKey(id)).Err()
|
|
cancel()
|
|
if err != nil {
|
|
break
|
|
}
|
|
m.mu.Lock()
|
|
delete(m.pending, id)
|
|
m.mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
func (m *Messages) isDown() bool {
|
|
return time.Now().UnixNano() < m.downUntil.Load()
|
|
}
|
|
|
|
func (m *Messages) markDown(err error) {
|
|
if err == nil || errors.Is(err, redis.Nil) {
|
|
return
|
|
}
|
|
if !m.isDown() {
|
|
m.log.Warn("history cache unavailable, bypassing", slog.Any("err", err))
|
|
}
|
|
m.downUntil.Store(time.Now().Add(breakerPause).UnixNano())
|
|
}
|
|
|
|
func (m *Messages) getPage(ctx context.Context, key string) (domain.Page[domain.Message], bool) {
|
|
b, err := m.rdb.Get(ctx, key).Bytes()
|
|
if err != nil {
|
|
m.markDown(err)
|
|
return domain.Page[domain.Message]{}, false
|
|
}
|
|
page, err := decodePage(b)
|
|
return page, err == nil
|
|
}
|
|
|
|
func (m *Messages) sleepFill(ctx context.Context) {
|
|
if m.opt.FillDelay <= 0 {
|
|
return
|
|
}
|
|
select {
|
|
case <-time.After(m.opt.FillDelay):
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
|
|
func verKey(chatID string) string { return "hc:ver:" + chatID }
|
|
|
|
func pageKey(prefix, cursor string, limit int) string {
|
|
return prefix + ":" + strconv.Itoa(limit) + ":" + cursor
|
|
}
|
|
|
|
func randomVersion() string {
|
|
var b [8]byte
|
|
_, _ = rand.Read(b[:])
|
|
return hex.EncodeToString(b[:])
|
|
}
|
|
|
|
type cachedPage struct {
|
|
Items []domain.Message `json:"items"`
|
|
NextCursor string `json:"next_cursor"`
|
|
}
|
|
|
|
func encodePage(p domain.Page[domain.Message]) ([]byte, error) {
|
|
return json.Marshal(cachedPage{Items: p.Items, NextCursor: p.NextCursor})
|
|
}
|
|
|
|
func decodePage(b []byte) (domain.Page[domain.Message], error) {
|
|
var c cachedPage
|
|
if err := json.Unmarshal(b, &c); err != nil {
|
|
return domain.Page[domain.Message]{}, err
|
|
}
|
|
return domain.Page[domain.Message]{Items: c.Items, NextCursor: c.NextCursor}, nil
|
|
}
|