Skip to content

Instantly share code, notes, and snippets.

@blark
Created January 15, 2018 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blark/fa196f4868242c5bdf58f8904a95c955 to your computer and use it in GitHub Desktop.
Save blark/fa196f4868242c5bdf58f8904a95c955 to your computer and use it in GitHub Desktop.
Asynchronous DNS requests with Nim
import asyncdispatch
import asynctools/asyncdns
type
DNSQueryResult = ref object
name: string
error: bool
ans: ptr AsyncAddrInfo
proc lookup(n: string): Future[DNSQueryResult] {.async.} =
var response = await asyncGetAddrInfo(n, Port(0))
return DNSQueryResult(name:n, error:false, ans:response)
proc main() {.async.} =
let domains = @["www.google.ca", "www.microsoft.com", "autodiscover-nameast3.outlook.com"]
var futures = newSeq[Future[DNSQueryResult]]()
for domain in domains:
futures.add(lookup(domain))
let results = await all futures
for r in results:
echo(r.name)
echo(r.ans)
echo()
waitFor main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment