Skip to content

Instantly share code, notes, and snippets.

@bhcleek
Created May 12, 2020 00:30
Show Gist options
  • Save bhcleek/cba55971319bf23639ee4f3d1718ae34 to your computer and use it in GitHub Desktop.
Save bhcleek/cba55971319bf23639ee4f3d1718ae34 to your computer and use it in GitHub Desktop.
dnschecker
package main
import (
"context"
"flag"
"log"
"net"
"syscall"
"time"
"do/doge/context/sigctx"
)
func main() {
name := flag.String("name", "kube-dns.kube-system.svc", "a DNS name to resolve")
interval := flag.Duration("frequency", time.Second, "the frequency for each worker")
flag.Parse()
log.Println("resolving", *name)
t := time.Tick(5 * *interval)
ctx := sigctx.WithSignal(context.Background(), syscall.SIGTERM)
for i := 0; i < 10; i++ {
go func() {
t := time.Tick(*interval)
for {
<-t
resolve(*name, false)
}
}()
}
for {
select {
case <-ctx.Done():
log.Println("done")
return
case <-t:
resolve(*name, true)
}
}
}
func resolve(name string, output bool) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
r := net.Resolver{}
addrs, err := r.LookupHost(ctx, name)
if err != nil {
log.Println(err)
}
if output {
log.Println("addrs =", addrs)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment