Skip to content

Add concurrency support#2

Merged
serroba merged 2 commits into
mainfrom
dev
Dec 31, 2025
Merged

Add concurrency support#2
serroba merged 2 commits into
mainfrom
dev

Conversation

@serroba
Copy link
Copy Markdown
Owner

@serroba serroba commented Dec 31, 2025

No description provided.

Copilot AI review requested due to automatic review settings December 31, 2025 03:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds concurrency support to the token bucket rate limiter implementation by introducing mutex-based synchronization. The changes make the Limiter type safe for concurrent use by multiple goroutines.

Key changes:

  • Added a sync.Mutex field to the Limiter struct to protect shared state
  • Protected the Allow() method with mutex lock/unlock operations
  • Added comprehensive concurrent tests to verify thread safety

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
token/rate.go Added sync.Mutex field to Limiter struct and wrapped the Allow() method with lock/unlock operations to ensure thread-safe access to shared state
token/rate_test.go Added two concurrent tests: one verifying correctness of token counting under concurrent access, and another stress-testing the limiter with high concurrency and refill rate

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread token/rate.go
@@ -18,6 +19,7 @@ func (c realClock) Now() time.Time {
// Limiter implements a token bucket rate limiter. It allows a burst of
// requests up to capacity, then refills tokens at the specified rate per second.
Copy link

Copilot AI Dec 31, 2025

Choose a reason for hiding this comment

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

The Limiter documentation should be updated to explicitly mention that it is safe for concurrent use by multiple goroutines. This is an important API property that users need to know about. Consider adding a sentence like "Limiter is safe for concurrent use by multiple goroutines." to the documentation.

Suggested change
// requests up to capacity, then refills tokens at the specified rate per second.
// requests up to capacity, then refills tokens at the specified rate per second.
// Limiter is safe for concurrent use by multiple goroutines.

Copilot uses AI. Check for mistakes.
@serroba serroba merged commit 775aef0 into main Dec 31, 2025
1 check passed
@serroba serroba deleted the dev branch December 31, 2025 03:39
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