Skip to content

Add a per-attempt request limiter hook after client-go selects the target store #2035

Description

@solotzg

Add a per-attempt request limiter hook after client-go selects the target store

Background

TiDB needs to limit concurrent coprocessor requests per statement and per TiKV store. The target store is selected inside client-go and may change when a region request is retried, so a caller cannot accurately limit the actual target store before invoking client-go.

The limiter token must cover exactly one RPC attempt. It must be acquired after client-go selects the target store, released exactly once when that attempt finishes, and released before a retry acquires another token.

Proposed API

Add an optional callback to tikvrpc.Request:

type RequestAttemptLimiterFunc func(ctx context.Context, storeID uint64) (release func(), err error)

type Request struct {
    // ...
    RequestAttemptLimiter RequestAttemptLimiterFunc
}

For every synchronous or asynchronous RPC attempt, including retries, client-go calls the callback with the actual target store ID after selecting the store and before starting the RPC. If the callback returns a release function and no error, client-go calls the release function exactly once after the attempt finishes.

When the callback is nil, existing behavior is unchanged.

Error, cancellation, and timing semantics

  • A limiter error terminates the request without sending an RPC.
  • Implementations must return promptly when the request context is canceled.
  • The limiter wait follows the request context and is not bounded by the per-attempt RPC timeout.
  • The per-attempt RPC timeout and RPC runtime statistics start after the limiter allows the attempt.
  • If the callback returns both a release function and an error, client-go calls the release function immediately because no RPC will be sent.
  • If acquiring the existing store-limit token fails after the limiter returns a release function, client-go releases the limiter token.

Compatibility

This is an additive client API. Existing callers are unaffected unless they set RequestAttemptLimiter.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributionThis PR is from a community contributor.first-time-contributorIndicates that the PR was contributed by an external member and is a first-time contributor.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions