Skip to content

Commit

Permalink
refactor!: Make Config in QuicP2p::from_config non-optional
Browse files Browse the repository at this point in the history
Now that there are no shenanigans needed when setting up default
`Config`, callers can just use `Config::default()`.

BREAKING CHANGE: `QuicP2p::from_config` now takes a `Config` argument,
rather than `Option<Config>`. `Config::default()` (or
`Option::unwrap_or_default`) should be used instead.
  • Loading branch information
Chris Connelly authored and connec committed Aug 27, 2021
1 parent df3fef8 commit 19b3795
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/p2p_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ async fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();

// instantiate QuicP2p with custom config
let qp2p: QuicP2p<XId> = QuicP2p::with_config(Some(Config {
let qp2p: QuicP2p<XId> = QuicP2p::with_config(Config {
idle_timeout_msec: Some(1000 * 3600), // 1 hour idle timeout.
..Default::default()
}))?;
})?;

// create an endpoint for us to listen on and send from.
let (node, _incoming_conns, mut incoming_messages, _disconnections) =
Expand Down
12 changes: 4 additions & 8 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ impl<I: ConnId> QuicP2p<I> {
/// # }
/// # }
///
/// let config = Config::default();
/// let quic_p2p = QuicP2p::<XId>::with_config(Some(config))
/// let quic_p2p = QuicP2p::<XId>::with_config(Config::default())
/// .expect("Error initializing QuicP2p");
/// ```
pub fn with_config(cfg: Option<Config>) -> Result<Self> {
pub fn with_config(cfg: Config) -> Result<Self> {
debug!("Config passed in to qp2p: {:?}", cfg);
let cfg = cfg.unwrap_or_default();

let idle_timeout_msec = cfg.idle_timeout_msec.unwrap_or(DEFAULT_IDLE_TIMEOUT_MSEC);

Expand Down Expand Up @@ -121,9 +119,8 @@ impl<I: ConnId> QuicP2p<I> {
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let config = Config::default();
/// let local_addr = (IpAddr::V4(Ipv4Addr::LOCALHOST), 3000).into();
/// let quic_p2p = QuicP2p::<XId>::with_config(Some(config.clone()))?;
/// let quic_p2p = QuicP2p::<XId>::with_config(Config::default())?;
/// let (mut endpoint, _, _, _) = quic_p2p.new_endpoint(local_addr).await?;
/// let peer_addr = endpoint.socket_addr();
///
Expand Down Expand Up @@ -181,9 +178,8 @@ impl<I: ConnId> QuicP2p<I> {
///
/// #[tokio::main]
/// async fn main() -> Result<(), Error> {
/// let config = Config::default();
/// let local_addr = (IpAddr::V4(Ipv4Addr::LOCALHOST), 0).into();
/// let quic_p2p = QuicP2p::<XId>::with_config(Some(config.clone()))?;
/// let quic_p2p = QuicP2p::<XId>::with_config(Config::default())?;
/// let (endpoint, incoming_connections, incoming_messages, disconnections) = quic_p2p.new_endpoint(local_addr).await?;
/// Ok(())
/// }
Expand Down
4 changes: 1 addition & 3 deletions src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,7 @@ mod tests {

#[tokio::test(flavor = "multi_thread")]
async fn echo_service() -> Result<(), Error> {
let qp2p = QuicP2p::<[u8; 32]>::with_config(Some(Config {
..Config::default()
}))?;
let qp2p = QuicP2p::<[u8; 32]>::with_config(Config::default())?;

// Create Endpoint
let (peer1, mut peer1_connections, _, _) = qp2p.new_endpoint(local_addr()).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ impl ConnId for [u8; 32] {

/// Constructs a `QuicP2p` node with some sane defaults for testing.
pub(crate) fn new_qp2p() -> Result<QuicP2p<[u8; 32]>> {
let qp2p = QuicP2p::<[u8; 32]>::with_config(Some(Config {
let qp2p = QuicP2p::<[u8; 32]>::with_config(Config {
// turn down the retry duration - we won't live forever
// note that this would just limit retries, UDP connection attempts seem to take 60s to
// timeout
retry_duration_msec: 500,
..Config::default()
}))?;
})?;

Ok(qp2p)
}
Expand Down

0 comments on commit 19b3795

Please sign in to comment.