forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
|
||||
)
|
||||
|
||||
// CreateUser atomically enforces uniqueness and resource limits.
|
||||
func (s *Store) CreateUser(_ context.Context, u dto.UserDTO) (dto.UserDTO, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if _, ok := s.usernames[u.Username]; ok {
|
||||
return dto.UserDTO{}, store.ErrConflict
|
||||
}
|
||||
if len(s.users) >= s.limits.MaxUsers || !s.reserve(userCost(u)) {
|
||||
return dto.UserDTO{}, store.ErrCapacity
|
||||
}
|
||||
u.CreatedAt = s.timestamp()
|
||||
s.users[u.ID] = u
|
||||
s.usernames[u.Username] = u.ID
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// User retrieves an immutable user.
|
||||
func (s *Store) User(_ context.Context, id uuid.UUID) (dto.UserDTO, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
u, ok := s.users[id]
|
||||
if !ok {
|
||||
return u, store.ErrNotFound
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// UsersExist checks the complete set under one read lock without loading user records.
|
||||
// Empty sets return true, and duplicate IDs do not affect the result.
|
||||
func (s *Store) UsersExist(_ context.Context, ids []uuid.UUID) (bool, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, id := range ids {
|
||||
if _, exists := s.users[id]; !exists {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
Reference in New Issue
Block a user