Исправить замечания ревью кода: убрать повтор в AppendOnce за счёт запаса у чистки ключей, вынести MessagePayload и SameContent, принять X-Request-Id от балансировщика, упростить фоновые задачи сервера, сократить комментарии
This commit is contained in:
+24
-18
@@ -12,6 +12,13 @@ import (
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
const (
|
||||
keyPurgeBatch = 1000
|
||||
// keyPurgeGrace keeps a key past its TTL, so a key that a request has just
|
||||
// found valid cannot vanish before the request reads its message back.
|
||||
keyPurgeGrace = time.Minute
|
||||
)
|
||||
|
||||
// UserLookup is what this module needs from the users module: one batched
|
||||
// existence check. It is declared here, on the consumer side, so this
|
||||
// package never imports the users package and tests substitute a map.
|
||||
@@ -26,8 +33,8 @@ type Service struct {
|
||||
keyTTL time.Duration
|
||||
}
|
||||
|
||||
// NewService builds the chats service; keyTTL is how long a message
|
||||
// Idempotency-Key stays bound to its message.
|
||||
// NewService takes keyTTL, how long an Idempotency-Key stays bound to its
|
||||
// message.
|
||||
func NewService(chats ChatRepository, messages MessageRepository, users UserLookup, keyTTL time.Duration) *Service {
|
||||
return &Service{chats: chats, messages: messages, users: users, keyTTL: keyTTL}
|
||||
}
|
||||
@@ -66,38 +73,37 @@ func (s *Service) List(ctx context.Context, actor uuid.UUID, after *uuid.UUID, l
|
||||
return pagination.Cut(items, limit, func(c ChatWithMembers) uuid.UUID { return c.ID }), nil
|
||||
}
|
||||
|
||||
// Send stores a message under the sender's Idempotency-Key. Access and the
|
||||
// text are checked first, on every attempt: a retry is a new request that
|
||||
// must still be authorised and valid. A key already bound to a message with
|
||||
// the same text returns that message; with a different text it is a conflict.
|
||||
func (s *Service) Send(ctx context.Context, actor, chatID uuid.UUID, key, text string) (Message, error) {
|
||||
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
||||
// MessagePayload is one send attempt: the message and its Idempotency-Key.
|
||||
type MessagePayload struct {
|
||||
ChatID uuid.UUID
|
||||
IdempotencyKey string
|
||||
Text string
|
||||
}
|
||||
|
||||
// Send checks access and the text on every attempt, a replay included, as the
|
||||
// contract requires of a repeat.
|
||||
func (s *Service) Send(ctx context.Context, actor uuid.UUID, p MessagePayload) (Message, error) {
|
||||
if err := s.requireMember(ctx, actor, p.ChatID); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message, err := NewMessage(ident.New(), chatID, actor, text)
|
||||
message, err := NewMessage(ident.New(), p.ChatID, actor, p.Text)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
stored, err := s.messages.AppendOnce(ctx, message, key, s.keyTTL)
|
||||
stored, err := s.messages.AppendOnce(ctx, message, p.IdempotencyKey, s.keyTTL)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if stored.ID != message.ID && stored.Text != message.Text {
|
||||
if !stored.SameContent(message) {
|
||||
return Message{}, ErrKeyReused
|
||||
}
|
||||
return stored, nil
|
||||
}
|
||||
|
||||
// keyPurgeBatch bounds one delete statement, so the cleanup never holds many
|
||||
// row locks or runs long.
|
||||
const keyPurgeBatch = 1000
|
||||
|
||||
// PurgeExpiredKeys deletes every key older than the retention period, batch
|
||||
// by batch, and reports how many it deleted.
|
||||
func (s *Service) PurgeExpiredKeys(ctx context.Context) (int64, error) {
|
||||
var total int64
|
||||
for {
|
||||
n, err := s.messages.DeleteExpiredKeys(ctx, s.keyTTL, keyPurgeBatch)
|
||||
n, err := s.messages.DeleteExpiredKeys(ctx, s.keyTTL+keyPurgeGrace, keyPurgeBatch)
|
||||
total += n
|
||||
if err != nil {
|
||||
return total, err
|
||||
|
||||
Reference in New Issue
Block a user