@@ -37,43 +37,13 @@ const (
3737 mapIndexFieldName = "%s[%v]"
3838)
3939
40- var structPool * pool
40+ var structPool * sync. Pool
4141
42- // Pool holds a channelStructErrors.
43- type pool struct {
44- pool chan * StructErrors
45- }
46-
47- // NewPool creates a new pool of Clients.
48- func newPool (max int ) * pool {
49- return & pool {
50- pool : make (chan * StructErrors , max ),
51- }
52- }
53-
54- // Borrow a StructErrors from the pool.
55- func (p * pool ) Borrow () * StructErrors {
56- var c * StructErrors
57-
58- select {
59- case c = <- p .pool :
60- default :
61- c = & StructErrors {
62- Errors : map [string ]* FieldError {},
63- StructErrors : map [string ]* StructErrors {},
64- }
65- }
66-
67- return c
68- }
69-
70- // Return returns a StructErrors to the pool.
71- func (p * pool ) Return (c * StructErrors ) {
72-
73- select {
74- case p .pool <- c :
75- default :
76- // let it go, let it go...
42+ // returns new *StructErrors to the pool
43+ func newStructErrors () interface {} {
44+ return & StructErrors {
45+ Errors : map [string ]* FieldError {},
46+ StructErrors : map [string ]* StructErrors {},
7747 }
7848}
7949
@@ -357,7 +327,7 @@ type Validate struct {
357327// New creates a new Validate instance for use.
358328func New (tagName string , funcs map [string ]Func ) * Validate {
359329
360- structPool = newPool ( 10 )
330+ structPool = & sync. Pool { New : newStructErrors }
361331
362332 return & Validate {
363333 tagName : tagName ,
@@ -377,9 +347,8 @@ func (v *Validate) SetTag(tagName string) {
377347// nearly all cases. only increase if you have a deeply nested struct structure.
378348// NOTE: this method is not thread-safe
379349// NOTE: this is only here to keep compatibility with v5, in v6 the method will be removed
380- // and the max pool size will be passed into the New function
381350func (v * Validate ) SetMaxStructPoolSize (max int ) {
382- structPool = newPool ( max )
351+ structPool = & sync. Pool { New : newStructErrors }
383352}
384353
385354// AddFunction adds a validation Func to a Validate's map of validators denoted by the key
@@ -440,7 +409,7 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
440409 cs = & cachedStruct {name : structName , children : numFields }
441410 }
442411
443- validationErrors := structPool .Borrow ( )
412+ validationErrors := structPool .Get ().( * StructErrors )
444413 validationErrors .Struct = structName
445414
446415 for i := 0 ; i < numFields ; i ++ {
@@ -617,7 +586,7 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
617586 structCache .Set (structType , cs )
618587
619588 if len (validationErrors .Errors ) == 0 && len (validationErrors .StructErrors ) == 0 {
620- structPool .Return (validationErrors )
589+ structPool .Put (validationErrors )
621590 return nil
622591 }
623592
0 commit comments