Files
mtusi2026lab2/evidence/cache-stampede/singleflight.patch.txt
T
2026-11-06 13:11:04 +03:00

136 lines
3.9 KiB
Plaintext

--- a/service/internal/service/chat/svc.go
+++ b/service/internal/service/chat/svc.go
@@ -11,6 +11,7 @@
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/redis/go-redis/v9"
+ "golang.org/x/sync/singleflight"
"gitea.privcloud.tech/mtusi2026/internal/dto"
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
@@ -18,12 +19,15 @@
)
var _ API = (*Chat)(nil)
+
+const chatLoadTimeout = 2 * time.Second
type Chat struct {
tm *txmanager.TransactionManager
chatsRepo chatsRepo
messagesRepo messagesRepo
chatsCache chatsCache
+ loads singleflight.Group
}
func New(tm *txmanager.TransactionManager, chatsRepo chatsRepo, messagesRepo messagesRepo, chatsCache chatsCache) *Chat {
@@ -72,30 +76,68 @@
}
func (c *Chat) GetForMember(ctx context.Context, req dto.ChatMember) (*dto.Chat, error) {
- chat, err := c.chatsCache.Get(ctx, req.ChatID)
- if err == nil {
- slog.Info("chat_cache_hit", "chat_id", req.ChatID)
- if !slices.Contains(chat.MemberIDs, req.UserID) {
- return nil, apperr.New(apperr.CodeNotFound, "chat not found")
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ if chat, err := c.cachedChat(ctx, req.ChatID); err == nil {
+ return chatForMember(chat, req.UserID)
+ }
+
+ // Repository results depend on membership: never share a user's 404 with
+ // another user requesting the same chat.
+ key := req.ChatID.String() + ":" + req.UserID.String()
+ result := c.loads.DoChan(key, func() (any, error) {
+ // One client disconnecting must not cancel the other clients' load.
+ loadCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), chatLoadTimeout)
+ defer cancel()
+
+ // A previous load may have populated Redis after our initial miss.
+ if chat, err := c.cachedChat(loadCtx, req.ChatID); err == nil {
+ return chatForMember(chat, req.UserID)
+ }
+
+ slog.Info("chat_db_lookup", "chat_id", req.ChatID)
+ chat, err := c.chatsRepo.GetForMember(loadCtx, req)
+ if err != nil {
+ return nil, err
+ }
+ if err = c.chatsCache.Create(loadCtx, *chat); err != nil {
+ slog.Warn("failed to cache chat", "chat_id", req.ChatID, "error", err)
}
return chat, nil
- }
-
- if errors.Is(err, redis.Nil) {
- slog.Info("chat_cache_miss", "chat_id", req.ChatID)
- } else {
- slog.Warn("chat_cache_read_error", "chat_id", req.ChatID, "error", err)
- }
-
- slog.Info("chat_db_lookup", "chat_id", req.ChatID)
- chat, err = c.chatsRepo.GetForMember(ctx, req)
- if err != nil {
- return nil, err
- }
- if err = c.chatsCache.Create(ctx, *chat); err != nil {
- slog.Warn("failed to cache chat", "chat_id", req.ChatID, "error", err)
- }
-
+ })
+
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case loaded := <-result:
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ if loaded.Err != nil {
+ return nil, loaded.Err
+ }
+ return chatForMember(loaded.Val.(*dto.Chat), req.UserID)
+ }
+}
+
+func (c *Chat) cachedChat(ctx context.Context, chatID uuid.UUID) (*dto.Chat, error) {
+ chat, err := c.chatsCache.Get(ctx, chatID)
+ switch {
+ case err == nil:
+ slog.Info("chat_cache_hit", "chat_id", chatID)
+ case errors.Is(err, redis.Nil):
+ slog.Info("chat_cache_miss", "chat_id", chatID)
+ default:
+ slog.Warn("chat_cache_read_error", "chat_id", chatID, "error", err)
+ }
+ return chat, err
+}
+
+func chatForMember(chat *dto.Chat, userID uuid.UUID) (*dto.Chat, error) {
+ if !slices.Contains(chat.MemberIDs, userID) {
+ return nil, apperr.New(apperr.CodeNotFound, "chat not found")
+ }
return chat, nil
}
--- a/service/go.mod
+++ b/service/go.mod
@@ -14,6 +14,7 @@
github.com/spf13/viper v1.21.0
go.uber.org/multierr v1.11.0
golang.org/x/crypto v0.57.0
+ golang.org/x/sync v0.23.0
google.golang.org/grpc v1.85.0
)
@@ -51,7 +52,6 @@
github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
- golang.org/x/sync v0.23.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/text v0.42.0 // indirect
)