mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	middleware/cache: only cache query and responses (#397)
Extent typify to check the transfers, dynamic updates and notifies. Extend *cache* to not put these in the cache. Fixes #393
This commit is contained in:
		
							
								
								
									
										10
									
								
								middleware/cache/cache.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								middleware/cache/cache.go
									
									
									
									
										vendored
									
									
								
							| @@ -29,13 +29,16 @@ type Cache struct { | ||||
| 	pttl   time.Duration | ||||
| } | ||||
|  | ||||
| // Return key under which we store the item. | ||||
| // Return key under which we store the item. The empty string is returned | ||||
| // when we don't want to cache the message. Currently we do not cache Truncated, errors | ||||
| // zone transfers or dynamic update messages. | ||||
| func key(m *dns.Msg, t response.Type, do bool) string { | ||||
| 	// We don't store truncated responses. | ||||
| 	if m.Truncated { | ||||
| 		// TODO(miek): wise to store truncated responses? | ||||
| 		return "" | ||||
| 	} | ||||
| 	if t == response.OtherError { | ||||
| 	// Nor errors or Meta or Update | ||||
| 	if t == response.OtherError || t == response.Meta || t == response.Update { | ||||
| 		return "" | ||||
| 	} | ||||
|  | ||||
| @@ -65,6 +68,7 @@ func (c *ResponseWriter) WriteMsg(res *dns.Msg) error { | ||||
| 		do = opt.Do() | ||||
| 	} | ||||
|  | ||||
| 	// key returns empty string for anything we don't want to cache. | ||||
| 	key := key(res, mt, do) | ||||
|  | ||||
| 	duration := c.pttl | ||||
|   | ||||
| @@ -14,11 +14,15 @@ const ( | ||||
| 	NoError Type = iota | ||||
| 	// NameError is a NXDOMAIN in header, SOA in auth. | ||||
| 	NameError | ||||
| 	// NoData indicated name found, but not the type: NOERROR in header, SOA in auth. | ||||
| 	// NoData indicates name found, but not the type: NOERROR in header, SOA in auth. | ||||
| 	NoData | ||||
| 	// Delegation is a msg with a pointer to another nameserver: NOERROR in header, NS in auth, optionally fluff in additional (not checked). | ||||
| 	Delegation | ||||
| 	// OtherError indicated any other error: don't cache these. | ||||
| 	// Meta indicates a meta message, NOTIFY, or a transfer:  qType is IXFR or AXFR. | ||||
| 	Meta | ||||
| 	// Update is an dynamic update message. | ||||
| 	Update | ||||
| 	// OtherError indicates any other error: don't cache these. | ||||
| 	OtherError | ||||
| ) | ||||
|  | ||||
| @@ -32,6 +36,10 @@ func (t Type) String() string { | ||||
| 		return "NODATA" | ||||
| 	case Delegation: | ||||
| 		return "DELEGATION" | ||||
| 	case Meta: | ||||
| 		return "META" | ||||
| 	case Update: | ||||
| 		return "UPDATE" | ||||
| 	case OtherError: | ||||
| 		return "OTHERERROR" | ||||
| 	} | ||||
| @@ -50,6 +58,10 @@ func TypeFromString(s string) (Type, error) { | ||||
| 		return NoData, nil | ||||
| 	case "DELEGATION": | ||||
| 		return Delegation, nil | ||||
| 	case "META": | ||||
| 		return Meta, nil | ||||
| 	case "UPDATE": | ||||
| 		return Update, nil | ||||
| 	case "OTHERERROR": | ||||
| 		return OtherError, nil | ||||
| 	} | ||||
| @@ -61,9 +73,23 @@ func Typify(m *dns.Msg) (Type, *dns.OPT) { | ||||
| 	if m == nil { | ||||
| 		return OtherError, nil | ||||
| 	} | ||||
|  | ||||
| 	opt := m.IsEdns0() | ||||
|  | ||||
| 	if m.Opcode == dns.OpcodeUpdate { | ||||
| 		return Update, opt | ||||
| 	} | ||||
|  | ||||
| 	// Check transfer and update first | ||||
| 	if m.Opcode == dns.OpcodeNotify { | ||||
| 		return Meta, opt | ||||
| 	} | ||||
|  | ||||
| 	if len(m.Question) > 0 { | ||||
| 		if m.Question[0].Qtype == dns.TypeAXFR || m.Question[0].Qtype == dns.TypeIXFR { | ||||
| 			return Meta, opt | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if len(m.Answer) > 0 && m.Rcode == dns.RcodeSuccess { | ||||
| 		return NoError, opt | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user