lab5: асинхронная очередь, worker и идемпотентный pipeline
API принимает 202 после durable staging и TX intent (job+outbox); отдельные relay и worker публикуют/обрабатывают задания с publisher confirm, lease, retry/backoff, DLQ и replay. Добавлены версии pipeline, варианты thumbnail/crop/optimized, presign в verified staging, status-aware sweeper и admission control.
This commit is contained in:
@@ -37,6 +37,73 @@ type Config struct {
|
||||
Session Session `mapstructure:"session"`
|
||||
Idempotency Idempotency `mapstructure:"idempotency"`
|
||||
S3 S3 `mapstructure:"s3"`
|
||||
Broker Broker `mapstructure:"broker"`
|
||||
Worker Worker `mapstructure:"worker"`
|
||||
Relay Relay `mapstructure:"relay"`
|
||||
Outbox Outbox `mapstructure:"outbox"`
|
||||
Processing Processing `mapstructure:"processing"`
|
||||
Staging Staging `mapstructure:"staging"`
|
||||
Admission Admission `mapstructure:"admission"`
|
||||
}
|
||||
|
||||
// Broker carries the AMQP transport settings. The API never publishes; relay
|
||||
// and worker require a non-empty URL at startup.
|
||||
type Broker struct {
|
||||
URL string `mapstructure:"url"`
|
||||
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
|
||||
ConfirmTimeout time.Duration `mapstructure:"confirm_timeout"`
|
||||
Heartbeat time.Duration `mapstructure:"heartbeat"`
|
||||
Exchange string `mapstructure:"exchange" validate:"required"`
|
||||
DLX string `mapstructure:"dlx" validate:"required"`
|
||||
Queue string `mapstructure:"queue" validate:"required"`
|
||||
DLQ string `mapstructure:"dlq" validate:"required"`
|
||||
Prefetch int `mapstructure:"prefetch" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
// Worker bounds the process that consumes jobs.
|
||||
type Worker struct {
|
||||
Concurrency int `mapstructure:"concurrency" validate:"required,gt=0,lte=32"`
|
||||
Prefetch int `mapstructure:"prefetch" validate:"required,gt=0,lte=100"`
|
||||
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout" validate:"required,gt=0"`
|
||||
HealthAddr string `mapstructure:"health_addr" validate:"required"`
|
||||
}
|
||||
|
||||
// Relay bounds the outbox publisher and lease reaper.
|
||||
type Relay struct {
|
||||
HealthAddr string `mapstructure:"health_addr" validate:"required"`
|
||||
}
|
||||
|
||||
// Outbox schedules publications and reclaims abandoned relay claims.
|
||||
type Outbox struct {
|
||||
PollInterval time.Duration `mapstructure:"poll_interval" validate:"required,gt=0"`
|
||||
ClaimTTL time.Duration `mapstructure:"claim_ttl" validate:"required,gt=0"`
|
||||
Batch int `mapstructure:"batch" validate:"required,gt=0,lte=1000"`
|
||||
ReaperInterval time.Duration `mapstructure:"reaper_interval" validate:"required,gt=0"`
|
||||
Retention time.Duration `mapstructure:"retention" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
// Processing holds the shared job contract knobs read by API and worker.
|
||||
type Processing struct {
|
||||
PipelineVersion int `mapstructure:"pipeline_version" validate:"required,gte=1,lte=2"`
|
||||
MaxAttempts int `mapstructure:"max_attempts" validate:"required,gte=1,lte=100"`
|
||||
JobTimeout time.Duration `mapstructure:"job_timeout" validate:"required,gt=0"`
|
||||
LeaseTTL time.Duration `mapstructure:"lease_ttl" validate:"required,gt=0"`
|
||||
Heartbeat time.Duration `mapstructure:"heartbeat" validate:"required,gt=0"`
|
||||
RetryBase time.Duration `mapstructure:"retry_base" validate:"required,gt=0"`
|
||||
RetryCap time.Duration `mapstructure:"retry_cap" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
// Staging controls cleanup of objects that are no longer the active source.
|
||||
type Staging struct {
|
||||
OrphanGrace time.Duration `mapstructure:"orphan_grace" validate:"required,gt=0"`
|
||||
FailedRetention time.Duration `mapstructure:"failed_retention" validate:"required,gt=0"`
|
||||
}
|
||||
|
||||
// Admission bounds accepted-but-unfinished work per user and globally.
|
||||
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"`
|
||||
}
|
||||
|
||||
// S3 describes the object store and attachment limits. Access keys are checked
|
||||
@@ -178,6 +245,38 @@ var defaults = map[string]any{
|
||||
"cache.chat_list_ttl": "5m",
|
||||
"cache.lock_ttl": "10s",
|
||||
"cache.lock_wait_timeout": "2s",
|
||||
|
||||
"broker.url": "",
|
||||
"broker.connect_timeout": "5s",
|
||||
"broker.confirm_timeout": "5s",
|
||||
"broker.heartbeat": "10s",
|
||||
"broker.exchange": "attachments.jobs",
|
||||
"broker.dlx": "attachments.dlx",
|
||||
"broker.queue": "attachment.process.v1",
|
||||
"broker.dlq": "attachment.process.dead",
|
||||
"broker.prefetch": 1,
|
||||
"worker.concurrency": 1,
|
||||
"worker.prefetch": 1,
|
||||
"worker.shutdown_timeout": "30s",
|
||||
"worker.health_addr": ":8081",
|
||||
"relay.health_addr": ":8082",
|
||||
"outbox.poll_interval": "1s",
|
||||
"outbox.claim_ttl": "30s",
|
||||
"outbox.batch": 20,
|
||||
"outbox.reaper_interval": "15s",
|
||||
"outbox.retention": "168h",
|
||||
"processing.pipeline_version": 1,
|
||||
"processing.max_attempts": 5,
|
||||
"processing.job_timeout": "120s",
|
||||
"processing.lease_ttl": "180s",
|
||||
"processing.heartbeat": "30s",
|
||||
"processing.retry_base": "2s",
|
||||
"processing.retry_cap": "60s",
|
||||
"staging.orphan_grace": "1h",
|
||||
"staging.failed_retention": "168h",
|
||||
"admission.max_pending_per_user": 20,
|
||||
"admission.max_pending": 200,
|
||||
"admission.max_staged_bytes": 2147483648,
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
@@ -233,6 +332,28 @@ func (c Config) validateDependencies() error {
|
||||
return errors.New("validate config: s3.sweep_grace must exceed s3.presign_ttl")
|
||||
}
|
||||
|
||||
if c.Processing.LeaseTTL < 3*c.Processing.Heartbeat {
|
||||
return errors.New("validate config: processing.lease_ttl must be at least 3x processing.heartbeat")
|
||||
}
|
||||
|
||||
if c.Processing.JobTimeout >= c.Processing.LeaseTTL {
|
||||
return errors.New("validate config: processing.job_timeout must be below processing.lease_ttl")
|
||||
}
|
||||
|
||||
if c.Processing.RetryCap < c.Processing.RetryBase {
|
||||
return errors.New("validate config: processing.retry_cap must be >= processing.retry_base")
|
||||
}
|
||||
|
||||
if c.Staging.FailedRetention < c.Staging.OrphanGrace {
|
||||
return errors.New("validate config: staging.failed_retention must be >= staging.orphan_grace")
|
||||
}
|
||||
|
||||
if c.Broker.URL != "" {
|
||||
if _, err := url.Parse(c.Broker.URL); err != nil {
|
||||
return fmt.Errorf("validate config: broker.url: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user