safa_buffer_pool/
builder.rs

1use std::time::Duration;
2
3use crate::context::{mono_thread, multi_thread};
4
5///A builder for BufferPool
6pub struct BufferPoolBuilder {
7    pub max_nb_buffer: usize,
8    pub min_nb_buffer: usize,
9    pub buffer_size: usize,
10    pub over_buffer_lifetime_opt: Option<Duration>,
11}
12
13impl Default for BufferPoolBuilder {
14    fn default() -> Self {
15        Self {
16            max_nb_buffer: 1024,
17            min_nb_buffer: 1024,
18            buffer_size: 10240,
19            over_buffer_lifetime_opt: None,
20        }
21    }
22}
23
24impl BufferPoolBuilder {
25    ///Create a new BufferPoolBuilder
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    ///Set the maximum of buffer in the pool
31    pub fn set_max_number_of_buffer(&mut self, number: usize) -> &mut Self {
32        self.max_nb_buffer = number;
33        self
34    }
35    ///Set the minimum of buffer in the pool
36    pub fn set_min_number_of_buffer(&mut self, number: usize) -> &mut Self {
37        self.min_nb_buffer = number;
38        if self.max_nb_buffer < number {
39            self.max_nb_buffer = number;
40        }
41        self
42    }
43    ///Set the size of each buffer in the pool
44    pub fn set_buffer_size(&mut self, number: usize) -> &mut Self {
45        self.buffer_size = number;
46        self
47    }
48    ///Only work on Multi-thread pool: Set the maximum inactivity time for excess buffers before being deleted
49    pub fn set_over_buffer_lifetime(&mut self, new_duration: Duration) -> &mut Self {
50        self.over_buffer_lifetime_opt = Some(new_duration);
51        self
52    }
53
54    ///Build a mono thread pool from this builder
55    pub fn build_mono_thread(&self) -> mono_thread::BufferPool {
56        mono_thread::BufferPool::from_builder(self)
57    }
58    ///Build a multi thread pool from this builder
59    pub fn build_multi_thread(&self) -> multi_thread::BufferPool {
60        multi_thread::BufferPool::from_builder(self)
61    }
62}