forked from mtusi2026/mtusi2026lab5
feat(outbox): add publisher with confirms and reconnect
This commit is contained in:
@@ -9,6 +9,7 @@ services:
|
|||||||
secrets:
|
secrets:
|
||||||
- database_url
|
- database_url
|
||||||
- cache_url
|
- cache_url
|
||||||
|
- broker_url
|
||||||
- s3_access_key_id
|
- s3_access_key_id
|
||||||
- s3_secret_access_key
|
- s3_secret_access_key
|
||||||
- s3_bootstrap_access_key_id
|
- s3_bootstrap_access_key_id
|
||||||
|
|||||||
+21
-10
@@ -16,6 +16,7 @@ import (
|
|||||||
"mtusi2026-messenger/internal/config"
|
"mtusi2026-messenger/internal/config"
|
||||||
"mtusi2026-messenger/internal/db"
|
"mtusi2026-messenger/internal/db"
|
||||||
"mtusi2026-messenger/internal/logger"
|
"mtusi2026-messenger/internal/logger"
|
||||||
|
"mtusi2026-messenger/internal/outbox"
|
||||||
"mtusi2026-messenger/internal/postgres"
|
"mtusi2026-messenger/internal/postgres"
|
||||||
"mtusi2026-messenger/internal/redis"
|
"mtusi2026-messenger/internal/redis"
|
||||||
s3store "mtusi2026-messenger/internal/s3"
|
s3store "mtusi2026-messenger/internal/s3"
|
||||||
@@ -80,17 +81,27 @@ func Run() error {
|
|||||||
|
|
||||||
log.Info("s3 storage ready", "endpoint", cfg.S3.Endpoint, "bucket", s3Client.Bucket())
|
log.Info("s3 storage ready", "endpoint", cfg.S3.Endpoint, "bucket", s3Client.Bucket())
|
||||||
|
|
||||||
cleanupCtx, stopCleanup := context.WithCancel(context.Background())
|
brokerURL, err := cfg.Broker.URL()
|
||||||
var cleanupWG sync.WaitGroup
|
if err != nil {
|
||||||
cleanupWG.Go(func() {
|
log.Error("read broker url", "error", err)
|
||||||
runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval)
|
return err
|
||||||
|
}
|
||||||
|
publisher := outbox.NewPublisher(pool, brokerURL, log)
|
||||||
|
|
||||||
|
backgroundCtx, stopBackground := context.WithCancel(context.Background())
|
||||||
|
var backgroundWG sync.WaitGroup
|
||||||
|
backgroundWG.Go(func() {
|
||||||
|
publisher.Run(backgroundCtx)
|
||||||
})
|
})
|
||||||
cleanupWG.Go(func() {
|
backgroundWG.Go(func() {
|
||||||
runIdempotencyCleanup(cleanupCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
runSessionCleanup(backgroundCtx, log, queries, sessionCleanupInterval)
|
||||||
})
|
})
|
||||||
cleanupWG.Go(func() {
|
backgroundWG.Go(func() {
|
||||||
|
runIdempotencyCleanup(backgroundCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
||||||
|
})
|
||||||
|
backgroundWG.Go(func() {
|
||||||
runAttachmentCleanup(
|
runAttachmentCleanup(
|
||||||
cleanupCtx,
|
backgroundCtx,
|
||||||
log,
|
log,
|
||||||
queries,
|
queries,
|
||||||
s3Client,
|
s3Client,
|
||||||
@@ -99,8 +110,8 @@ func Run() error {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
defer func() {
|
defer func() {
|
||||||
stopCleanup()
|
stopBackground()
|
||||||
cleanupWG.Wait()
|
backgroundWG.Wait()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
type AttachmentJob struct {
|
||||||
|
SchemaVersion int `json:"schema_version"`
|
||||||
|
JobID uuid.UUID `json:"job_id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
AttachmentID uuid.UUID `json:"attachment_id"`
|
||||||
|
PipelineVersion int `json:"pipeline_version"`
|
||||||
|
Attempt int `json:"attempt"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
package outbox
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"mtusi2026-messenger/internal/db"
|
||||||
|
"mtusi2026-messenger/internal/model"
|
||||||
|
"mtusi2026-messenger/internal/rabbitmq"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
pollInterval = time.Second
|
||||||
|
retryInterval = 2 * time.Second
|
||||||
|
publishTimeout = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
type Publisher struct {
|
||||||
|
pool *pgxpool.Pool
|
||||||
|
url string
|
||||||
|
log *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewPublisher(pool *pgxpool.Pool, url string, log *slog.Logger) *Publisher {
|
||||||
|
return &Publisher{
|
||||||
|
pool: pool,
|
||||||
|
url: url,
|
||||||
|
log: log,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Publisher) Run(ctx context.Context) {
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
client, err := rabbitmq.New(p.url)
|
||||||
|
if err == nil {
|
||||||
|
p.log.Info("outbox publisher connected")
|
||||||
|
err = p.publish(ctx, client)
|
||||||
|
_ = client.Close()
|
||||||
|
}
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p.log.Error("outbox publisher retry", "error", err)
|
||||||
|
if !wait(ctx, retryInterval) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Publisher) publish(ctx context.Context, client *rabbitmq.Client) error {
|
||||||
|
for ctx.Err() == nil {
|
||||||
|
published, err := p.publishOne(ctx, client)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !published && !wait(ctx, pollInterval) {
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Publisher) publishOne(ctx context.Context, client *rabbitmq.Client) (bool, error) {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, publishTimeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
tx, err := p.pool.Begin(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer tx.Rollback(ctx)
|
||||||
|
|
||||||
|
q := db.New(tx)
|
||||||
|
rows, err := q.LockPendingOutboxEntries(ctx, 1)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
row := rows[0]
|
||||||
|
body, err := json.Marshal(model.AttachmentJob{
|
||||||
|
SchemaVersion: 1,
|
||||||
|
JobID: row.JobID,
|
||||||
|
Type: "attachment.process.v1",
|
||||||
|
AttachmentID: row.AttachmentID,
|
||||||
|
PipelineVersion: row.PipelineVersion,
|
||||||
|
Attempt: row.Attempt,
|
||||||
|
CreatedAt: row.CreatedAt.UTC(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if err := client.Publish(ctx, row.JobID.String(), body); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if _, err := q.MarkOutboxPublished(ctx, row.ID); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
if err := tx.Commit(ctx); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func wait(ctx context.Context, delay time.Duration) bool {
|
||||||
|
timer := time.NewTimer(delay)
|
||||||
|
defer timer.Stop()
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return false
|
||||||
|
case <-timer.C:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user