chore: establish local platform baseline

This commit is contained in:
Codex Local
2026-08-26 06:41:54 +08:00
commit 29bdf4900f
575 changed files with 95644 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
package health
import (
"context"
"sync"
"time"
)
type CheckFunc func(context.Context) error
type Checker struct {
timeout time.Duration
checks map[string]CheckFunc
}
type Result struct {
Status string `json:"status"`
Checks map[string]string `json:"checks"`
}
func NewChecker(timeout time.Duration, checks map[string]CheckFunc) *Checker {
return &Checker{timeout: timeout, checks: checks}
}
func (c *Checker) Ready(ctx context.Context) Result {
result := Result{Status: "ready", Checks: make(map[string]string, len(c.checks))}
var mutex sync.Mutex
var waitGroup sync.WaitGroup
for name, check := range c.checks {
name, check := name, check
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
checkContext, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
status := "up"
if err := check(checkContext); err != nil {
status = "down"
}
mutex.Lock()
result.Checks[name] = status
if status == "down" {
result.Status = "not_ready"
}
mutex.Unlock()
}()
}
waitGroup.Wait()
return result
}
+22
View File
@@ -0,0 +1,22 @@
package health
import (
"context"
"errors"
"testing"
"time"
)
func TestReadyReportsDependencyFailure(t *testing.T) {
checker := NewChecker(time.Second, map[string]CheckFunc{
"database": func(context.Context) error { return nil },
"redis": func(context.Context) error { return errors.New("unavailable") },
})
result := checker.Ready(context.Background())
if result.Status != "not_ready" {
t.Fatalf("status = %s, want not_ready", result.Status)
}
if result.Checks["database"] != "up" || result.Checks["redis"] != "down" {
t.Fatalf("unexpected checks: %#v", result.Checks)
}
}