Skip to content

Support custom chunk sizes on Connection#22

Merged
HaveFunTrading merged 3 commits intoHaveFunTrading:mainfrom
arb000r:feat/customisable-chunk-size
Nov 3, 2025
Merged

Support custom chunk sizes on Connection#22
HaveFunTrading merged 3 commits intoHaveFunTrading:mainfrom
arb000r:feat/customisable-chunk-size

Conversation

@arb000r
Copy link
Contributor

@arb000r arb000r commented Oct 27, 2025

Wanted to provide own implementation for a connection pool with a custom CHUNK_SIZE. The motivation for this was to support local development on macOS. Attempting to read into a buffer with a capacity larger than the number of bytes available in the stream leads to blocking. (Stream non blocking set to false via socket config)

Ancilliary changes:

  • Connection::new was private, changed visibility, is this fine?
  • disconnected was private, added a getter method for it, also fine?

Main changes:

  • Expose CHUNK_SIZE const param on ConnectionPool with a default to 1024
  • Updating trait to support passing through optionally provided CHUNK_SIZE
  • Pass through CHUNK_SIZE to HttpRequest

Example:

// Custom chunk size provided here.
const MY_ARBITRARY_CHUNK_SIZE: usize = 1;
type HttpTlsConnection = Connection<BufferedStream<TlsStream<TcpStream>>, MY_ARBITRARY_CHUNK_SIZE>;

/// A single-connection pool over TLS, reconnecting on demand.
pub struct SingleTlsConnectionPool {
    connection_info: ConnectionInfo,
    conn: Option<HttpTlsConnection>,
    has_active_connection: bool,
}

impl SingleTlsConnectionPool {
    /// Build a new TLS pool for the given connection info.
    pub fn new(connection_info: impl Into<ConnectionInfo>) -> SingleTlsConnectionPool {
        Self {
            connection_info: connection_info.into(),
            conn: None,
            has_active_connection: false,
        }
    }
}

impl ConnectionPool<MY_ARBITRARY_CHUNK_SIZE> for SingleTlsConnectionPool {
    type Stream = BufferedStream<TlsStream<TcpStream>>;

    fn host(&self) -> &str {
        self.connection_info.host()
    }

    fn acquire(&mut self) -> std::io::Result<Option<Connection<Self::Stream, MY_ARBITRARY_CHUNK_SIZE>>> {
        match (self.conn.take(), self.has_active_connection) {
            (Some(_), true) => {
                // we can at most have one active connection
                unreachable!()
            }
            (Some(stream), false) => {
                self.has_active_connection = true;
                Ok(Some(stream))
            }
            (None, true) => Ok(None),
            (None, false) => {
                let stream = self
                    .connection_info
                    .clone()
                    .into_tcp_stream()?
                    .into_tls_stream_with_config(|tls_cfg| tls_cfg.with_no_cert_verification())?
                    .into_default_buffered_stream();
                self.has_active_connection = true;

                // Visibility change of Connection::new required here
                Ok(Some(Connection::new(stream)))
            }
        }
    }

    fn release(&mut self, conn: Option<Connection<Self::Stream, MY_ARBITRARY_CHUNK_SIZE>>) {
        self.has_active_connection = false;
        if let Some(conn) = conn {
            // Getter method used here
            if !conn.is_disconnected() {
                let _ = self.conn.insert(conn);
            }
        }
    }
}


@HaveFunTrading
Copy link
Owner

Thanks for raising the PR. Will review later this week, probably ok to have chunk size configurable. Just bear in mind boomnet is not supposed to be used with blocking sockets.

@arb000r
Copy link
Contributor Author

arb000r commented Oct 28, 2025

Just bear in mind boomnet is not supposed to be used with blocking sockets.

Yep! This is just to get it working on a macOS context.... probably should looking at setting up a proper linux dev environment

src/http/mod.rs Outdated
}

#[inline]
pub fn is_disconnected(&self) -> bool {
Copy link
Owner

@HaveFunTrading HaveFunTrading Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also be const and please add rustdocs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im of the opinion that this didn't need rustdocs, its pretty self documenting. I will add though

src/http/mod.rs Outdated
/// Represents an in-flight HTTP exchange.
pub struct HttpRequest<C: ConnectionPool> {
conn: Option<Connection<C::Stream>>,
pub struct HttpRequest<C: ConnectionPool<CHUNK_SIZE>, const CHUNK_SIZE: usize = 1024> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's introduce const DEFAULT_CHUNK_SIZE: usize = 1024 and replace hard coded default values

@HaveFunTrading
Copy link
Owner

hi @arb000r looks good, only few minor comments to address

@arb000r
Copy link
Contributor Author

arb000r commented Oct 30, 2025

done

@HaveFunTrading HaveFunTrading merged commit 0277dc1 into HaveFunTrading:main Nov 3, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants