Midwess
Est. 2025 • Mekong Delta, South Vietnam

Software that feels
like magic

Midwess is a boutique startup studio and open knowledge base. We build high-performance productivity tools. We are dedicated to building user-centric solutions that enable modern engineering teams to achieve more through reliable, high-speed tools.

Vibe
High Fidelity
Heart
Mekong Delta
~/midwess/dna.json
ethos
{
  "mission": "Create moments of delight",
  "vision": "Software worth loving",
  "craft": ["Rust", "Next.js", "AI"],
  "location": "Mekong Delta",
  "status": "Building magic"
}
Portfolio

Our Products

Bytover Magic

Experience the freedom of instant sharing. No cloud, no zips, no waiting. Just a link and your files move like magic, directly between devices.

Background

Bytover
Direct

Why upload when you can just share? Bytover creates a direct bridge between devices, giving you total control and unmatched speed.

Instant
Private
Blog posts.

Where curiosity meets the keyboard.

Building software is a journey of discovery. We share the stories, the mistakes, and the breakthroughs behind the products we craft.

Article3 min read

Deploy Flutter Web with GitHub Actions

A step-by-step guide to deploying Flutter web applications using Docker and GitHub Actions for automated delivery.

Jan 20, 2026
Article3 min read

Implementing a Custom Resource Pool in Rust

A deep dive into building an efficient, thread-safe resource pool in Rust to optimize performance.

Dec 1, 2025
Article3 min read

Understanding Polymorphism in Rust

A clear and simple guide to how Rust handles polymorphism using Traits and Enums.

Dec 15, 2025
Article3 min read

My Journey from JavaScript to Rust

Why I moved from JavaScript to Rust for backend services and a simple plan to help you do the same.

Jan 5, 2026

Implementing a Custom Resource Pool in Rust

December 1, 20253 min read

Why Resource Pooling?

Every software system interacts with finite resources: database connections, network sockets, or open files. Creating these resources from scratch is expensive. For instance, establishing a new PostgreSQL connection requires a TCP handshake, authentication, and backend process startup. Doing this for every single request in a high-traffic app would waste a massive amount of CPU cycles.

A Resource Pool solves this by keeping a cache of already-open connections ready for reuse. Instead of creating a new one, your application "borrows" a connection, uses it, and then "returns" it to the pool.

In this guide, we will build a custom, asynchronous resource pool in Rust using the Tokio runtime.

The Architecture

Our pool will consist of four main components:

  1. Resource Provider: A way to define how new resources are created.
  2. Pool Item: A wrapper to track the state of a specific resource.
  3. The Pool: The core manager that stores and distributes resources.
  4. The Response: A smart wrapper that automatically returns the resource when finished.

1. Defining the Resource Provider

We need an abstraction that allows the pool to create new resources without knowing their implementation details.

#[async_trait]
pub trait PoolResourceProvider<T>: Send + Sync where T: Send + Sync {
    async fn create_new(&self) -> T;
}

2. The Pool Item

Each resource is wrapped in a PoolItem to track how long it has been sitting idle.

pub struct PoolItem<T> {
    resource: Box<T>,
    created_at: Instant,
    max_idle_time: Duration,
}
 
impl<T> PoolItem<T> {
    pub fn is_expired(&self) -> bool {
        self.created_at.elapsed() > self.max_idle_time
    }
}

3. Managing the Pool

The pool manager handles two limits: min_size (keep these open always) and max_size (the absolute maximum). It uses a Mutex to protect the resource list.

pub struct Pool<T> {
    min_size: usize,
    max_size: usize,
    available: Mutex<VecDeque<PoolItem<T>>>,
    total_count: Mutex<usize>,
}

Handling Concurrent Requests

What happens when your app requests a connection but the pool is full?

  1. Check Available: If the pool has an idle resource, return it immediately.
  2. Create New: If the pool is below max_size, create a new one.
  3. Wait: If the pool is at max_size, put the request in a "waiter queue" using a one-shot channel.
pub async fn retrieve(&self) -> Result<PoolResponse<T>, oneshot::Receiver<PoolResponse<T>>> {
    let mut items = self.available.lock().await;
    if let Some(item) = items.pop_front() {
        return Ok(PoolResponse::new(item, self.clone()));
    }
    
    // Logic to create new or wait...
}

Automatic Return using the Drop Trait

The "magic" of a resource pool in Rust is that we can ensure resources return home without the user doing anything. By implementing the Drop trait on our response wrapper, the resource is automatically sent back to the pool as soon as the variable goes out of scope.

pub struct PoolResponse<T> {
    item: Option<PoolItem<T>>,
    pool: Arc<PoolAllocator<T>>,
}
 
impl<T> Drop for PoolResponse<T> {
    fn drop(&mut self) {
        if let Some(item) = self.item.take() {
            let pool = self.pool.clone();
            tokio::spawn(async move {
                pool.return_resource(item).await;
            });
        }
    }
}

Efficient Cleanup

We don't want unused resources to sit in memory forever. We implement a Cleaner that runs in the background. It wakes up periodically and closes resources that are expired and above the min_size requirement.

To optimize, the cleaner doesn't just run every few seconds. It calculates the time until the next item will expire and sleeps for exactly that duration.

Conclusion

Building your own resource pool in Rust gives you total control over how your application manages its most expensive assets. By using Arc, Mutex, and the Drop trait, we’ve created a system that is not only fast but also extremely safe. You can use this logic for database connections, HTTP clients, or any other resource that is expensive to initialize.

Midwess Software that feels like magic.

We believe software should be more than just a tool. It should be a seamless, high-fidelity experience that delights the people who use it.