dns: split node lookup from request handling

This commit is contained in:
Frank Schroeder 2017-08-21 14:05:39 +02:00 committed by Frank Schröder
parent db8ad8922e
commit 8e1f9b9b68
1 changed files with 16 additions and 9 deletions

View File

@ -679,9 +679,8 @@ func trimUDPResponse(config *DNSConfig, req, resp *dns.Msg) (trimmed bool) {
return len(resp.Answer) < numAnswers return len(resp.Answer) < numAnswers
} }
// serviceLookup is used to handle a service query // lookupServiceNodes returns nodes with a given service.
func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req, resp *dns.Msg) { func (d *DNSServer) lookupServiceNodes(datacenter, service, tag string) (structs.IndexedCheckServiceNodes, error) {
// Make an RPC request
args := structs.ServiceSpecificRequest{ args := structs.ServiceSpecificRequest{
Datacenter: datacenter, Datacenter: datacenter,
ServiceName: service, ServiceName: service,
@ -695,9 +694,7 @@ func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req,
var out structs.IndexedCheckServiceNodes var out structs.IndexedCheckServiceNodes
if err := d.agent.RPC("Health.ServiceNodes", &args, &out); err != nil { if err := d.agent.RPC("Health.ServiceNodes", &args, &out); err != nil {
d.logger.Printf("[ERR] dns: rpc error: %v", err) return structs.IndexedCheckServiceNodes{}, err
resp.SetRcode(req, dns.RcodeServerFailure)
return
} }
if args.AllowStale && out.LastContact > staleCounterThreshold { if args.AllowStale && out.LastContact > staleCounterThreshold {
@ -710,15 +707,25 @@ func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req,
d.logger.Printf("[WARN] dns: Query results too stale, re-requesting") d.logger.Printf("[WARN] dns: Query results too stale, re-requesting")
if err := d.agent.RPC("Health.ServiceNodes", &args, &out); err != nil { if err := d.agent.RPC("Health.ServiceNodes", &args, &out); err != nil {
d.logger.Printf("[ERR] dns: rpc error: %v", err) return structs.IndexedCheckServiceNodes{}, err
resp.SetRcode(req, dns.RcodeServerFailure)
return
} }
} }
// Filter out any service nodes due to health checks // Filter out any service nodes due to health checks
out.Nodes = out.Nodes.Filter(d.config.OnlyPassing) out.Nodes = out.Nodes.Filter(d.config.OnlyPassing)
return out, nil
}
// serviceLookup is used to handle a service query
func (d *DNSServer) serviceLookup(network, datacenter, service, tag string, req, resp *dns.Msg) {
out, err := d.lookupServiceNodes(datacenter, service, tag)
if err != nil {
d.logger.Printf("[ERR] dns: rpc error: %v", err)
resp.SetRcode(req, dns.RcodeServerFailure)
return
}
// If we have no nodes, return not found! // If we have no nodes, return not found!
if len(out.Nodes) == 0 { if len(out.Nodes) == 0 {
d.addSOA(resp) d.addSOA(resp)