Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
client: bound the enrollment retry by time, not by attempts
Antonio Ojeaa888f2acb21f
diff --git a/internal/controlplane/client/retry.go b/internal/controlplane/client/retry.goindex e03da32d..2ae3904b 100644--- a/internal/controlplane/client/retry.go+++ b/internal/controlplane/client/retry.go@@ -32,33 +32,37 @@ type Backoff struct {// Initial is the first wait when the answer carries no Retry-After;// each further wait doubles, up to Max.Initial, Max time.Duration- // Attempts is how many answers are retried before the last one is- // returned to the caller as it came.- Attempts int+ // Budget is how long in all the caller keeps trying. The last answer+ // before the budget runs out is returned to the caller as it came.+ // Time, not a count of attempts: a control plane that names a short+ // Retry-After is asking for many short waits, and a fleet of N members+ // behind a limiter of R a second needs N/R seconds whatever it answers.+ Budget time.Duration}-// DefaultEnrollBackoff waits about a minute in all before giving up, which-// covers a fleet of a few hundred members joining through a limiter of ten-// per second.-var DefaultEnrollBackoff = Backoff{Initial: time.Second, Max: 15 * time.Second, Attempts: 8}+// DefaultEnrollBackoff keeps trying for three minutes, which admits a+// fleet of about 1500 members that start at once through the control+// plane's limiter of ten a second. A larger fleet starts in waves.+var DefaultEnrollBackoff = Backoff{Initial: time.Second, Max: 15 * time.Second, Budget: 3 * time.Minute}// DoWithChallengeRetry is DoWithChallenge for a request the control plane// may answer with 429 or 503. Such an answer is retried after Retry-After// when the control plane names a wait, and after the backoff's own delay// when it does not, with jitter so a fleet told "not now" together does-// not come back together. Any other answer, and the last retried one, is-// returned to the caller.+// not come back together. Any other answer, and the last one within the+// budget, is returned to the caller.func DoWithChallengeRetry(ctx context.Context, httpClient *http.Client, now func() time.Time, b Backoff, build func(ts int64) (*http.Request, error)) (*http.Response, error) {if now == nil {now = time.Now}+ deadline := now().Add(b.Budget)delay := b.Initial- for attempt := 0; ; attempt++ {+ for {resp, err := DoWithChallenge(httpClient, now, build)if err != nil {return nil, err}- if !retryable(resp.StatusCode) || attempt >= b.Attempts {+ if !retryable(resp.StatusCode) {return resp, nil}wait, named := retryAfter(resp, now)@@ -68,6 +72,9 @@ func DoWithChallengeRetry(ctx context.Context, httpClient *http.Client, now func}// Up to half the wait again, at random.wait += time.Duration(rand.Int64N(int64(wait)/2 + 1))+ if now().Add(wait).After(deadline) {+ return resp, nil+ }_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, MaxBodyBytes))_ = resp.Body.Close()select {diff --git a/internal/controlplane/client/retry_test.go b/internal/controlplane/client/retry_test.goindex 11369915..1a48b439 100644--- a/internal/controlplane/client/retry_test.go+++ b/internal/controlplane/client/retry_test.go@@ -28,7 +28,7 @@ import (// returned at once, and a limiter that never relents hands its last answer// back after the configured attempts.func TestDoWithChallengeRetry(t *testing.T) {- fast := Backoff{Initial: time.Millisecond, Max: 5 * time.Millisecond, Attempts: 4}+ fast := Backoff{Initial: time.Millisecond, Max: 5 * time.Millisecond, Budget: 2 * time.Second}build := func(url string) func(ts int64) (*http.Request, error) {return func(ts int64) (*http.Request, error) {return http.NewRequest(http.MethodPost, url, nil)@@ -123,20 +123,27 @@ func TestDoWithChallengeRetry(t *testing.T) {}})- t.Run("gives up after the attempts", func(t *testing.T) {+ t.Run("gives up when the budget runs out", func(t *testing.T) {var calls atomic.Int32srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {calls.Add(1)+ w.Header().Set("Retry-After", "1")http.Error(w, "busy", http.StatusTooManyRequests)}))defer srv.Close()+ // A budget of 2 s and a named wait of 1 s to 1.5 s: one retry fits,+ // a second would end past the budget.+ started := time.Now()resp, err := DoWithChallengeRetry(context.Background(), srv.Client(), nil, fast, build(srv.URL))if err != nil {t.Fatal(err)}_ = resp.Body.Close()- if resp.StatusCode != http.StatusTooManyRequests || int(calls.Load()) != fast.Attempts+1 {- t.Fatalf("status %d after %d calls, want 429 after %d", resp.StatusCode, calls.Load(), fast.Attempts+1)+ if resp.StatusCode != http.StatusTooManyRequests || calls.Load() != 2 {+ t.Fatalf("status %d after %d calls, want 429 after 2", resp.StatusCode, calls.Load())+ }+ if took := time.Since(started); took > fast.Budget {+ t.Fatalf("kept trying for %s, past the %s budget", took, fast.Budget)}})@@ -148,7 +155,8 @@ func TestDoWithChallengeRetry(t *testing.T) {defer srv.Close()ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)defer cancel()- if _, err := DoWithChallengeRetry(ctx, srv.Client(), nil, fast, build(srv.URL)); err != context.DeadlineExceeded {+ patient := Backoff{Initial: time.Second, Max: time.Minute, Budget: time.Hour}+ if _, err := DoWithChallengeRetry(ctx, srv.Client(), nil, patient, build(srv.URL)); err != context.DeadlineExceeded {t.Fatalf("err = %v, want context deadline", err)}})diff --git a/site/content/docs/reference/control-plane.md b/site/content/docs/reference/control-plane.mdindex 40aa3dad..6bea8afb 100644--- a/site/content/docs/reference/control-plane.md+++ b/site/content/docs/reference/control-plane.md@@ -76,10 +76,11 @@ names. Every instant in a response (`expire_time` and the like) is aEnrollment is admitted at 10 requests a second for the whole mesh, with aburst of 20. A request over that is answered `429` with a `Retry-After`header. `agentmesh-node` waits for it and sends the request again, with-jitter, for about a minute before it reports the failure, so a fleet of a-few hundred members that starts at once joins over its first minute instead-of losing the members the limiter turned away. A client of your own should-do the same.+jitter, for up to three minutes before it reports the failure, so a fleet of+up to about 1500 members that starts at once joins over its first minutes+instead of losing the members the limiter turned away. A larger fleet starts+in waves; the [scalability guide](../../guides/scalability/) has the+arithmetic. A client of your own should retry the same way.| Route | Body | Purpose ||---|---|---|