Skip to content

Commit 68776d0

Browse files
committed
Add Timeout newtype wrapper
1 parent 544a564 commit 68776d0

File tree

4 files changed

+34
-27
lines changed

4 files changed

+34
-27
lines changed

examples/status.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use core::time::Duration;
1+
use block_dn_client::Timeout;
22

33
const ENDPOINT: block_dn_client::Endpoint<'static> = block_dn_client::Endpoint::BLOCK_DN_ORG;
4-
const TIMEOUT: Duration = Duration::from_secs(2);
4+
const TIMEOUT: Timeout = Timeout::from_seconds(2);
55

66
fn main() {
77
let mut client_builder = block_dn_client::Builder::new();

examples/taproot.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use core::time::Duration;
21
use std::time::Instant;
32

3+
use block_dn_client::Timeout;
4+
45
const ENDPOINT: block_dn_client::Endpoint<'static> = block_dn_client::Endpoint::BLOCK_DN_ORG;
5-
const TIMEOUT: Duration = Duration::from_secs(5);
6+
const TIMEOUT: Timeout = Timeout::from_seconds(5);
67
const TAPROOT_ACTIVATION_HEIGHT: u32 = 700_000;
78

89
fn main() {

src/lib.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! A Rust client for [`block-dn`](https://github.com/guggero/block-dn#).
22
#![warn(missing_docs)]
3-
use core::time::Duration;
43
use std::{borrow::Cow, io::Cursor, net::SocketAddr};
54

65
use bitcoin::{Block, BlockHash, bip158::BlockFilter, block::Header, consensus::Decodable};
@@ -42,24 +41,41 @@ impl<'e> Endpoint<'e> {
4241
}
4342
}
4443

44+
/// The response timeout permitted.
45+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
46+
pub struct Timeout(u64);
47+
48+
impl Timeout {
49+
/// Build a timeout from number of seconds.
50+
pub const fn from_seconds(seconds: u64) -> Self {
51+
Self(seconds)
52+
}
53+
}
54+
55+
impl Default for Timeout {
56+
fn default() -> Self {
57+
Self(1)
58+
}
59+
}
60+
4561
/// Build a new client to query data for.
4662
#[derive(Debug)]
4763
pub struct Builder<'e> {
4864
endpoint: Endpoint<'e>,
49-
timeout: Duration,
65+
timeout: Timeout,
5066
}
5167

5268
impl<'e> Builder<'e> {
5369
/// Create a new builder [`ClientBuilder`].
5470
pub fn new() -> Self {
5571
Self {
5672
endpoint: Endpoint::BLOCK_DN_ORG,
57-
timeout: Duration::from_secs(1),
73+
timeout: Timeout::default(),
5874
}
5975
}
6076

6177
/// Set the timeout the server has to respond.
62-
pub fn timeout(mut self, timeout: Duration) -> Self {
78+
pub fn timeout(mut self, timeout: Timeout) -> Self {
6379
self.timeout = timeout;
6480
self
6581
}
@@ -89,15 +105,15 @@ impl<'e> Default for Builder<'e> {
89105
#[derive(Debug)]
90106
pub struct Client<'e> {
91107
endpoint: Endpoint<'e>,
92-
timeout: Duration,
108+
timeout: Timeout,
93109
}
94110

95111
impl<'e> Client<'e> {
96112
const EXPECTED_HEADER_LIST_SIZE: usize = 100_000;
97113
/// Return the root HTML of the server.
98114
pub fn index_html(&self) -> Result<Html, Error> {
99115
let response = bitreq::get(self.endpoint.0.to_string())
100-
.with_timeout(self.timeout.as_secs())
116+
.with_timeout(self.timeout.0)
101117
.send()?;
102118
let html = response.as_str()?;
103119
Ok(Html(html.to_string()))
@@ -106,7 +122,7 @@ impl<'e> Client<'e> {
106122
/// Get the status of the server. See [`ServerStatus`] for the response structure.
107123
pub fn status(&self) -> Result<ServerStatus, Error> {
108124
let status = bitreq::get(self.endpoint.append_route("status"))
109-
.with_timeout(self.timeout.as_secs())
125+
.with_timeout(self.timeout.0)
110126
.send()?;
111127
Ok(status.json::<ServerStatus>()?)
112128
}
@@ -116,9 +132,7 @@ impl<'e> Client<'e> {
116132
let route = self
117133
.endpoint
118134
.append_route(format!("headers/{start_height}"));
119-
let response = bitreq::get(route)
120-
.with_timeout(self.timeout.as_secs())
121-
.send()?;
135+
let response = bitreq::get(route).with_timeout(self.timeout.0).send()?;
122136
let mut headers = Vec::with_capacity(Self::EXPECTED_HEADER_LIST_SIZE * 80);
123137
for chunk in response.as_bytes().chunks_exact(80) {
124138
headers.push(bitcoin::consensus::deserialize::<Header>(chunk)?);
@@ -131,9 +145,7 @@ impl<'e> Client<'e> {
131145
let route = self
132146
.endpoint
133147
.append_route(format!("filters/{start_height}"));
134-
let response = bitreq::get(route)
135-
.with_timeout(self.timeout.as_secs())
136-
.send()?;
148+
let response = bitreq::get(route).with_timeout(self.timeout.0).send()?;
137149
let mut cursor = Cursor::new(response.into_bytes());
138150
let mut filters = Vec::new();
139151
while let Ok(bytes) = Vec::<u8>::consensus_decode_from_finite_reader(&mut cursor) {
@@ -147,18 +159,14 @@ impl<'e> Client<'e> {
147159
let route = self
148160
.endpoint
149161
.append_route(format!("sp/tweak-data/{start_height}"));
150-
let response = bitreq::get(route)
151-
.with_timeout(self.timeout.as_secs())
152-
.send()?;
162+
let response = bitreq::get(route).with_timeout(self.timeout.0).send()?;
153163
Ok(response.json::<TapTweaks>()?)
154164
}
155165

156166
/// Fetch the block by its hash.
157167
pub fn block(&self, block_hash: BlockHash) -> Result<Block, Error> {
158168
let route = self.endpoint.append_route(format!("block/{block_hash}"));
159-
let response = bitreq::get(route)
160-
.with_timeout(self.timeout.as_secs())
161-
.send()?;
169+
let response = bitreq::get(route).with_timeout(self.timeout.0).send()?;
162170
let block = bitcoin::consensus::deserialize::<Block>(response.as_bytes())?;
163171
Ok(block)
164172
}

tests/client.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use block_dn_client::{Builder, Client};
1+
use block_dn_client::{Builder, Client, Timeout};
22

33
fn default_client() -> Client<'static> {
44
Builder::default().build()
@@ -31,9 +31,7 @@ fn test_filters() {
3131

3232
#[test]
3333
fn test_tap_tweaks() {
34-
let client = Builder::new()
35-
.timeout(core::time::Duration::from_secs(10))
36-
.build();
34+
let client = Builder::new().timeout(Timeout::from_seconds(10)).build();
3735
let tweaks = client.tweaks(900_000).unwrap();
3836
assert!(!tweaks.blocks.is_empty());
3937
let _ = tweaks.fallible_into_iterator();

0 commit comments

Comments
 (0)