Добавьте S3-вложения с дедупликацией и безопасной сверкой хранилищ
This commit is contained in:
@@ -17,10 +17,12 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/s3store"
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
@@ -59,6 +61,26 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
return err
|
||||
}
|
||||
defer sessions.Close()
|
||||
var blobs attachment.Blobs
|
||||
var reconciler *attachment.Reconciler
|
||||
if cfg.S3.Endpoint != "" {
|
||||
blobs, err = s3store.New(cfg.S3.Endpoint, cfg.S3.Region, cfg.S3.Bucket, cfg.S3.AccessKey, cfg.S3.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reconCfg, e := config.LoadS3(config.ReconcileS3(os.LookupEnv))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
reconBlobs, e := s3store.New(reconCfg.Endpoint, reconCfg.Region, reconCfg.Bucket, reconCfg.AccessKey, reconCfg.SecretKey)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
reconciler, err = attachment.NewReconciler(pool, reconBlobs, time.Hour)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
@@ -77,6 +99,9 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
Blobs: blobs,
|
||||
MaxUploadBytes: cfg.S3.MaxBytes,
|
||||
MaxImagePixels: cfg.S3.MaxPixels,
|
||||
})
|
||||
var debugLn net.Listener
|
||||
if cfg.DebugAddr != "" {
|
||||
@@ -89,6 +114,27 @@ func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string)
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
var background sync.WaitGroup
|
||||
background.Go(func() { chat.RunKeyCleanup(ctx, log, a.Chats, keyCleanupEvery) })
|
||||
if reconciler != nil {
|
||||
background.Go(func() {
|
||||
ticker := time.NewTicker(10 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
scan, cancel := context.WithTimeout(ctx, 2*time.Minute)
|
||||
stats, err := reconciler.Run(scan, true, false, nil)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Warn("attachment cleanup incomplete", "error", err)
|
||||
} else {
|
||||
log.Info("attachment cleanup", "scanned_files", stats.ScannedFiles, "repaired", stats.Repaired)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
if debugLn != nil {
|
||||
background.Go(func() {
|
||||
if err := server.RunDebug(ctx, log, debugLn, expvar.Handler()); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user