Skip to content

feat: add hard memory cap with backpressure to BufferPool - #2976

Merged
oferchen merged 1 commit into
masterfrom
feat/buffer-pool-memory-cap
Mar 22, 2026
Merged

oferchen merged 1 commit into
masterfrom
feat/buffer-pool-memory-cap

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Add configurable hard memory cap to BufferPool via with_memory_cap(bytes) builder method
  • Track outstanding (checked-out) bytes with AtomicUsize - zero overhead when no cap configured
  • Block acquire()/acquire_from() with Condvar-based backpressure when cap is reached
  • Add non-blocking try_acquire()/try_acquire_from() variants returning None at cap
  • Add memory_usage() and memory_cap() public accessors

Closes #1188

Test plan

  • Allocation under cap succeeds
  • try_acquire returns None at cap
  • Blocking acquire unblocks when buffer returned
  • Concurrent pressure with memory cap stays bounded
  • Builder chaining with custom allocator
  • Zero-byte cap panics
  • memory_usage returns 0 without cap
  • CI: fmt, clippy, nextest on all platforms

Add configurable memory cap that tracks outstanding (checked-out) buffer
bytes and blocks acquire calls when the cap is reached. Threads waiting
for memory are unblocked via Condvar when buffers are returned.

- Add MemoryCap struct with AtomicUsize tracking + Mutex/Condvar pair
- Add with_memory_cap() builder method for fluent configuration
- Add memory_usage() and memory_cap() accessors
- Add try_acquire() and try_acquire_from() non-blocking variants
- Blocking acquire waits on Condvar until memory is available
- No overhead when cap is not configured (Option::None fast path)
- Tests cover: under-cap allocation, at-cap backpressure, concurrent
  pressure, try_acquire None at cap, builder chaining, zero-cap panic
@github-actions github-actions Bot added the enhancement New feature or request label Mar 22, 2026
@oferchen
oferchen merged commit 8660101 into master Mar 22, 2026
12 of 17 checks passed
@oferchen
oferchen deleted the feat/buffer-pool-memory-cap branch March 22, 2026 14:10
oferchen added a commit that referenced this pull request May 1, 2026
Add configurable memory cap that tracks outstanding (checked-out) buffer
bytes and blocks acquire calls when the cap is reached. Threads waiting
for memory are unblocked via Condvar when buffers are returned.

- Add MemoryCap struct with AtomicUsize tracking + Mutex/Condvar pair
- Add with_memory_cap() builder method for fluent configuration
- Add memory_usage() and memory_cap() accessors
- Add try_acquire() and try_acquire_from() non-blocking variants
- Blocking acquire waits on Condvar until memory is available
- No overhead when cap is not configured (Option::None fast path)
- Tests cover: under-cap allocation, at-cap backpressure, concurrent
  pressure, try_acquire None at cap, builder chaining, zero-cap panic
oferchen added a commit that referenced this pull request May 5, 2026
Add configurable memory cap that tracks outstanding (checked-out) buffer
bytes and blocks acquire calls when the cap is reached. Threads waiting
for memory are unblocked via Condvar when buffers are returned.

- Add MemoryCap struct with AtomicUsize tracking + Mutex/Condvar pair
- Add with_memory_cap() builder method for fluent configuration
- Add memory_usage() and memory_cap() accessors
- Add try_acquire() and try_acquire_from() non-blocking variants
- Blocking acquire waits on Condvar until memory is available
- No overhead when cap is not configured (Option::None fast path)
- Tests cover: under-cap allocation, at-cap backpressure, concurrent
  pressure, try_acquire None at cap, builder chaining, zero-cap panic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant