This commit is contained in:
Vladislav Kunetskiy
2026-11-05 20:33:41 +03:00
parent edc2a405ee
commit 147fed2d97
194 changed files with 10062 additions and 3831 deletions
+21 -3
View File
@@ -4,11 +4,15 @@ package outbox
import (
"context"
"errors"
"log/slog"
"time"
"go.opentelemetry.io/otel/codes"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/failpoint"
"mtusi-messenger/internal/telemetry"
)
type Repository interface {
@@ -17,7 +21,9 @@ type Repository interface {
ReleaseOutbox(ctx context.Context, id int64, reason string) error
ReapOutboxClaims(ctx context.Context) error
ReapExpiredLeases(ctx context.Context, limit int) (int, error)
ReconcileQueuedJobs(ctx context.Context, olderThan time.Duration, limit int) (int, error)
ReconcileQueuedJobs(ctx context.Context, olderThan time.Duration, limit int, traceparent, tracestate string) (int, error)
OldestActiveJobAge(ctx context.Context) (float64, error)
CountOverdueJobs(ctx context.Context, olderThan time.Duration) (int64, error)
PruneOutbox(ctx context.Context, retention time.Duration) error
OldestUnpublished(ctx context.Context) (time.Time, error)
}
@@ -103,7 +109,15 @@ func (r *Relay) RunOnce(ctx context.Context) (int, error) {
failpoint.Check("OUTBOX_AFTER_COMMIT", job.JobID)
if err := r.publisher.PublishConfirmed(ctx, message.RoutingKey, job); err != nil {
publishCtx, span := telemetry.StartPublishSpan(ctx, job)
job.Traceparent, job.Tracestate = telemetry.Inject(publishCtx)
err = r.publisher.PublishConfirmed(publishCtx, message.RoutingKey, job)
if err != nil {
span.RecordError(errors.New(telemetry.Redact(err.Error())))
span.SetStatus(codes.Error, telemetry.Redact(err.Error()))
}
span.End()
if err != nil {
_ = r.repo.ReleaseOutbox(ctx, message.ID, err.Error())
continue
@@ -121,6 +135,9 @@ func (r *Relay) RunOnce(ctx context.Context) (int, error) {
}
func (r *Relay) reap(ctx context.Context) {
ctx, span := telemetry.StartRequeueSpan(ctx, "requeue")
defer span.End()
if err := r.repo.ReapOutboxClaims(ctx); err != nil {
r.log.WarnContext(ctx, "outbox claim reaper failed", slog.Any("error", err))
}
@@ -135,7 +152,8 @@ func (r *Relay) reap(ctx context.Context) {
// The broker is not the source of truth. If a delivery was dead-lettered
// or lost, the queued job has no unpublished outbox row; republish it.
if r.config.ReconcileAfter > 0 {
reconciled, err := r.repo.ReconcileQueuedJobs(ctx, r.config.ReconcileAfter, 100)
traceparent, tracestate := telemetry.Inject(ctx)
reconciled, err := r.repo.ReconcileQueuedJobs(ctx, r.config.ReconcileAfter, 100, traceparent, tracestate)
if err != nil {
r.log.WarnContext(ctx, "queued-job reconciliation failed", slog.Any("error", err))
} else if reconciled > 0 {