Перенёс приложение из lab4, добавил очередь заданий, outbox и worker

This commit is contained in:
gera
2026-11-06 01:59:07 +03:00
parent 276dbdad8a
commit dfa8784afa
19 changed files with 3371 additions and 0 deletions
+57
View File
@@ -0,0 +1,57 @@
package main
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
func readSecret(envKey, fileKey string) (string, error) {
if v := os.Getenv(envKey); v != "" {
return strings.TrimSpace(v), nil
}
path := os.Getenv(fileKey)
if path == "" {
return "", fmt.Errorf("%s or %s required", envKey, fileKey)
}
b, err := os.ReadFile(path)
if err != nil {
return "", err
}
return strings.TrimSpace(string(b)), nil
}
func openDB(ctx context.Context) (*pgxpool.Pool, error) {
dsn, err := readSecret("DATABASE_URL", "DATABASE_URL_FILE")
if err != nil {
return nil, err
}
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, err
}
cfg.MaxConns = 10
cfg.MinConns = 2
cfg.MaxConnLifetime = 5 * time.Minute
cfg.ConnConfig.ConnectTimeout = 3 * time.Second
pool, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
return nil, err
}
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := pool.Ping(pingCtx); err != nil {
pool.Close()
return nil, err
}
return pool, nil
}
func dbCtx(parent context.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, 3*time.Second)
}