Created
January 15, 2018 21:04
-
-
Save blark/fa196f4868242c5bdf58f8904a95c955 to your computer and use it in GitHub Desktop.
Asynchronous DNS requests with Nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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