()
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | // Parse command line flags |
| 38 | showVersion := flag.Bool("version", false, "Display version information") |
| 39 | flag.Parse() |
| 40 | |
| 41 | // Show version information if requested |
| 42 | if *showVersion { |
| 43 | log.Printf("MCP Registry %s\n", Version) |
| 44 | log.Printf("Git commit: %s\n", GitCommit) |
| 45 | log.Printf("Build time: %s\n", BuildTime) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | log.Printf("Starting MCP Registry Application v%s (commit: %s)", Version, GitCommit) |
| 50 | |
| 51 | var ( |
| 52 | registryService service.RegistryService |
| 53 | db database.Database |
| 54 | err error |
| 55 | ) |
| 56 | |
| 57 | // Initialize configuration |
| 58 | cfg := config.NewConfig() |
| 59 | |
| 60 | // Connect to PostgreSQL with bounded retry. The DB endpoint can flap briefly |
| 61 | // during voluntary disruptions (node drains, rollouts) and without retry the |
| 62 | // pod exits cleanly, restarts, then races with the still-recovering service |
| 63 | // — turning a transient blip into a multi-minute outage. |
| 64 | const ( |
| 65 | maxStartupAttempts = 8 |
| 66 | perAttemptTimeout = 10 * time.Second |
| 67 | initialBackoff = 1 * time.Second |
| 68 | maxBackoff = 8 * time.Second |
| 69 | ) |
| 70 | backoff := initialBackoff |
| 71 | for attempt := 1; ; attempt++ { |
| 72 | attemptCtx, attemptCancel := context.WithTimeout(context.Background(), perAttemptTimeout) |
| 73 | db, err = database.NewPostgreSQL(attemptCtx, cfg.DatabaseURL) |
| 74 | attemptCancel() |
| 75 | if err == nil { |
| 76 | break |
| 77 | } |
| 78 | if attempt >= maxStartupAttempts { |
| 79 | log.Printf("Failed to connect to PostgreSQL after %d attempts: %v", attempt, err) |
| 80 | return |
| 81 | } |
| 82 | log.Printf("PostgreSQL not reachable (attempt %d/%d): %v, retrying in %s", attempt, maxStartupAttempts, err, backoff) |
| 83 | time.Sleep(backoff) |
| 84 | backoff = min(backoff*2, maxBackoff) |
| 85 | } |
| 86 | |
| 87 | // Store the PostgreSQL instance for later cleanup |
| 88 | defer func() { |
| 89 | if err := db.Close(); err != nil { |
| 90 | log.Printf("Error closing PostgreSQL connection: %v", err) |
| 91 | } else { |
| 92 | log.Println("PostgreSQL connection closed successfully") |
| 93 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…