forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
package production
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/cursor"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPageLimit = 50
|
||||
maxPageLimit = 100
|
||||
// Fetching one extra record determines whether to issue a next cursor.
|
||||
pageLookahead = 1
|
||||
)
|
||||
|
||||
type pagination struct {
|
||||
position dto.PositionDTO
|
||||
limit int
|
||||
resource string
|
||||
}
|
||||
|
||||
// parsePage validates the page size and resource-bound cursor.
|
||||
func (s *Service) parsePage(request dto.PageRequestDTO, resource string) (pagination, error) {
|
||||
limit, err := parseLimit(request.Limit)
|
||||
if err != nil {
|
||||
return pagination{}, err
|
||||
}
|
||||
position, err := s.cursors.Decode(request.Cursor, resource)
|
||||
if errors.Is(err, cursor.ErrInvalid) {
|
||||
return pagination{}, service.ErrInvalid
|
||||
}
|
||||
if err != nil {
|
||||
return pagination{}, err
|
||||
}
|
||||
return pagination{
|
||||
position: dto.PositionDTO{CreatedAt: position.CreatedAt, ID: position.ID},
|
||||
limit: limit,
|
||||
resource: resource,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// parseLimit parses a bounded page size or supplies the default.
|
||||
func parseLimit(raw string) (int, error) {
|
||||
if raw == "" {
|
||||
return defaultPageLimit, nil
|
||||
}
|
||||
for _, char := range raw {
|
||||
if char < '0' || char > '9' {
|
||||
return 0, service.ErrInvalid
|
||||
}
|
||||
}
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxPageLimit {
|
||||
return 0, service.ErrInvalid
|
||||
}
|
||||
return limit, nil
|
||||
}
|
||||
|
||||
// fetchLimit includes one lookahead record to detect a following page.
|
||||
func (page pagination) fetchLimit() int {
|
||||
return page.limit + pageLookahead
|
||||
}
|
||||
|
||||
// chatListResource scopes a cursor to one user's visible chats.
|
||||
func chatListResource(userID uuid.UUID) string {
|
||||
return "chats:" + userID.String()
|
||||
}
|
||||
|
||||
// messageHistoryResource scopes a cursor to one chat's message history.
|
||||
func messageHistoryResource(chatID uuid.UUID) string {
|
||||
return "messages:" + chatID.String()
|
||||
}
|
||||
Reference in New Issue
Block a user