forked from mtusi2026/mtusi2026lab4
лаба 4 на оценку 5
This commit is contained in:
@@ -6,15 +6,22 @@ import (
|
||||
)
|
||||
|
||||
const MinimumIdempotencyTTL = 24 * time.Hour
|
||||
const defaultAttachmentCleanupTimeout = time.Second
|
||||
const defaultAttachmentUploadConcurrency = 4
|
||||
const defaultAttachmentDecodeConcurrency = 1
|
||||
|
||||
type ServiceOption func(*Service) error
|
||||
|
||||
type Service struct {
|
||||
repository Repository
|
||||
cursors *CursorCodec
|
||||
sessionTTL time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
now func() time.Time
|
||||
repository Repository
|
||||
cursors *CursorCodec
|
||||
sessionTTL time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
now func() time.Time
|
||||
objectStore ObjectStore
|
||||
attachmentCleanupTimeout time.Duration
|
||||
attachmentUploadSlots chan struct{}
|
||||
attachmentDecodeSlots chan struct{}
|
||||
}
|
||||
|
||||
func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Duration, options ...ServiceOption) (*Service, error) {
|
||||
@@ -28,11 +35,14 @@ func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Dur
|
||||
return nil, fmt.Errorf("session TTL must be positive")
|
||||
}
|
||||
service := &Service{
|
||||
repository: repository,
|
||||
cursors: cursors,
|
||||
sessionTTL: sessionTTL,
|
||||
idempotencyTTL: MinimumIdempotencyTTL,
|
||||
now: func() time.Time { return time.Now().UTC() },
|
||||
repository: repository,
|
||||
cursors: cursors,
|
||||
sessionTTL: sessionTTL,
|
||||
idempotencyTTL: MinimumIdempotencyTTL,
|
||||
now: func() time.Time { return time.Now().UTC() },
|
||||
attachmentCleanupTimeout: defaultAttachmentCleanupTimeout,
|
||||
attachmentUploadSlots: make(chan struct{}, defaultAttachmentUploadConcurrency),
|
||||
attachmentDecodeSlots: make(chan struct{}, defaultAttachmentDecodeConcurrency),
|
||||
}
|
||||
for _, option := range options {
|
||||
if err := option(service); err != nil {
|
||||
@@ -51,3 +61,13 @@ func WithIdempotencyTTL(ttl time.Duration) ServiceOption {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithObjectStore(objectStore ObjectStore) ServiceOption {
|
||||
return func(service *Service) error {
|
||||
if objectStore == nil {
|
||||
return fmt.Errorf("object store is required")
|
||||
}
|
||||
service.objectStore = objectStore
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user