Skip to content

Commit 48a433b

Browse files
author
Dean Karn
authored
Merge pull request #336 from go-playground/correct-var-lock
Correct Var tagCache locking
2 parents 230db62 + e00f5e0 commit 48a433b

File tree

4 files changed

+21
-43
lines changed

4 files changed

+21
-43
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package validator
22
================
33
<img align="right" src="https://raw.githubusercontent.com/go-playground/validator/v9/logo.png">[![Join the chat at https://gitter.im/go-playground/validator](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-playground/validator?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
![Project status](https://img.shields.io/badge/version-9.9.2-green.svg)
4+
![Project status](https://img.shields.io/badge/version-9.9.3-green.svg)
55
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/validator/branches/v9/badge.svg)](https://semaphoreci.com/joeybloggs/validator)
66
[![Coverage Status](https://coveralls.io/repos/go-playground/validator/badge.svg?branch=v9&service=github)](https://coveralls.io/github/go-playground/validator?branch=v9)
77
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/validator)](https://goreportcard.com/report/github.com/go-playground/validator)

cache.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,23 @@ func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias s
315315
current.isBlockEnd = true
316316
}
317317
}
318-
319318
return
320319
}
320+
321+
func (v *Validate) fetchCacheTag(tag string) *cTag {
322+
// find cached tag
323+
ctag, found := v.tagCache.Get(tag)
324+
if !found {
325+
v.tagCache.lock.Lock()
326+
defer v.tagCache.lock.Unlock()
327+
328+
// could have been multiple trying to access, but once first is done this ensures tag
329+
// isn't parsed again.
330+
ctag, found = v.tagCache.Get(tag)
331+
if !found {
332+
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
333+
v.tagCache.Set(tag, ctag)
334+
}
335+
}
336+
return ctag
337+
}

validator.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,7 @@ OUTER:
473473
)
474474

475475
return
476-
477476
}
478-
479477
ct = ct.next
480478
}
481479
}

validator_instance.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -520,36 +520,18 @@ func (v *Validate) VarCtx(ctx context.Context, field interface{}, tag string) (e
520520
return nil
521521
}
522522

523-
// find cached tag
524-
ctag, ok := v.tagCache.Get(tag)
525-
if !ok {
526-
v.tagCache.lock.Lock()
527-
defer v.tagCache.lock.Unlock()
528-
529-
// could have been multiple trying to access, but once first is done this ensures tag
530-
// isn't parsed again.
531-
ctag, ok = v.tagCache.Get(tag)
532-
if !ok {
533-
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
534-
v.tagCache.Set(tag, ctag)
535-
}
536-
}
537-
523+
ctag := v.fetchCacheTag(tag)
538524
val := reflect.ValueOf(field)
539-
540525
vd := v.pool.Get().(*validate)
541526
vd.top = val
542527
vd.isPartial = false
543-
544528
vd.traverseField(ctx, val, val, vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
545529

546530
if len(vd.errs) > 0 {
547531
err = vd.errs
548532
vd.errs = nil
549533
}
550-
551534
v.pool.Put(vd)
552-
553535
return
554536
}
555537

@@ -590,36 +572,17 @@ func (v *Validate) VarWithValueCtx(ctx context.Context, field interface{}, other
590572
if len(tag) == 0 || tag == skipValidationTag {
591573
return nil
592574
}
593-
594-
// find cached tag
595-
ctag, ok := v.tagCache.Get(tag)
596-
if !ok {
597-
v.tagCache.lock.Lock()
598-
defer v.tagCache.lock.Unlock()
599-
600-
// could have been multiple trying to access, but once first is done this ensures tag
601-
// isn't parsed again.
602-
ctag, ok = v.tagCache.Get(tag)
603-
if !ok {
604-
ctag, _ = v.parseFieldTagsRecursive(tag, "", "", false)
605-
v.tagCache.Set(tag, ctag)
606-
}
607-
}
608-
575+
ctag := v.fetchCacheTag(tag)
609576
otherVal := reflect.ValueOf(other)
610-
611577
vd := v.pool.Get().(*validate)
612578
vd.top = otherVal
613579
vd.isPartial = false
614-
615580
vd.traverseField(ctx, otherVal, reflect.ValueOf(field), vd.ns[0:0], vd.actualNs[0:0], defaultCField, ctag)
616581

617582
if len(vd.errs) > 0 {
618583
err = vd.errs
619584
vd.errs = nil
620585
}
621-
622586
v.pool.Put(vd)
623-
624587
return
625588
}

0 commit comments

Comments
 (0)