git » dnss » commit a775eaa

Use a 2s deadline for RPCs

author Alberto Bertogli
2015-09-07 00:19:58 UTC
committer Alberto Bertogli
2015-09-07 00:19:58 UTC
parent 67cabb97c70574a73804baf113a66ae7568e393d

Use a 2s deadline for RPCs

dnsproxy.go +0 -76
dnstogrpc/dnstogrpc.go +7 -4
grpctodns/grpctodns.go +0 -1

diff --git a/dnsproxy.go b/dnsproxy.go
deleted file mode 100644
index 2fc593b..0000000
--- a/dnsproxy.go
+++ /dev/null
@@ -1,76 +0,0 @@
-// dnsproxy is a simple DNS proxy server.
-package main
-
-import (
-	"fmt"
-	"strings"
-	"sync"
-
-	"github.com/miekg/dns"
-)
-
-func questionsToString(qs []dns.Question) string {
-	var s []string
-	for _, q := range qs {
-		s = append(s, fmt.Sprintf("(%s %s %s)", q.Name,
-			dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]))
-	}
-	return "Q[" + strings.Join(s, " ") + "]"
-}
-
-func rrsToString(rrs []dns.RR) string {
-	var s []string
-	for _, rr := range rrs {
-		s = append(s, fmt.Sprintf("(%s)", rr))
-	}
-	return "RR[" + strings.Join(s, " ") + "]"
-
-}
-
-func L(w dns.ResponseWriter, r *dns.Msg) string {
-	return fmt.Sprintf("%v %v", w.RemoteAddr(), r.Id)
-}
-
-func Handler(w dns.ResponseWriter, r *dns.Msg) {
-	fmt.Printf("%v %v\n", L(w, r), questionsToString(r.Question))
-
-	// TODO: we should create our own IDs, in case different users pick the
-	// same id and we pass that upstream.
-
-	from_up, err := dns.Exchange(r, "8.8.8.8:53")
-	if err != nil {
-		fmt.Printf("%v  ERR: %v\n", L(w, r), err)
-		fmt.Printf("%v  UP: %v\n", L(w, r), from_up)
-	}
-
-	if from_up != nil {
-		if from_up.Rcode != dns.RcodeSuccess {
-			rcode := dns.RcodeToString[from_up.Rcode]
-			fmt.Printf("%v  !->  %v\n", L(w, r), rcode)
-
-		}
-		for _, rr := range from_up.Answer {
-			fmt.Printf("%v  ->  %v\n", L(w, r), rr)
-		}
-		w.WriteMsg(from_up)
-	}
-}
-
-func main() {
-	var wg sync.WaitGroup
-	wg.Add(1)
-	go func() {
-		defer wg.Done()
-		err := dns.ListenAndServe(":5354", "udp", dns.HandlerFunc(Handler))
-		fmt.Printf("Exiting UDP: %v\n", err)
-	}()
-
-	wg.Add(1)
-	go func() {
-		defer wg.Done()
-		err := dns.ListenAndServe(":5354", "tcp", dns.HandlerFunc(Handler))
-		fmt.Printf("Exiting TCP: %v\n", err)
-	}()
-
-	wg.Wait()
-}
diff --git a/dnstogrpc/dnstogrpc.go b/dnstogrpc/dnstogrpc.go
index 3f06e18..94dd9e0 100644
--- a/dnstogrpc/dnstogrpc.go
+++ b/dnstogrpc/dnstogrpc.go
@@ -5,6 +5,7 @@ package dnstogrpc
 import (
 	"fmt"
 	"sync"
+	"time"
 
 	pb "blitiri.com.ar/go/dnss/proto"
 	"blitiri.com.ar/go/dnss/util"
@@ -36,9 +37,12 @@ func (c *grpcclient) Query(r *dns.Msg) (*dns.Msg, error) {
 		return nil, err
 	}
 
-	g, err := c.client.Query(
-		context.Background(),
-		&pb.RawMsg{Data: buf})
+	// Give our RPCs 2 second timeouts: DNS usually doesn't wait that long
+	// anyway.
+	ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+	defer cancel()
+
+	g, err := c.client.Query(ctx, &pb.RawMsg{Data: buf})
 	if err != nil {
 		return nil, err
 	}
@@ -74,7 +78,6 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 	from_up, err := s.client.Query(r)
 	if err != nil {
 		l.Printf("DNS  %v  ERR: %v\n", p(w, r), err)
-		l.Printf("DNS  %v  UP: %v\n", p(w, r), from_up)
 	}
 
 	if from_up != nil {
diff --git a/grpctodns/grpctodns.go b/grpctodns/grpctodns.go
index 5178089..b2ba44c 100644
--- a/grpctodns/grpctodns.go
+++ b/grpctodns/grpctodns.go
@@ -52,7 +52,6 @@ func (s *Server) Query(ctx context.Context, in *pb.RawMsg) (*pb.RawMsg, error) {
 	from_up, err := dns.Exchange(r, s.Upstream)
 	if err != nil {
 		l.Printf("GRPC   ERR: %v\n", err)
-		l.Printf("GRPC   UP: %v\n", from_up)
 		return nil, err
 	}