From c146770f82ebaeb0407cfe1038326179b8a11d5a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 25 Aug 2012 19:03:49 +0200 Subject: [PATCH 1/3] locking here...? --- zone.go | 1 + 1 file changed, 1 insertion(+) diff --git a/zone.go b/zone.go index 14623695..8d20ba52 100644 --- a/zone.go +++ b/zone.go @@ -20,6 +20,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 } // toRadixName reverses a domainname so that when we store it in the radix tree From 23d4972158bd61db6b4955026a100b7a5325b38a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 25 Aug 2012 19:57:25 +0200 Subject: [PATCH 2/3] 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: From 251af89dcc82a3835d42c26981013c69eade207a Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 25 Aug 2012 20:19:13 +0200 Subject: [PATCH 3/3] Add locking for the zone structure --- zone.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/zone.go b/zone.go index abc41872..efd9ea37 100644 --- a/zone.go +++ b/zone.go @@ -8,7 +8,7 @@ import ( "sync" ) -// Zone represents a DNS zone. Currently there is no locking implemented. +// Zone represents a DNS zone. type Zone struct { Origin string // Origin of the zone Wildcard int // Whenever we see a wildcard name, this is incremented @@ -48,6 +48,7 @@ func NewZone(origin string) *Zone { return nil } z := new(Zone) + z.mutex = new(sync.RWMutex) z.Origin = Fqdn(origin) z.Radix = radix.New() return z @@ -73,6 +74,7 @@ func (z *Zone) Insert(r RR) error { zd.Name = r.Header().Name zd.RR = make(map[uint16][]RR) zd.Signatures = make(map[uint16][]*RR_RRSIG) + zd.mutex = new(sync.RWMutex) switch t := r.Header().Rrtype; t { case TypeRRSIG: sigtype := r.(*RR_RRSIG).TypeCovered