[PR #3582] [CLOSED] fix/refill-token-bucket Closes #3573 #21797

Closed
opened 2026-04-15 20:36:33 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3582
Author: @Adityakk9031
Created: 7/24/2025
Status: Closed

Base: mainHead: fix/refill-token-bucket


📝 Commits (10+)

📊 Changes

2 files changed (+190 additions, -143 deletions)

View changed files

📝 packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts (+158 -103)
📝 packages/better-auth/src/plugins/api-key/rate-limit.ts (+32 -40)

📄 Description

🐛 Problem
Issue #3573 pointed out that the rate limiter currently does not respect refillAmount and refillInterval, even when they are configured in the rateLimit settings. This led to:

Only max and window being enforced.

No gradual token refill happening over time.

refillInterval and refillAmount being ignored during request evaluation.

🔧 Fix Summary
This PR adds token bucket-style refill logic to the rate limiter using refillAmount and refillInterval:

Introduced timestamp-based refill mechanism.

On every request:

Compute elapsed time since last refill.

Refill tokens based on how many intervals passed.

Update token count and last refill timestamp.

Ensured backward compatibility if refill options are not provided.

🧪 Tests Added
Added a dedicated test suite covering:

Initial burst up to max.

New token refilled after 1 second.

Multiple tokens refilled over longer intervals.

Full integration with getSession route.

All tests pass using vitest.

📂 Files Changed
packages/authjs/src/core/rate-limit.ts: Core logic update.

test/core/rate-limit.test.ts: Added tests under new describe("refillAmount and refillInterval logic") block.


Summary by cubic

Added support for gradual token refill in the rate limiter using refillAmount and refillInterval, fixing the issue where these settings were ignored.

  • Bug Fixes
    • Implemented token bucket logic to refill tokens over time based on configured intervals.
    • Updated tests to cover new refill behavior and ensure correct rate limiting.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/3582 **Author:** [@Adityakk9031](https://github.com/Adityakk9031) **Created:** 7/24/2025 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/refill-token-bucket` --- ### 📝 Commits (10+) - [`b1c2b54`](https://github.com/better-auth/better-auth/commit/b1c2b54df89520f84018130ac6815b90e302e299) Closes #3573 - [`1fea6d6`](https://github.com/better-auth/better-auth/commit/1fea6d6059621740d7f6a82c7b40e46f56e33a06) Update rate-limit.ts - [`69561c4`](https://github.com/better-auth/better-auth/commit/69561c43314622eaa09d78e234d5f26ef23c2bf0) Update rate-limit.ts - [`ef88f41`](https://github.com/better-auth/better-auth/commit/ef88f4106a372d7aee65fcdcd673f53c374e5fa7) Update rate-limiter.test.ts - [`14159e5`](https://github.com/better-auth/better-auth/commit/14159e5c474e05fe4bf18adb953d76536b824851) Update rate-limiter.test.ts - [`ad8fa41`](https://github.com/better-auth/better-auth/commit/ad8fa418a429aedd7fa30dc6b7c685d6c8617f5a) Update rate-limiter.test.ts - [`e6ba57e`](https://github.com/better-auth/better-auth/commit/e6ba57e3a0d40111dca532e57808ad1da6c99cb2) Update rate-limiter.test.ts - [`8519f93`](https://github.com/better-auth/better-auth/commit/8519f93f5ede2979cee884616d98df2af885740a) Update rate-limiter.test.ts - [`0258d69`](https://github.com/better-auth/better-auth/commit/0258d69b1f6ee695aab19d1cacb5e69a849f2ad0) a - [`cc73931`](https://github.com/better-auth/better-auth/commit/cc739315b11b504b6c0019ccea61401ad2398629) Update rate-limit.ts ### 📊 Changes **2 files changed** (+190 additions, -143 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts` (+158 -103) 📝 `packages/better-auth/src/plugins/api-key/rate-limit.ts` (+32 -40) </details> ### 📄 Description 🐛 Problem Issue #3573 pointed out that the rate limiter currently does not respect refillAmount and refillInterval, even when they are configured in the rateLimit settings. This led to: Only max and window being enforced. No gradual token refill happening over time. refillInterval and refillAmount being ignored during request evaluation. 🔧 Fix Summary This PR adds token bucket-style refill logic to the rate limiter using refillAmount and refillInterval: Introduced timestamp-based refill mechanism. On every request: Compute elapsed time since last refill. Refill tokens based on how many intervals passed. Update token count and last refill timestamp. Ensured backward compatibility if refill options are not provided. 🧪 Tests Added Added a dedicated test suite covering: Initial burst up to max. New token refilled after 1 second. Multiple tokens refilled over longer intervals. Full integration with getSession route. All tests pass ✅ using vitest. 📂 Files Changed packages/authjs/src/core/rate-limit.ts: Core logic update. test/core/rate-limit.test.ts: Added tests under new describe("refillAmount and refillInterval logic") block. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added support for gradual token refill in the rate limiter using refillAmount and refillInterval, fixing the issue where these settings were ignored. - **Bug Fixes** - Implemented token bucket logic to refill tokens over time based on configured intervals. - Updated tests to cover new refill behavior and ensure correct rate limiting. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 20:36:33 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#21797