forked from loweel/zabov
				
			if the DNS response Rcode contains an error the cache expires after just 10 seconds.
this should limit the cache in case of temporary upstream DNS errors.golang-1.15-vendoring
							parent
							
								
									2fe0b7b0c2
								
							
						
					
					
						commit
						e2a625a92e
					
				
							
								
								
									
										19
									
								
								02.cache.go
								
								
								
								
							
							
						
						
									
										19
									
								
								02.cache.go
								
								
								
								
							| 
						 | 
				
			
			@ -13,7 +13,7 @@ import (
 | 
			
		|||
 | 
			
		||||
type cacheItem struct {
 | 
			
		||||
	Query []byte
 | 
			
		||||
	Date  time.Time
 | 
			
		||||
	ExpireDate  time.Time
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//DomainCache stores a domain name inside the cache
 | 
			
		||||
| 
						 | 
				
			
			@ -28,7 +28,17 @@ func DomainCache(s string, resp *dns.Msg) {
 | 
			
		|||
	if err != nil {
 | 
			
		||||
		fmt.Println("Problems packing the response: ", err.Error())
 | 
			
		||||
	}
 | 
			
		||||
	domain2cache.Date = time.Now()
 | 
			
		||||
	if resp.Rcode == dns.RcodeSuccess{
 | 
			
		||||
		// on success stores response normally
 | 
			
		||||
		domain2cache.ExpireDate = time.Now().Add((time.Duration(ZabovCacheTTL) * time.Hour))
 | 
			
		||||
	}else
 | 
			
		||||
	{
 | 
			
		||||
		// on failure stores response for a very short time
 | 
			
		||||
		if ZabovDebug {
 | 
			
		||||
			fmt.Println("DomainCache(): DNS error Rcode: ", resp.Rcode, s, "cache time reduced to 10 seconds...")
 | 
			
		||||
		}
 | 
			
		||||
		domain2cache.ExpireDate = time.Now().Add((time.Duration(10) * time.Second))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = enc.Encode(domain2cache)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -77,7 +87,10 @@ func GetDomainFromCache(s string) *dns.Msg {
 | 
			
		|||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if time.Since(record.Date) > (time.Duration(ZabovCacheTTL) * time.Hour) {
 | 
			
		||||
	if time.Now().After(record.ExpireDate) {
 | 
			
		||||
		if ZabovDebug {
 | 
			
		||||
			fmt.Println("GetDomainFromCache(): entry expired:", s)
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue