mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	This checks if the next middleware to be called is nil, and if so returns ServerFailure and an error. This makes the next calling more robust and saves some lines of code. Also prefix the error with the name of the middleware to aid in debugging.
		
			
				
	
	
		
			24 lines
		
	
	
		
			698 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			24 lines
		
	
	
		
			698 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Package loadbalance is middleware for rewriting responses to do "load balancing"
 | |
| package loadbalance
 | |
| 
 | |
| import (
 | |
| 	"github.com/miekg/coredns/middleware"
 | |
| 
 | |
| 	"github.com/miekg/dns"
 | |
| 	"golang.org/x/net/context"
 | |
| )
 | |
| 
 | |
| // RoundRobin is middleware to rewrite responses for "load balancing".
 | |
| type RoundRobin struct {
 | |
| 	Next middleware.Handler
 | |
| }
 | |
| 
 | |
| // ServeDNS implements the middleware.Handler interface.
 | |
| func (rr RoundRobin) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | |
| 	wrr := &RoundRobinResponseWriter{w}
 | |
| 	return middleware.NextOrFailure(rr.Name(), rr.Next, ctx, wrr, r)
 | |
| }
 | |
| 
 | |
| // Name implements the Handler interface.
 | |
| func (rr RoundRobin) Name() string { return "loadbalance" }
 |