Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
// Command server starts the messenger API over PostgreSQL, Valkey and S3.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"expvar"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/attachment"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/pipeline"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/s3store"
|
||||
"messenger/internal/platform/telemetry"
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/processing"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
const keyCleanupEvery = 10 * time.Minute
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
alias := ident.NewAlias()
|
||||
log := cfg.NewLogger().With("instance", alias)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, log, cfg, alias); err != nil {
|
||||
log.Error("server stopped with error", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run connects the dependencies before it listens, so the port accepts
|
||||
// connections only once they answer.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config, alias string) error {
|
||||
shutdown, err := telemetry.Start(ctx, "messenger-api")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer shutdown()
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL, cfg.DBMaxConns)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
if err := telemetry.ObservePool(pool); err != nil {
|
||||
return err
|
||||
}
|
||||
sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sessions.Close()
|
||||
blobs, reconciler, err := newAttachmentStorage(pool, cfg.S3, os.LookupEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.Addr(), err)
|
||||
}
|
||||
defer ln.Close()
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(),
|
||||
"gomaxprocs", runtime.GOMAXPROCS(0), "db_max_conns", cfg.DBMaxConns,
|
||||
"drain_delay", cfg.DrainDelay.String(), "shutdown_timeout", cfg.ShutdownTimeout.String(),
|
||||
"session_ttl", cfg.SessionTTL.String(), "idempotency_ttl", cfg.IdempotencyTTL.String())
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pool,
|
||||
Sessions: sessions,
|
||||
SessionTTL: cfg.SessionTTL,
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
InstanceID: alias,
|
||||
IdempotencyTTL: cfg.IdempotencyTTL,
|
||||
Blobs: blobs,
|
||||
AttachmentsCfg: attachment.Config{
|
||||
BarrierDir: os.Getenv("LAB5_BARRIERS"),
|
||||
MaxBytes: cfg.S3.MaxBytes,
|
||||
MaxPixels: cfg.S3.MaxPixels,
|
||||
},
|
||||
})
|
||||
var jobs config.Jobs
|
||||
var barrier func(context.Context, pipeline.Job, string) error
|
||||
if dir := os.Getenv("LAB5_BARRIERS"); dir != "" {
|
||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
||||
return err
|
||||
}
|
||||
barrier = processing.FileBarrier(dir)
|
||||
}
|
||||
if blobs != nil {
|
||||
deadline := 5 * time.Second
|
||||
if raw, ok := os.LookupEnv("READY_DEADLINE"); ok {
|
||||
deadline, err = time.ParseDuration(raw)
|
||||
if err != nil || deadline <= 0 {
|
||||
return fmt.Errorf("READY_DEADLINE must be a positive duration")
|
||||
}
|
||||
}
|
||||
if err := pipeline.NewStore(pool).RegisterMetrics(deadline); err != nil {
|
||||
return err
|
||||
}
|
||||
jobs, err = config.LoadJobs(os.LookupEnv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var debugLn net.Listener
|
||||
if cfg.DebugAddr != "" {
|
||||
if debugLn, err = net.Listen("tcp", cfg.DebugAddr); err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.DebugAddr, err)
|
||||
}
|
||||
publishDebugVars(alias, pool)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
var background sync.WaitGroup
|
||||
if blobs != nil {
|
||||
background.Go(func() { processing.Dispatch(ctx, log, pipeline.NewStore(pool), jobs, barrier) })
|
||||
}
|
||||
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, attachment.ReconcileOptions{Apply: true}, 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 {
|
||||
log.Error("debug listener stopped", "error", err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
err = server.Run(ctx, log, ln, a.Engine, a.Health, cfg.DrainDelay, cfg.ShutdownTimeout)
|
||||
cancel()
|
||||
background.Wait()
|
||||
return err
|
||||
}
|
||||
|
||||
func newAttachmentStorage(pool *pgxpool.Pool, cfg config.S3, lookup config.Lookup) (attachment.Blobs, *attachment.Reconciler, error) {
|
||||
if cfg.Endpoint == "" {
|
||||
return nil, nil, nil
|
||||
}
|
||||
blobs, err := s3store.New(cfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconCfg, err := config.LoadS3(config.ReconcileS3(lookup))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconBlobs, err := s3store.New(reconCfg)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
reconciler, err := attachment.NewReconcilerWithRetention(pool, reconBlobs, time.Hour, reconCfg.StagingRetention)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return blobs, reconciler, nil
|
||||
}
|
||||
|
||||
// publishDebugVars adds to expvar what the load tests read from a replica.
|
||||
func publishDebugVars(alias string, pool *pgxpool.Pool) {
|
||||
expvar.NewString("instance").Set(alias)
|
||||
expvar.Publish("gomaxprocs", expvar.Func(func() any { return runtime.GOMAXPROCS(0) }))
|
||||
expvar.Publish("pgxpool", expvar.Func(func() any {
|
||||
s := pool.Stat()
|
||||
return map[string]any{
|
||||
"acquire_count": s.AcquireCount(),
|
||||
"acquire_duration_ms": s.AcquireDuration().Milliseconds(),
|
||||
"acquired_conns": s.AcquiredConns(),
|
||||
"idle_conns": s.IdleConns(),
|
||||
"total_conns": s.TotalConns(),
|
||||
"max_conns": s.MaxConns(),
|
||||
"empty_acquire_count": s.EmptyAcquireCount(),
|
||||
"empty_acquire_wait_ms": s.EmptyAcquireWaitTime().Milliseconds(),
|
||||
"canceled_acquire_count": s.CanceledAcquireCount(),
|
||||
"new_conns_count": s.NewConnsCount(),
|
||||
}
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user