Skip to content

Commit 665d8ca

Browse files
committed
Merge branch 'master' of github.com:oxidecomputer/qorb
2 parents a633b63 + f09fcf0 commit 665d8ca

File tree

5 files changed

+103
-39
lines changed

5 files changed

+103
-39
lines changed

.github/workflows/rust.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ jobs:
1313
check-style:
1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
16+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
1717
- name: Report rustfmt version
1818
run: cargo fmt -- --version
1919
- name: Check style
2020
run: cargo fmt -- --check
2121
build-docs:
2222
runs-on: ubuntu-latest
2323
steps:
24-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
24+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
2525
- name: Test build documentation
2626
run: cargo doc
2727
build-and-test:
2828
runs-on: ubuntu-latest
2929
steps:
30-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
30+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
3131
- name: Build
3232
run: cargo build --workspace --verbose
3333
- name: Install latest nextest release
@@ -37,6 +37,6 @@ jobs:
3737
clippy:
3838
runs-on: ubuntu-latest
3939
steps:
40-
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
40+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4
4141
- name: Test Libraries
4242
run: cargo clippy --workspace --all-targets

Cargo.lock

Lines changed: 34 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ serde_json = { version = "1.0" }
2121
thiserror = "1.0"
2222
tokio = { version = "1.38"}
2323
tokio-stream = { version = "0.1", features = [ "sync" ] }
24-
tokio-tungstenite = "0.23"
24+
tokio-tungstenite = "0.24"
2525
tracing = "0.1"
2626
crossterm = "0.28.1"
2727
hickory-client = { version = "0.24", default-features = false }

src/resolvers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//! Default implementations of [crate::resolver::Resolver]
22
33
pub mod dns;
4+
pub mod single_host;

src/resolvers/single_host.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! Implementation of [Resolver] that always returns an explicit address.
2+
3+
use tokio::sync::watch;
4+
5+
use crate::backend;
6+
use crate::resolver::{AllBackends, Resolver};
7+
8+
use std::collections::BTreeMap;
9+
use std::net::SocketAddr;
10+
use std::sync::Arc;
11+
12+
/// A [`Resolver`] that always returns a single address.
13+
#[derive(Clone, Debug)]
14+
pub struct SingleHostResolver {
15+
tx: watch::Sender<AllBackends>,
16+
}
17+
18+
const SINGLE_HOST_BACKEND_NAME: &str = "singleton";
19+
20+
impl SingleHostResolver {
21+
/// Construct a resolver to always return the provided address.
22+
pub fn new(address: SocketAddr) -> Self {
23+
let backends = Arc::new(BTreeMap::from([(
24+
backend::Name::new(SINGLE_HOST_BACKEND_NAME),
25+
backend::Backend { address },
26+
)]));
27+
let (tx, _rx) = watch::channel(backends.clone());
28+
Self { tx }
29+
}
30+
}
31+
32+
impl Resolver for SingleHostResolver {
33+
fn monitor(&mut self) -> watch::Receiver<AllBackends> {
34+
self.tx.subscribe()
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
41+
42+
use crate::{
43+
backend::Backend, resolver::Resolver as _, resolvers::single_host::SINGLE_HOST_BACKEND_NAME,
44+
};
45+
46+
use super::SingleHostResolver;
47+
48+
#[test]
49+
fn single_host_resolver_returns_address() {
50+
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 4444);
51+
let mut res = SingleHostResolver::new(addr);
52+
let rx = res.monitor();
53+
let backends = rx.borrow();
54+
assert_eq!(backends.len(), 1);
55+
let Backend { address } = backends
56+
.get(SINGLE_HOST_BACKEND_NAME)
57+
.expect("Expected the single host backend name");
58+
assert_eq!(
59+
address, &addr,
60+
"Single host resolver returned wrong address"
61+
);
62+
}
63+
}

0 commit comments

Comments
 (0)