Skip to content

lib/ratelimit, lib/concurrentlimit: add protocol-neutral rate and concurrency limiters - #288

Open
Vikramarjuna wants to merge 1 commit into
Cloud-Foundations:masterfrom
Vikramarjuna:rate-limiters
Open

lib/ratelimit, lib/concurrentlimit: add protocol-neutral rate and concurrency limiters#288
Vikramarjuna wants to merge 1 commit into
Cloud-Foundations:masterfrom
Vikramarjuna:rate-limiters

Conversation

@Vikramarjuna

@Vikramarjuna Vikramarjuna commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Adds lib/ratelimit, a three-tier token-bucket rate limiter (global, per-method, per-user-per-method) intended to be shared across SRPC, gRPC and REST handlers so quotas cannot be bypassed by switching protocols. Denials return *errors.ResourceExhaustedError.

Comment thread lib/concurrentlimit/api.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this package needed? Why can't callers use the existing lib/srpc/serverutil package?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped — serverutil is unmodified. I'd lifted it out so a future gRPC/REST handler wouldn't have to import lib/srpc. Will add it later if needed.

Comment thread lib/ratelimit/api.go Outdated
// Protocol identifies the wire protocol that admitted a request. It is used as
// a dimension on rate-limit denial metrics so that quota violations can be
// attributed to SRPC, gRPC or REST.
type Protocol string

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change to uint type and implement a String() method for readable output. See the various examples in the proto directory for the pattern used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Comment thread lib/ratelimit/api.go Outdated
const (
LimitTypeGlobal = "global"
LimitTypePerMethod = "per_method"
LimitTypePerUserPerMethod = "per_user_per_method"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add and use LimitType as a uint type and implement a String() method for readable output. See the various examples in the proto directory for the pattern used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Comment thread lib/ratelimit/impl.go Outdated
return errors.NewResourceExhaustedError(method, LimitTypePerMethod)
}
// Empty username means an unauthenticated caller reached a public or
// unauthenticated method; the per-user-per-method tier has no identity

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unauthenticated callers should not reach a method which is merely marked as public. A method must be marked as allowing unauthenticated access in order for an unauthenticated caller to reach it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants