Return a buffer to the pool for reuse
(&self, buffer: BytesMut)
| 192 | |
| 193 | /// Return a buffer to the pool for reuse |
| 194 | fn return_buffer(&self, buffer: BytesMut) { |
| 195 | let mut inner = self.inner.lock(); |
| 196 | |
| 197 | // Check if we should discard this buffer |
| 198 | if buffer.capacity() > self.config.max_buffer_capacity { |
| 199 | inner.stats.buffers_discarded += 1; |
| 200 | |
| 201 | // Record deallocation in global memory monitor (skip in tests) |
| 202 | #[cfg(not(test))] |
| 203 | global_memory_monitor().record_buffer_deallocation(buffer.capacity() as u64); |
| 204 | |
| 205 | if self.config.enable_monitoring { |
| 206 | debug!("Discarded oversized buffer: capacity={}B > max={}B", |
| 207 | buffer.capacity(), self.config.max_buffer_capacity); |
| 208 | } |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Check if pool is full |
| 213 | if inner.buffers.len() >= self.config.max_pool_size { |
| 214 | inner.stats.buffers_discarded += 1; |
| 215 | |
| 216 | // Record deallocation in global memory monitor (skip in tests) |
| 217 | #[cfg(not(test))] |
| 218 | global_memory_monitor().record_buffer_deallocation(buffer.capacity() as u64); |
| 219 | |
| 220 | if self.config.enable_monitoring { |
| 221 | debug!("Discarded buffer: pool full (size={})", inner.buffers.len()); |
| 222 | } |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Return buffer to pool |
| 227 | let pooled_buffer = PooledBuffer { |
| 228 | buffer, |
| 229 | last_used: Instant::now(), |
| 230 | allocation_count: 1, |
| 231 | }; |
| 232 | |
| 233 | let buffer_capacity = pooled_buffer.buffer.capacity(); |
| 234 | |
| 235 | inner.buffers.push_back(pooled_buffer); |
| 236 | inner.stats.buffers_returned += 1; |
| 237 | inner.stats.current_pool_size = inner.buffers.len(); |
| 238 | |
| 239 | if inner.stats.current_pool_size > inner.stats.peak_pool_size { |
| 240 | inner.stats.peak_pool_size = inner.stats.current_pool_size; |
| 241 | } |
| 242 | |
| 243 | if self.config.enable_monitoring { |
| 244 | debug!("Returned buffer to pool: capacity={}B, pool_size={}", |
| 245 | buffer_capacity, inner.buffers.len()); |
| 246 | } |
| 247 | |
| 248 | // Perform cleanup if needed |
| 249 | if inner.last_cleanup.elapsed() > self.config.cleanup_interval { |
| 250 | self.cleanup_old_buffers(&mut inner); |
| 251 | } |
no test coverage detected