lab2 fix
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
||||
type ChatSource interface {
|
||||
Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error)
|
||||
GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error)
|
||||
ListVersion(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
ListVisible(
|
||||
ctx context.Context,
|
||||
callerID uuid.UUID,
|
||||
@@ -42,20 +43,21 @@ func NewChatRepository(
|
||||
}
|
||||
|
||||
func (r *ChatRepository) Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error) {
|
||||
chat, err := r.source.Create(ctx, draft)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
r.invalidate(ctx, draft.MemberIDs)
|
||||
|
||||
return chat, nil
|
||||
return r.source.Create(ctx, draft)
|
||||
}
|
||||
|
||||
func (r *ChatRepository) GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error) {
|
||||
return r.source.GetVisible(ctx, chatID, callerID)
|
||||
}
|
||||
|
||||
type sourceError struct{ err error }
|
||||
|
||||
func (e sourceError) Error() string { return e.err.Error() }
|
||||
|
||||
func (e sourceError) Unwrap() error { return e.err }
|
||||
|
||||
const cacheOperationTimeout = 500 * time.Millisecond
|
||||
|
||||
func (r *ChatRepository) ListVisible(
|
||||
ctx context.Context,
|
||||
callerID uuid.UUID,
|
||||
@@ -66,58 +68,54 @@ func (r *ChatRepository) ListVisible(
|
||||
return r.source.ListVisible(ctx, callerID, after, limit)
|
||||
}
|
||||
|
||||
key := chatListKey(callerID, r.generation(ctx, callerID), limit)
|
||||
version, err := r.source.ListVersion(ctx, callerID)
|
||||
if err != nil {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("chat list version: %w", err)
|
||||
}
|
||||
|
||||
key := chatListKey(callerID, version, limit)
|
||||
|
||||
loader := func(ctx context.Context) ([]byte, error) {
|
||||
page, err := r.source.ListVisible(ctx, callerID, nil, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, sourceError{err}
|
||||
}
|
||||
|
||||
return json.Marshal(newChatPageDTO(page))
|
||||
}
|
||||
|
||||
encoded, err := r.cache.GetOrLoad(ctx, key, r.ttl, loader)
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOperationTimeout)
|
||||
defer cancel()
|
||||
|
||||
encoded, err := r.cache.GetOrLoad(cctx, key, r.ttl, loader)
|
||||
if err != nil {
|
||||
if errors.Is(err, cache.ErrLoadInProgress) {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("load chat list: %w", domain.ErrUnavailable)
|
||||
var sourceErr sourceError
|
||||
switch {
|
||||
case errors.As(err, &sourceErr):
|
||||
return domain.Page[domain.Chat]{}, sourceErr.err
|
||||
case ctx.Err() != nil:
|
||||
return domain.Page[domain.Chat]{}, ctx.Err()
|
||||
}
|
||||
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("load chat list: %w", err)
|
||||
}
|
||||
|
||||
var dto chatPageDTO
|
||||
if err := json.Unmarshal(encoded, &dto); err != nil {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("decode cached chat list: %w", err)
|
||||
}
|
||||
|
||||
return dto.toDomain(), nil
|
||||
}
|
||||
|
||||
func (r *ChatRepository) generation(ctx context.Context, userID uuid.UUID) string {
|
||||
value, err := r.cache.Get(ctx, chatGenKey(userID))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(value)
|
||||
}
|
||||
|
||||
func (r *ChatRepository) invalidate(ctx context.Context, memberIDs []uuid.UUID) {
|
||||
for _, memberID := range memberIDs {
|
||||
if err := r.cache.Set(ctx, chatGenKey(memberID), []byte(uuid.NewString()), 0); err != nil && r.log != nil {
|
||||
r.log.WarnContext(ctx, "chat cache invalidation failed",
|
||||
slog.String("user_id", memberID.String()), slog.Any("error", err))
|
||||
} else {
|
||||
var dto chatPageDTO
|
||||
unmarshalErr := json.Unmarshal(encoded, &dto)
|
||||
if unmarshalErr == nil {
|
||||
return dto.toDomain(), nil
|
||||
}
|
||||
err = unmarshalErr
|
||||
|
||||
_ = r.cache.Delete(ctx, key)
|
||||
}
|
||||
|
||||
if r.log != nil {
|
||||
r.log.WarnContext(ctx, "chat list cache unavailable, falling back to source", slog.Any("error", err))
|
||||
}
|
||||
|
||||
return r.source.ListVisible(ctx, callerID, nil, limit)
|
||||
}
|
||||
|
||||
func chatGenKey(userID uuid.UUID) string {
|
||||
return "chat_gen:" + userID.String()
|
||||
}
|
||||
|
||||
func chatListKey(userID uuid.UUID, generation string, limit int) string {
|
||||
return fmt.Sprintf("chat_list:%s:%s:%d", userID, generation, limit)
|
||||
func chatListKey(userID uuid.UUID, version int64, limit int) string {
|
||||
return fmt.Sprintf("chat_list:%s:%d:%d", userID, version, limit)
|
||||
}
|
||||
|
||||
type chatPageDTO struct {
|
||||
|
||||
Reference in New Issue
Block a user