automc: pg LISTEN/NOTIFY route source + HTTP waker
Adds opt-in extension package internal/automc/ that: - Subscribes to Postgres notifications on a 'servers' table and pushes route changes into server.Routes (no file I/O, no fsnotify). - Provides a WakerFunc that POSTs to a configurable HTTP control plane (server-manager) and polls until state=running. When AUTOMC_DSN is unset, Wire() is a no-op and the binary behaves exactly like upstream itzg/mc-router. Single patch site in main.go (import + 4-line call) keeps upstream rebases trivial. See docs/AUTOMC.md for env vars and the expected DB schema/trigger.
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
package automc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/itzg/mc-router/server"
|
||||
)
|
||||
|
||||
const (
|
||||
wakerPollInterval = 2 * time.Second
|
||||
wakerPollTimeout = 90 * time.Second
|
||||
)
|
||||
|
||||
var wakerPollIntervalForTest = wakerPollInterval
|
||||
|
||||
type wakerConfig struct {
|
||||
baseURL string
|
||||
token string
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
func newWakerConfig(baseURL, token string) *wakerConfig {
|
||||
if baseURL == "" {
|
||||
return nil
|
||||
}
|
||||
return &wakerConfig{
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
token: token,
|
||||
client: &http.Client{Timeout: 10 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wakerConfig) wakerFor(serverName string) server.WakerFunc {
|
||||
if w == nil {
|
||||
return nil
|
||||
}
|
||||
return func(ctx context.Context) (string, error) {
|
||||
if err := w.start(ctx, serverName); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return w.pollUntilRunning(ctx, serverName)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wakerConfig) start(ctx context.Context, name string) error {
|
||||
u := fmt.Sprintf("%s/servers/%s/start", w.baseURL, url.PathEscape(name))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.setAuth(req)
|
||||
resp, err := w.client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("waker start %s: %w", name, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 400 && resp.StatusCode != http.StatusConflict {
|
||||
// 409 = already starting/running, treat as success
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
return fmt.Errorf("waker start %s: %s — %s", name, resp.Status, strings.TrimSpace(string(body)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *wakerConfig) pollUntilRunning(ctx context.Context, name string) (string, error) {
|
||||
deadline := time.Now().Add(wakerPollTimeout)
|
||||
ticker := time.NewTicker(wakerPollIntervalForTest)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
state, addr, err := w.queryState(ctx, name)
|
||||
if err == nil && state == "running" && addr != "" {
|
||||
return addr, nil
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return "", fmt.Errorf("waker timeout for %s after %s (last state=%q err=%v)", name, wakerPollTimeout, state, err)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *wakerConfig) queryState(ctx context.Context, name string) (string, string, error) {
|
||||
u := fmt.Sprintf("%s/servers/%s", w.baseURL, url.PathEscape(name))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
w.setAuth(req)
|
||||
resp, err := w.client.Do(req)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", "", fmt.Errorf("query state %s: %s", name, resp.Status)
|
||||
}
|
||||
var body struct {
|
||||
State string `json:"state"`
|
||||
Address string `json:"address"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return body.State, body.Address, nil
|
||||
}
|
||||
|
||||
func (w *wakerConfig) setAuth(req *http.Request) {
|
||||
if w.token != "" {
|
||||
req.Header.Set("X-API-Key", w.token)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user