From 23d4972158bd61db6b4955026a100b7a5325b38a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 25 Aug 2012 19:57:25 +0200 Subject: [PATCH] locking --- zone.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/zone.go b/zone.go index 8d20ba52..abc41872 100644 --- a/zone.go +++ b/zone.go @@ -5,6 +5,7 @@ package dns import ( "github.com/miekg/radix" "strings" + "sync" ) // Zone represents a DNS zone. Currently there is no locking implemented. @@ -12,6 +13,7 @@ type Zone struct { Origin string // Origin of the zone Wildcard int // Whenever we see a wildcard name, this is incremented *radix.Radix // Zone data + mutex *sync.RWMutex } // ZoneData holds all the RRs having their ownername equal to Name. @@ -20,7 +22,7 @@ type ZoneData struct { RR map[uint16][]RR // Map of the RR type to the RR Signatures map[uint16][]*RR_RRSIG // DNSSEC signatures for the RRs, stored under type covered NonAuth bool // Always false, except for NSsets that differ from z.Origin - mutex *sync.RWMutex // lock for reading/writing + mutex *sync.RWMutex } // toRadixName reverses a domainname so that when we store it in the radix tree @@ -59,8 +61,10 @@ func (z *Zone) Insert(r RR) error { } key := toRadixName(r.Header().Name) + z.mutex.Lock() zd := z.Radix.Find(key) if zd == nil { + defer z.mutex.Unlock() // Check if its a wildcard name if len(r.Header().Name) > 1 && r.Header().Name[0] == '*' && r.Header().Name[1] == '.' { z.Wildcard++ @@ -85,6 +89,9 @@ func (z *Zone) Insert(r RR) error { z.Radix.Insert(key, zd) return nil } + z.mutex.Unlock() + zd.Value.(*ZoneData).mutex.Lock() + defer zd.Value.(*ZoneData).mutex.Unlock() // Name already there switch t := r.Header().Rrtype; t { case TypeRRSIG: @@ -105,10 +112,15 @@ func (z *Zone) Insert(r RR) error { // this is a no-op. func (z *Zone) Remove(r RR) error { key := toRadixName(r.Header().Name) + z.mutex.Lock() zd := z.Radix.Find(key) if zd == nil { + defer z.mutex.Unlock() return nil } + z.mutex.Unlock() + zd.Value.(*ZoneData).mutex.Lock() + defer zd.Value.(*ZoneData).mutex.Unlock() remove := false switch t := r.Header().Rrtype; t { case TypeRRSIG: