mirror of
				https://github.com/coredns/coredns.git
				synced 2025-11-04 03:03:14 -05:00 
			
		
		
		
	middleware/file: reload on file mv (#365)
When a file is moved into position we should also reload the zones' content. This also fixes deadlock bug in the locking, a reload would block any further lookups.
This commit is contained in:
		@@ -29,28 +29,36 @@ func (z *Zone) Lookup(qname string, qtype uint16, do bool) ([]dns.RR, []dns.RR,
 | 
				
			|||||||
		if !z.NoReload {
 | 
							if !z.NoReload {
 | 
				
			||||||
			z.reloadMu.RLock()
 | 
								z.reloadMu.RLock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return z.lookupSOA(do)
 | 
					
 | 
				
			||||||
 | 
							r1, r2, r3, res := z.lookupSOA(do)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if !z.NoReload {
 | 
							if !z.NoReload {
 | 
				
			||||||
			z.reloadMu.RUnlock()
 | 
								z.reloadMu.RUnlock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return r1, r2, r3, res
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if qtype == dns.TypeNS && qname == z.origin {
 | 
						if qtype == dns.TypeNS && qname == z.origin {
 | 
				
			||||||
		if !z.NoReload {
 | 
							if !z.NoReload {
 | 
				
			||||||
			z.reloadMu.RLock()
 | 
								z.reloadMu.RLock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return z.lookupNS(do)
 | 
					
 | 
				
			||||||
 | 
							r1, r2, r3, res := z.lookupNS(do)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if !z.NoReload {
 | 
							if !z.NoReload {
 | 
				
			||||||
			z.reloadMu.RUnlock()
 | 
								z.reloadMu.RUnlock()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
							return r1, r2, r3, res
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if !z.NoReload {
 | 
						if !z.NoReload {
 | 
				
			||||||
		z.reloadMu.RLock()
 | 
							z.reloadMu.RLock()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	elem, res := z.Tree.Search(qname, qtype)
 | 
						elem, res := z.Tree.Search(qname, qtype)
 | 
				
			||||||
	if !z.NoReload {
 | 
						if !z.NoReload {
 | 
				
			||||||
		z.reloadMu.RUnlock()
 | 
							z.reloadMu.RUnlock()
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if elem == nil {
 | 
						if elem == nil {
 | 
				
			||||||
		if res == tree.EmptyNonTerminal {
 | 
							if res == tree.EmptyNonTerminal {
 | 
				
			||||||
			return z.emptyNonTerminal(qname, do)
 | 
								return z.emptyNonTerminal(qname, do)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -123,8 +123,10 @@ func (z *Zone) TransferAllowed(req request.Request) bool {
 | 
				
			|||||||
// All returns all records from the zone, the first record will be the SOA record,
 | 
					// All returns all records from the zone, the first record will be the SOA record,
 | 
				
			||||||
// otionally followed by all RRSIG(SOA)s.
 | 
					// otionally followed by all RRSIG(SOA)s.
 | 
				
			||||||
func (z *Zone) All() []dns.RR {
 | 
					func (z *Zone) All() []dns.RR {
 | 
				
			||||||
	z.reloadMu.RLock()
 | 
						if !z.NoReload {
 | 
				
			||||||
	defer z.reloadMu.RUnlock()
 | 
							z.reloadMu.RLock()
 | 
				
			||||||
 | 
							defer z.reloadMu.RUnlock()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	records := []dns.RR{}
 | 
						records := []dns.RR{}
 | 
				
			||||||
	allNodes := z.Tree.All()
 | 
						allNodes := z.Tree.All()
 | 
				
			||||||
@@ -162,7 +164,9 @@ func (z *Zone) Reload() error {
 | 
				
			|||||||
		for {
 | 
							for {
 | 
				
			||||||
			select {
 | 
								select {
 | 
				
			||||||
			case event := <-watcher.Events:
 | 
								case event := <-watcher.Events:
 | 
				
			||||||
				if event.Op == fsnotify.Write && path.Clean(event.Name) == z.file {
 | 
									// Looks for Write and Create events. Write is obvious, Create is used when
 | 
				
			||||||
 | 
									// a file in mv-ed into this place.
 | 
				
			||||||
 | 
									if (event.Op == fsnotify.Write || event.Op == fsnotify.Create) && path.Clean(event.Name) == z.file {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					reader, err := os.Open(z.file)
 | 
										reader, err := os.Open(z.file)
 | 
				
			||||||
					if err != nil {
 | 
										if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user