Skip to content

Commit

Permalink
move TCP pool from TcpListener to TcpProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Feb 26, 2024
1 parent 59c0615 commit c6ee01b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
10 changes: 2 additions & 8 deletions lib/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ pub struct Server {
max_poll_errors: i32, // TODO: make this configurable? this defaults to 10000 for now
pub poll: Poll,
poll_timeout: Option<Duration>, // TODO: make this configurable? this defaults to 1000 milliseconds for now
pool: Rc<RefCell<Pool>>,
scm_listeners: Option<Listeners>,
scm: ScmSocket,
sessions: Rc<RefCell<SessionManager>>,
Expand Down Expand Up @@ -378,7 +377,7 @@ impl Server {
.try_clone()
.map_err(ServerError::CloneRegistry)?;

tcp::TcpProxy::new(registry, sessions.clone(), backends.clone())
tcp::TcpProxy::new(registry, sessions.clone(), pool.clone(), backends.clone())
}
}));

Expand All @@ -400,7 +399,6 @@ impl Server {
max_poll_errors: 10000, // TODO: make it configurable?
poll_timeout: Some(Duration::milliseconds(1000)), // TODO: make it configurable?
poll,
pool,
scm_listeners: None,
scm,
sessions,
Expand Down Expand Up @@ -1162,11 +1160,7 @@ impl Server {
let entry = session_manager.slab.vacant_entry();
let token = Token(entry.key());

match self
.tcp
.borrow_mut()
.add_listener(listener, self.pool.clone(), token)
{
match self.tcp.borrow_mut().add_listener(listener, token) {
Ok(_token) => {
entry.insert(Rc::new(RefCell::new(ListenSession {
protocol: Protocol::TCPListen,
Expand Down
15 changes: 7 additions & 8 deletions lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,6 @@ pub struct TcpListener {
cluster_id: Option<String>,
config: TcpListenerConfig,
listener: Option<MioTcpListener>,
pool: Rc<RefCell<Pool>>,
tags: BTreeMap<String, CachedTags>,
token: Token,
}
Expand All @@ -1028,15 +1027,13 @@ impl ListenerHandler for TcpListener {
impl TcpListener {
fn new(
config: TcpListenerConfig,
pool: Rc<RefCell<Pool>>,
token: Token,
) -> Result<TcpListener, ListenerError> {
Ok(TcpListener {
cluster_id: None,
listener: None,
token,
address: config.address.clone().into(),
pool,
config,
active: false,
tags: BTreeMap::new(),
Expand Down Expand Up @@ -1108,12 +1105,14 @@ pub struct TcpProxy {
configs: HashMap<ClusterId, ClusterConfiguration>,
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
pool: Rc<RefCell<Pool>>,
}

impl TcpProxy {
pub fn new(
registry: Registry,
sessions: Rc<RefCell<SessionManager>>,
pool: Rc<RefCell<Pool>>,
backends: Rc<RefCell<BackendMap>>,
) -> TcpProxy {
TcpProxy {
Expand All @@ -1123,20 +1122,20 @@ impl TcpProxy {
fronts: HashMap::new(),
registry,
sessions,
pool,
}
}

// TODO: return Result with context
pub fn add_listener(
&mut self,
config: TcpListenerConfig,
pool: Rc<RefCell<Pool>>,
token: Token,
) -> Result<Token, ProxyError> {
match self.listeners.entry(token) {
Entry::Vacant(entry) => {
let tcp_listener =
TcpListener::new(config, pool, token).map_err(ProxyError::AddListener)?;
TcpListener::new(config, token).map_err(ProxyError::AddListener)?;
entry.insert(Rc::new(RefCell::new(tcp_listener)));
Ok(token)
}
Expand Down Expand Up @@ -1358,7 +1357,7 @@ impl ProxyConfiguration for TcpProxy {
.ok_or(AcceptError::IoError)?;

let owned = listener.borrow();
let mut pool = owned.pool.borrow_mut();
let mut pool = self.pool.borrow_mut();

let (front_buffer, back_buffer) = match (pool.checkout(), pool.checkout()) {
(Some(fb), Some(bb)) => (fb, bb),
Expand Down Expand Up @@ -1466,9 +1465,9 @@ pub mod testing {
Token(key)
};

let mut proxy = TcpProxy::new(registry, sessions.clone(), backends.clone());
let mut proxy = TcpProxy::new(registry, sessions.clone(), pool.clone(), backends.clone());
proxy
.add_listener(config, pool.clone(), token)
.add_listener(config, token)
.with_context(|| "Failed at creating adding the listener")?;
proxy
.activate_listener(&address, None)
Expand Down

0 comments on commit c6ee01b

Please sign in to comment.