Ограничить ожидание зависимостей и отвечать 503 при их недоступности
This commit is contained in:
@@ -4,7 +4,8 @@
|
||||
// greater than the nil UUID, so one query serves the first page and the
|
||||
// following ones, and the condition stays a range the index can scan. Ids
|
||||
// are UUIDv7 and created_at derives from them, so ordering by id is the
|
||||
// contract order (created_at, id).
|
||||
// contract order (created_at, id). Errors pass through postgres.Classify, so
|
||||
// an unreachable database surfaces as unavailability.
|
||||
package store
|
||||
|
||||
import (
|
||||
@@ -17,6 +18,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type Chats struct {
|
||||
@@ -34,7 +36,7 @@ const chatWithMembers = `c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)`
|
||||
|
||||
func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error {
|
||||
return pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`,
|
||||
c.ID, c.Title, c.CreatedAt); err != nil {
|
||||
@@ -46,7 +48,7 @@ func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID)
|
||||
return fmt.Errorf("insert chat members: %w", err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
||||
func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) {
|
||||
@@ -61,7 +63,7 @@ func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (cha
|
||||
return chat.ChatWithMembers{}, chat.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", err)
|
||||
return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", postgres.Classify(err))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
@@ -72,7 +74,7 @@ func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, e
|
||||
`SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
||||
chatID, userID).Scan(&ok)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check membership: %w", err)
|
||||
return false, fmt.Errorf("check membership: %w", postgres.Classify(err))
|
||||
}
|
||||
return ok, nil
|
||||
}
|
||||
@@ -88,13 +90,13 @@ func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.
|
||||
LIMIT $3`,
|
||||
userID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select chats: %w", err)
|
||||
return nil, fmt.Errorf("select chats: %w", postgres.Classify(err))
|
||||
}
|
||||
chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) {
|
||||
return scanChat(row)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read chats: %w", err)
|
||||
return nil, fmt.Errorf("read chats: %w", postgres.Classify(err))
|
||||
}
|
||||
return chats, nil
|
||||
}
|
||||
@@ -123,7 +125,7 @@ func (r *Messages) Append(ctx context.Context, m chat.Message) error {
|
||||
`INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert message: %w", err)
|
||||
return fmt.Errorf("insert message: %w", postgres.Classify(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -140,7 +142,7 @@ func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid
|
||||
LIMIT $3`,
|
||||
chatID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select messages: %w", err)
|
||||
return nil, fmt.Errorf("select messages: %w", postgres.Classify(err))
|
||||
}
|
||||
messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) {
|
||||
var m chat.Message
|
||||
@@ -149,7 +151,7 @@ func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid
|
||||
return m, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read messages: %w", err)
|
||||
return nil, fmt.Errorf("read messages: %w", postgres.Classify(err))
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user