lab5: ограничение одновременных upload-spool и evidence после ревью

Process-level semaphore bounds temporary disk use on the upload path; refresh raw evidence after the review fixes.
This commit is contained in:
Vladislav Kunetskiy
2026-10-30 23:16:12 +03:00
parent 9d7a0acb5a
commit e40a5cf215
13 changed files with 69 additions and 48 deletions
+1
View File
@@ -105,6 +105,7 @@ type Admission struct {
MaxPendingPerUser int `mapstructure:"max_pending_per_user" validate:"required,gte=0"`
MaxPending int `mapstructure:"max_pending" validate:"required,gte=0"`
MaxStagedBytes int64 `mapstructure:"max_staged_bytes" validate:"required,gte=0"`
MaxConcurrent int `mapstructure:"max_concurrent_uploads" validate:"omitempty,gt=0"`
}
// S3 describes the object store and attachment limits. Access keys are checked
+16 -1
View File
@@ -17,6 +17,7 @@ import (
"unicode/utf8"
"github.com/google/uuid"
"golang.org/x/sync/semaphore"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/imagecheck"
@@ -81,6 +82,7 @@ type Config struct {
MaxPendingPerUser int
MaxPending int
MaxStagedBytes int64
MaxConcurrent int
}
type Service struct {
@@ -88,6 +90,7 @@ type Service struct {
store objectstore.Store
config Config
log *slog.Logger
upload *semaphore.Weighted
}
func NewService(repo Repository, store objectstore.Store, config Config, log *slog.Logger) *Service {
@@ -95,7 +98,12 @@ func NewService(repo Repository, store objectstore.Store, config Config, log *sl
log = slog.Default()
}
return &Service{repo: repo, store: store, config: config, log: log}
service := &Service{repo: repo, store: store, config: config, log: log}
if config.MaxConcurrent > 0 {
service.upload = semaphore.NewWeighted(int64(config.MaxConcurrent))
}
return service
}
// UploadInput is one raw-body upload. DeclaredContentType comes from the HTTP
@@ -111,6 +119,13 @@ type UploadInput struct {
// Upload durably stages the original, records the job intent and returns the
// queued snapshot. It never waits for the broker or the worker.
func (s *Service) Upload(ctx context.Context, input UploadInput) (domain.Attachment, error) {
if s.upload != nil {
if err := s.upload.Acquire(ctx, 1); err != nil {
return domain.Attachment{}, unavailable("acquire upload slot", err)
}
defer s.upload.Release(1)
}
if err := ValidateFilename(input.Filename); err != nil {
return domain.Attachment{}, err
}