Реализовать lab3 на оценку 4: балансировка и отказ API

This commit is contained in:
Daria0w0
2026-10-23 17:14:32 +03:00
parent 0ab3a341aa
commit a67e7996ec
88 changed files with 5105 additions and 21 deletions
+45
View File
@@ -0,0 +1,45 @@
package service
import (
"context"
"time"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/model"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/password"
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
)
type UserWriter interface {
CreateUser(context.Context, string, string, string) (model.User, error)
}
type Registration struct {
users UserWriter
hashSlots chan struct{}
}
func NewRegistration(users UserWriter) *Registration {
return &Registration{users: users, hashSlots: make(chan struct{}, 2)}
}
func (s *Registration) Register(ctx context.Context, username, displayName, secret string) (model.User, error) {
if !usernamePattern.MatchString(username) || !validText(displayName, 100) || password.Validate(secret) != nil {
return model.User{}, ErrInvalidRequest
}
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
select {
case s.hashSlots <- struct{}{}:
case <-ctx.Done():
return model.User{}, store.ErrUnavailable
}
hash, err := password.Hash(secret)
<-s.hashSlots
if err != nil {
return model.User{}, err
}
if ctx.Err() != nil {
return model.User{}, store.ErrUnavailable
}
return s.users.CreateUser(ctx, username, displayName, hash)
}