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

fix(stdlib): properly detect tokio runtime in dns_lookup #882

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ stdlib = [
"dep:snap",
"dep:strip-ansi-escapes",
"dep:syslog_loose",
"dep:tokio",
"dep:uaparser",
"dep:url",
"dep:utf8-width",
Expand Down Expand Up @@ -203,6 +204,7 @@ domain = { version = "0.10.0", optional = true, features = ["resolv-sync", "serd
hostname = { version = "0.4", optional = true }
grok = { version = "2", optional = true }
onig = { version = "6", default-features = false, optional = true }
tokio = { version = "1.37", optional = true, features = ["io-util", "macros", "net", "time", "sync", "rt", "rt-multi-thread" ] }
uuid = { version = "1", features = ["v4", "v7"], optional = true }

# Dependencies used for WASM
Expand Down
17 changes: 14 additions & 3 deletions src/stdlib/dns_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::compiler::prelude::*;
mod non_wasm {
use std::collections::BTreeMap;
use std::net::ToSocketAddrs;
use std::thread;
use std::time::Duration;

use domain::base::iana::Class;
Expand All @@ -12,6 +13,7 @@ mod non_wasm {
use domain::resolv::stub::conf::{ResolvConf, ResolvOptions, ServerConf, Transport};
use domain::resolv::stub::Answer;
use domain::resolv::StubResolver;
use tokio::runtime::Handle;

use crate::compiler::prelude::*;
use crate::value::Value;
Expand All @@ -34,9 +36,18 @@ mod non_wasm {
.map_err(|err| format!("parsing query class failed: {err}"))?;

let conf = build_options(options.try_object()?)?;
let answer = StubResolver::run_with_conf(conf, move |stub| async move {
stub.query((host, qtype, qclass)).await
})
let answer = match Handle::try_current() {
Ok(_) => thread::spawn(move || {
Copy link
Member

@jszwedko jszwedko Jun 7, 2024

Choose a reason for hiding this comment

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

I think if it returns Ok you can use something like:

futures::executor::block_on(async {
        handle
            .spawn(async {
            ...
         })
})

to just execute in the current Tokio runtime instead of spawning a separate thread per https://stackoverflow.com/a/62536772

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I still have to await on return value of block_on which bring us back to the same issue. I think we will have to go with a pool or a dedicated thread until proper async support is added to VRL (if it ever gets added, since it maybe clashes with original goals).

Copy link
Member

Choose a reason for hiding this comment

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

Ah gotcha. Then yeah, a pool seems like a good approach. If/when VRL functions become async we can revisit this. I was going to suggest we thread the through the runtime from Vector into VRL, but I think I'd prefer to just make the functions async than do that.

StubResolver::run_with_conf(conf, move |stub| async move {
stub.query((host, qtype, qclass)).await
})
})
.join()
.map_err(|_| format!("starting a thread for the lookup failed"))?,
Err(_) => StubResolver::run_with_conf(conf, move |stub| async move {
stub.query((host, qtype, qclass)).await
}),
}
.map_err(|err| format!("query failed: {err}"))?;

Ok(parse_answer(answer)?.into())
Expand Down
Loading