feat(lab3): HAProxy LB, stateless API replicas, X-Instance-Id, Idempotency-Key, Grade 5 Track 1
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/internal/handler"
|
||||
"messenger/internal/store"
|
||||
)
|
||||
|
||||
func getEnv(key, defaultVal string) string {
|
||||
if val := strings.TrimSpace(os.Getenv(key)); val != "" {
|
||||
return val
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func getEnvInt(key string, defaultVal int) int {
|
||||
if val := strings.TrimSpace(os.Getenv(key)); val != "" {
|
||||
if n, err := strconv.Atoi(val); err == nil {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func getEnvBool(key string, defaultVal bool) bool {
|
||||
if val := strings.TrimSpace(os.Getenv(key)); val != "" {
|
||||
if b, err := strconv.ParseBool(val); err == nil {
|
||||
return b
|
||||
}
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func resolveSecretOrEnv(fileEnvKey, directEnvKey, defaultVal string) string {
|
||||
if filePath := strings.TrimSpace(os.Getenv(fileEnvKey)); filePath != "" {
|
||||
if data, err := os.ReadFile(filePath); err == nil {
|
||||
if trimmed := strings.TrimSpace(string(data)); trimmed != "" {
|
||||
return trimmed
|
||||
}
|
||||
}
|
||||
}
|
||||
if direct := strings.TrimSpace(os.Getenv(directEnvKey)); direct != "" {
|
||||
return direct
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func resolveInstanceID() string {
|
||||
if id := strings.TrimSpace(os.Getenv("INSTANCE_ID")); id != "" {
|
||||
return id
|
||||
}
|
||||
if host, err := os.Hostname(); err == nil && strings.TrimSpace(host) != "" {
|
||||
return strings.TrimSpace(host)
|
||||
}
|
||||
return "api-" + uuid.New().String()[:12]
|
||||
}
|
||||
|
||||
func loadMigrations(dir string) ([]store.MigrationFile, error) {
|
||||
entries, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read migrations dir %s: %w", dir, err)
|
||||
}
|
||||
var names []string
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".sql") {
|
||||
names = append(names, entry.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(names)
|
||||
if len(names) == 0 {
|
||||
return nil, fmt.Errorf("no .sql migration files found in %s", dir)
|
||||
}
|
||||
|
||||
migrations := make([]store.MigrationFile, 0, len(names))
|
||||
for _, name := range names {
|
||||
raw, err := os.ReadFile(filepath.Join(dir, name))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read migration %s: %w", name, err)
|
||||
}
|
||||
migrations = append(migrations, store.MigrationFile{
|
||||
Version: name,
|
||||
SQL: string(raw),
|
||||
})
|
||||
}
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
migrateOnly := flag.Bool("migrate-only", false, "Run database migrations and exit")
|
||||
flag.Parse()
|
||||
|
||||
host := getEnv("APP_HOST", "0.0.0.0")
|
||||
port := getEnv("APP_PORT", "8080")
|
||||
appEnv := getEnv("APP_ENV", "lab")
|
||||
sessionTTL := getEnvInt("SESSION_TTL_SECONDS", 3600)
|
||||
idempotencyTTL := getEnvInt("IDEMPOTENCY_TTL_SECONDS", 86400)
|
||||
maxConns := int32(getEnvInt("DB_MAX_CONNS", 12))
|
||||
minConns := int32(getEnvInt("DB_MIN_CONNS", 2))
|
||||
queryTimeoutMs := getEnvInt("DB_QUERY_TIMEOUT_MS", 5000)
|
||||
drainDelayMs := getEnvInt("SHUTDOWN_DRAIN_DELAY_MS", 1500)
|
||||
cacheMode := getEnv("CACHE_MODE", "write_through_versioned")
|
||||
writeMode := getEnv("WRITE_MODE", "pipeline")
|
||||
migrationsDir := getEnv("MIGRATIONS_DIR", "migrations")
|
||||
runMigrations := *migrateOnly || getEnvBool("RUN_MIGRATIONS", true)
|
||||
instanceID := resolveInstanceID()
|
||||
|
||||
dbURL := resolveSecretOrEnv("DATABASE_URL_FILE", "DATABASE_URL", "postgres://messenger:messenger_lab3_pass@localhost:5432/messenger?sslmode=disable")
|
||||
cacheURL := resolveSecretOrEnv("CACHE_URL_FILE", "CACHE_URL", "redis://localhost:6379/0")
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
st, err := store.NewPostgresStore(ctx, store.Config{
|
||||
DatabaseURL: dbURL,
|
||||
CacheURL: cacheURL,
|
||||
MaxConns: maxConns,
|
||||
MinConns: minConns,
|
||||
QueryTimeout: time.Duration(queryTimeoutMs) * time.Millisecond,
|
||||
CacheMode: cacheMode,
|
||||
WriteMode: writeMode,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to initialize store: %v", err)
|
||||
}
|
||||
defer st.Close()
|
||||
|
||||
if runMigrations {
|
||||
migrations, err := loadMigrations(migrationsDir)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to load migrations: %v", err)
|
||||
}
|
||||
|
||||
var migErr error
|
||||
for attempt := 1; attempt <= 30; attempt++ {
|
||||
migErr = st.RunMigrations(ctx, migrations)
|
||||
if migErr == nil {
|
||||
break
|
||||
}
|
||||
log.Printf("Waiting for PostgreSQL to apply migrations (attempt %d/30): %v", attempt, migErr)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Fatalf("Interrupted while waiting for database migrations")
|
||||
case <-time.After(1 * time.Second):
|
||||
}
|
||||
}
|
||||
if migErr != nil {
|
||||
log.Fatalf("Could not apply database migrations after 30 attempts: %v", migErr)
|
||||
}
|
||||
}
|
||||
|
||||
if *migrateOnly {
|
||||
log.Println("Migrate-only flag set; exiting 0.")
|
||||
return
|
||||
}
|
||||
|
||||
h, apiHandler := handler.RegisterRoutes(st, sessionTTL, idempotencyTTL, instanceID)
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", host, port)
|
||||
srv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: h,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
go func() {
|
||||
log.Printf("Starting messenger server (Lab 3, instance=%s) on %s (env=%s, sessionTTL=%ds, idempotencyTTL=%ds, dbMaxConns=%d, writeMode=%s, cacheMode=%s)",
|
||||
instanceID, addr, appEnv, sessionTTL, idempotencyTTL, maxConns, writeMode, cacheMode)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Fatalf("ListenAndServe error: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
log.Printf("Instance %s received termination signal: marking /health/ready=503 and draining...", instanceID)
|
||||
apiHandler.SetDraining(true)
|
||||
|
||||
if drainDelayMs > 0 {
|
||||
time.Sleep(time.Duration(drainDelayMs) * time.Millisecond)
|
||||
}
|
||||
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
log.Printf("Server shutdown error: %v", err)
|
||||
} else {
|
||||
log.Printf("Instance %s stopped gracefully", instanceID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user