Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor main loop #436

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Refactor main loop #436

wants to merge 1 commit into from

Conversation

cyqsimon
Copy link
Collaborator

@cyqsimon cyqsimon commented Oct 9, 2024

Main objectives

  • Cleanly delimiting thread responsibilities
  • Take advantage of more modern Rust language features and APIs
  • Improve readability and maintainability

Discussion

  • Is this message-passing code actually better for readability and maintainability?
  • Honestly, with the significantly more mature async ecosystem in 2024, should we just use tokio?

@cyqsimon
Copy link
Collaborator Author

cyqsimon commented Oct 9, 2024

@YJDoc2 Here's the WIP branch. It's in its initial prototype stages so please be extra critical =).

@YJDoc2
Copy link
Contributor

YJDoc2 commented Oct 9, 2024

Hey, Thanks for this. I took a quick look, and the new structure with messages seems more clean than the original impl with atomic vars.

Is this message-passing code actually better for readability and maintainability?

Atleast from first look it feels such. Will give more feedback once I'm more familiar.

Honestly, with the significantly more mature async ecosystem in 2024, should we just use tokio?

My personal opinion is async should not be added unless there is a significant benefit or requirement for it. I agree that from just message passing perspective async feels like a sensible choice, but if we could manage by native threads, personally I'd prefer that. My reasoning behind not preferring async we have to add a runtime which can be bulky, and can also run into issues with native multi threading.

That said, if you think async is the way to go, I don't have anything against it either.
I'll take a more detailed look into existing code and this implementation and come up with more suggestions. Is there anything specifically you want me to contribute to this? I can open a PR to be merged into this branch for that. Thanks for making this!

@sigmaSd
Copy link
Collaborator

sigmaSd commented Oct 9, 2024

Just saying but we have already tokio in the dependencies (for dns resolution)

cargo tree -i -p tokio
tokio v1.40.0
├── bandwhich v0.23.1 (/home/mrcool/dev/rust/bandwhich_upstream)
├── trust-dns-proto v0.23.2
│   └── trust-dns-resolver v0.23.2
│       └── bandwhich v0.23.1 (/home/mrcool/dev/rust/bandwhich_upstream)
└── trust-dns-resolver v0.23.2 (*)

@cyqsimon
Copy link
Collaborator Author

cyqsimon commented Oct 9, 2024

My personal opinion is async should not be added unless there is a significant benefit or requirement for it. I agree that from just message passing perspective async feels like a sensible choice, but if we could manage by native threads, personally I'd prefer that. My reasoning behind not preferring async we have to add a runtime which can be bulky, and can also run into issues with native multi threading.

Yeah I agree with basically all that's said here. Switching to async is often just substituting one source of problem with another. That being said, @sigmaSd is correct. We wouldn't be bringing in extra dependencies because it's already here.

P.s. It seems like upstream now offers a synchronous API too (although, it's still tokio behind the curtains), so we can get rid of all this:

pub struct Resolver(TokioAsyncResolver);
impl Resolver {
pub async fn new(dns_server: Option<Ipv4Addr>) -> anyhow::Result<Self> {
let resolver = match dns_server {
Some(dns_server_address) => {
let mut config = ResolverConfig::new();
let options = ResolverOpts::default();
let socket = SocketAddr::V4(SocketAddrV4::new(dns_server_address, 53));
let nameserver_config = NameServerConfig {
socket_addr: socket,
protocol: Protocol::Udp,
tls_dns_name: None,
trust_negative_responses: false,
bind_addr: None,
};
config.add_name_server(nameserver_config);
TokioAsyncResolver::tokio(config, options)
}
None => TokioAsyncResolver::tokio_from_system_conf()?,
};
Ok(Self(resolver))
}
}

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.

3 participants