change to use native sync.Pool

for #98
pull/100/head
joeybloggs 9 years ago
parent b9105b5bf7
commit 562e77833a
  1. 47
      validator.go

@ -37,46 +37,16 @@ const (
mapIndexFieldName = "%s[%v]" mapIndexFieldName = "%s[%v]"
) )
var structPool *pool var structPool *sync.Pool
// Pool holds a channelStructErrors. // returns new *StructErrors to the pool
type pool struct { func newStructErrors() interface{} {
pool chan *StructErrors return &StructErrors{
}
// NewPool creates a new pool of Clients.
func newPool(max int) *pool {
return &pool{
pool: make(chan *StructErrors, max),
}
}
// Borrow a StructErrors from the pool.
func (p *pool) Borrow() *StructErrors {
var c *StructErrors
select {
case c = <-p.pool:
default:
c = &StructErrors{
Errors: map[string]*FieldError{}, Errors: map[string]*FieldError{},
StructErrors: map[string]*StructErrors{}, StructErrors: map[string]*StructErrors{},
} }
} }
return c
}
// Return returns a StructErrors to the pool.
func (p *pool) Return(c *StructErrors) {
select {
case p.pool <- c:
default:
// let it go, let it go...
}
}
type cachedTags struct { type cachedTags struct {
keyVals [][]string keyVals [][]string
isOrVal bool isOrVal bool
@ -357,7 +327,7 @@ type Validate struct {
// New creates a new Validate instance for use. // New creates a new Validate instance for use.
func New(tagName string, funcs map[string]Func) *Validate { func New(tagName string, funcs map[string]Func) *Validate {
structPool = newPool(10) structPool = &sync.Pool{New: newStructErrors}
return &Validate{ return &Validate{
tagName: tagName, tagName: tagName,
@ -377,9 +347,8 @@ func (v *Validate) SetTag(tagName string) {
// nearly all cases. only increase if you have a deeply nested struct structure. // nearly all cases. only increase if you have a deeply nested struct structure.
// NOTE: this method is not thread-safe // NOTE: this method is not thread-safe
// NOTE: this is only here to keep compatibility with v5, in v6 the method will be removed // NOTE: this is only here to keep compatibility with v5, in v6 the method will be removed
// and the max pool size will be passed into the New function
func (v *Validate) SetMaxStructPoolSize(max int) { func (v *Validate) SetMaxStructPoolSize(max int) {
structPool = newPool(max) structPool = &sync.Pool{New: newStructErrors}
} }
// AddFunction adds a validation Func to a Validate's map of validators denoted by the key // 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
cs = &cachedStruct{name: structName, children: numFields} cs = &cachedStruct{name: structName, children: numFields}
} }
validationErrors := structPool.Borrow() validationErrors := structPool.Get().(*StructErrors)
validationErrors.Struct = structName validationErrors.Struct = structName
for i := 0; i < numFields; i++ { for i := 0; i < numFields; i++ {
@ -617,7 +586,7 @@ func (v *Validate) structRecursive(top interface{}, current interface{}, s inter
structCache.Set(structType, cs) structCache.Set(structType, cs)
if len(validationErrors.Errors) == 0 && len(validationErrors.StructErrors) == 0 { if len(validationErrors.Errors) == 0 && len(validationErrors.StructErrors) == 0 {
structPool.Return(validationErrors) structPool.Put(validationErrors)
return nil return nil
} }

Loading…
Cancel
Save