How do I manage a Pool of Pools of TCPStreams?

I've gone back and forth deciding how to design a solution to the following problem. I need someone's wisdom for the correct way to do this.

I am writing an SMTP server. When I send an email I need to open a TCPStream to the recipient's mail server. I need to also hold that connection open for a few mins afterwards (time-to-idle) on the chance that I have another email for that domain so I can reuse the connection. Each connection needs a queue of mail waiting to be sent on it, so if that queue gets too large we know to open an additional connection to increase our throughput.

I've considered numerous ways of doing this but I can't really decide the proper way, and I'm also curious if there is a well known pattern or library for this problem.

Attempt 1: A OnceCell with Moka (moka - Rust) with a String of the mail server's domain as the key and a value of Arc<RWLock<Vec < mpsc > >> (simplifying the type a little but the idea is the same) the mpsc here is connected to the actual TCP connection which then sends the data that comes through it.

I believe this method will work but the amount of locking seems like it will be excessive so I tried to think of other ways.

Attempt 2: one Tokio task per domain manages that domain's connection pool, and I still use Moka for handing out the mpsc Sender for the Tokio task that manages the pool. this eliminates the locking but now has other problems like managing a buffer of mail to be then assigned to a connection (Vec< mpsc>) which means more channels to pass data through which seems inefficient, but maybe not as inefficient as locks?.

I could go into a lot more detail about each approach but I don't want to make this too long. Any ideas are appreciated.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.