feat(httpapi): harden control plane and speed up prefix refresh
Очередь job получила lease и fencing, телеметрия и прогресс deploy сохраняются, ревизия пишет manifest входов. Обновление префиксов не переписывает неизменённый снимок, большие наборы идут через COPY, а устаревшая ревизия не попадает в deploy. Бандл ноды кэшируется, prune и постановка job больше не держат горячий путь. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
924f111e48
commit
f8e0520817
197 files changed
+18655
-1416
No files matched your search
@@ -23,6 +23,8 @@ const DefaultASOverviewURL = "https://stat.ripe.net/data/as-overview/data.json"
|
||||
|
||||
// AnnouncedPrefixes returns currently announced IPv4/IPv6 prefixes for the ASN (best-effort via RIPEstat).
|
||||
func AnnouncedPrefixes(ctx context.Context, hc *http.Client, asn int64) ([]netip.Prefix, error) {
|
||||
ctx, cancel := ripeRequestContext(ctx)
|
||||
defer cancel()
|
||||
if hc == nil {
|
||||
hc = httpclient.New(httpclient.DefaultTimeout)
|
||||
}
|
||||
@@ -43,13 +45,14 @@ func AnnouncedPrefixes(ctx context.Context, hc *http.Client, asn int64) ([]netip
|
||||
return nil, fmt.Errorf("ripestat fetch AS%d: %w", asn, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 32<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
|
||||
return nil, fmt.Errorf("ripestat AS%d: HTTP %s: %s", asn, resp.Status, truncateForErr(body, 200))
|
||||
}
|
||||
body, err := httpclient.ReadResponseBody(resp, 32<<20)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ripestat AS%d response: %w", asn, err)
|
||||
}
|
||||
|
||||
var wrap struct {
|
||||
Status string `json:"status"`
|
||||
@@ -85,6 +88,8 @@ func AnnouncedPrefixes(ctx context.Context, hc *http.Client, asn int64) ([]netip
|
||||
|
||||
// ASHolderName returns the holder / organization label for the ASN from RIPEstat as-overview (best-effort).
|
||||
func ASHolderName(ctx context.Context, hc *http.Client, asn int64) (string, error) {
|
||||
ctx, cancel := ripeRequestContext(ctx)
|
||||
defer cancel()
|
||||
if hc == nil {
|
||||
hc = httpclient.New(httpclient.DefaultTimeout)
|
||||
}
|
||||
@@ -105,13 +110,14 @@ func ASHolderName(ctx context.Context, hc *http.Client, asn int64) (string, erro
|
||||
return "", fmt.Errorf("ripestat as-overview AS%d: %w", asn, err)
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4<<10))
|
||||
return "", fmt.Errorf("ripestat as-overview AS%d: HTTP %s: %s", asn, resp.Status, truncateForErr(body, 200))
|
||||
}
|
||||
body, err := httpclient.ReadResponseBody(resp, 4<<20)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("ripestat as-overview AS%d response: %w", asn, err)
|
||||
}
|
||||
|
||||
var wrap struct {
|
||||
Status string `json:"status"`
|
||||
@@ -135,3 +141,10 @@ func truncateForErr(b []byte, n int) string {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func ripeRequestContext(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
if _, ok := ctx.Deadline(); ok {
|
||||
return context.WithCancel(ctx)
|
||||
}
|
||||
return context.WithTimeout(ctx, httpclient.DefaultTimeout)
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package asnresolve
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"evobgp/internal/httpclient"
|
||||
)
|
||||
|
||||
type ripeRoundTrip func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f ripeRoundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||
|
||||
type ripeCountingBody struct {
|
||||
io.Reader
|
||||
read int
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (b *ripeCountingBody) Read(p []byte) (int, error) {
|
||||
n, e := b.Reader.Read(p)
|
||||
b.read += n
|
||||
return n, e
|
||||
}
|
||||
func (b *ripeCountingBody) Close() error { b.closed = true; return nil }
|
||||
|
||||
func TestRIPECompleteResponseLimits(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
limit int
|
||||
prefix string
|
||||
call func(context.Context, *http.Client) error
|
||||
}{
|
||||
{"announced", 32 << 20, `{"status":"ok","data":{"prefixes":[]}}`, func(ctx context.Context, hc *http.Client) error { _, e := AnnouncedPrefixes(ctx, hc, 64496); return e }},
|
||||
{"holder", 4 << 20, `{"status":"ok","data":{"holder":"Example"}}`, func(ctx context.Context, hc *http.Client) error { _, e := ASHolderName(ctx, hc, 64496); return e }},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
for _, known := range []bool{true, false} {
|
||||
for _, extra := range []int{0, 1} {
|
||||
body := &ripeCountingBody{Reader: strings.NewReader(tc.prefix + strings.Repeat(" ", tc.limit-len(tc.prefix)+extra))}
|
||||
hc := &http.Client{Transport: ripeRoundTrip(func(r *http.Request) (*http.Response, error) {
|
||||
if _, ok := r.Context().Deadline(); !ok {
|
||||
t.Error("missing request deadline")
|
||||
}
|
||||
length := int64(-1)
|
||||
if known {
|
||||
length = int64(tc.limit + extra)
|
||||
}
|
||||
return &http.Response{StatusCode: 200, Body: body, ContentLength: length, Header: make(http.Header)}, nil
|
||||
})}
|
||||
err := tc.call(context.Background(), hc)
|
||||
if extra == 0 && err != nil {
|
||||
t.Fatalf("exact limit rejected known=%v: %v", known, err)
|
||||
}
|
||||
if extra == 1 && !errors.Is(err, httpclient.ErrResponseTooLarge) {
|
||||
t.Fatalf("oversized valid prefix accepted known=%v: %v", known, err)
|
||||
}
|
||||
if body.read > tc.limit+1 || !body.closed {
|
||||
t.Fatalf("read=%d closed=%v", body.read, body.closed)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRIPEErrorBodySmallAndContextPreserved(t *testing.T) {
|
||||
body := &ripeCountingBody{Reader: strings.NewReader(strings.Repeat("x", 1<<20))}
|
||||
hc := &http.Client{Transport: ripeRoundTrip(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 400, Status: "400 Bad Request", Body: body, Header: make(http.Header)}, nil
|
||||
})}
|
||||
if _, err := AnnouncedPrefixes(context.Background(), hc, 1); err == nil || body.read > 4<<10 || !body.closed {
|
||||
t.Fatalf("non200 unbounded read=%d close=%v err=%v", body.read, body.closed, err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
||||
defer cancel()
|
||||
hc.Transport = ripeRoundTrip(func(r *http.Request) (*http.Response, error) { <-r.Context().Done(); return nil, r.Context().Err() })
|
||||
if _, err := ASHolderName(ctx, hc, 1); !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatalf("caller deadline lost: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRIPEGzipExpandedLimit(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
gz := gzip.NewWriter(w)
|
||||
_, _ = io.WriteString(gz, `{"status":"ok","data":{"holder":"Example"}}`+strings.Repeat(" ", 4<<20))
|
||||
_ = gz.Close()
|
||||
}))
|
||||
defer server.Close()
|
||||
t.Setenv("EVOBGP_RIPESTAT_AS_OVERVIEW_URL", server.URL)
|
||||
if _, err := ASHolderName(context.Background(), server.Client(), 1); !errors.Is(err, httpclient.ErrResponseTooLarge) {
|
||||
t.Fatalf("gzip bypassed decoded limit: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user