Skip to content

Commit

Permalink
extractStructCache method - fixed unnecessary locking operation for r…
Browse files Browse the repository at this point in the history
…eading existing cStruct
  • Loading branch information
amirkaramiit committed Feb 22, 2024
1 parent 3094d59 commit 1f654bf
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ type cTag struct {
}

func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStruct {
v.structCache.lock.Lock()
defer v.structCache.lock.Unlock() // leave as defer! because if inner panics, it will never get unlocked otherwise!

typ := current.Type()

// could have been multiple trying to access, but once first is done this ensures struct
Expand All @@ -112,6 +109,9 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
return cs
}

v.structCache.lock.Lock()
defer v.structCache.lock.Unlock() // leave as defer! because if inner panics, it will never get unlocked otherwise!

cs = &cStruct{name: sName, fields: make([]*cField, 0), fn: v.structLevelFuncs[typ]}

numFields := current.NumField()
Expand Down

0 comments on commit 1f654bf

Please sign in to comment.