...

Source file src/github.com/go-openapi/validate/pools.go

Documentation: github.com/go-openapi/validate

     1  //go:build !validatedebug
     2  
     3  package validate
     4  
     5  import (
     6  	"sync"
     7  
     8  	"github.com/go-openapi/spec"
     9  )
    10  
    11  var pools allPools
    12  
    13  func init() {
    14  	resetPools()
    15  }
    16  
    17  func resetPools() {
    18  	// NOTE: for testing purpose, we might want to reset pools after calling Validate twice.
    19  	// The pool is corrupted in that case: calling Put twice inserts a duplicate in the pool
    20  	// and further calls to Get are mishandled.
    21  
    22  	pools = allPools{
    23  		poolOfSchemaValidators: schemaValidatorsPool{
    24  			Pool: &sync.Pool{
    25  				New: func() any {
    26  					s := &SchemaValidator{}
    27  
    28  					return s
    29  				},
    30  			},
    31  		},
    32  		poolOfObjectValidators: objectValidatorsPool{
    33  			Pool: &sync.Pool{
    34  				New: func() any {
    35  					s := &objectValidator{}
    36  
    37  					return s
    38  				},
    39  			},
    40  		},
    41  		poolOfSliceValidators: sliceValidatorsPool{
    42  			Pool: &sync.Pool{
    43  				New: func() any {
    44  					s := &schemaSliceValidator{}
    45  
    46  					return s
    47  				},
    48  			},
    49  		},
    50  		poolOfItemsValidators: itemsValidatorsPool{
    51  			Pool: &sync.Pool{
    52  				New: func() any {
    53  					s := &itemsValidator{}
    54  
    55  					return s
    56  				},
    57  			},
    58  		},
    59  		poolOfBasicCommonValidators: basicCommonValidatorsPool{
    60  			Pool: &sync.Pool{
    61  				New: func() any {
    62  					s := &basicCommonValidator{}
    63  
    64  					return s
    65  				},
    66  			},
    67  		},
    68  		poolOfHeaderValidators: headerValidatorsPool{
    69  			Pool: &sync.Pool{
    70  				New: func() any {
    71  					s := &HeaderValidator{}
    72  
    73  					return s
    74  				},
    75  			},
    76  		},
    77  		poolOfParamValidators: paramValidatorsPool{
    78  			Pool: &sync.Pool{
    79  				New: func() any {
    80  					s := &ParamValidator{}
    81  
    82  					return s
    83  				},
    84  			},
    85  		},
    86  		poolOfBasicSliceValidators: basicSliceValidatorsPool{
    87  			Pool: &sync.Pool{
    88  				New: func() any {
    89  					s := &basicSliceValidator{}
    90  
    91  					return s
    92  				},
    93  			},
    94  		},
    95  		poolOfNumberValidators: numberValidatorsPool{
    96  			Pool: &sync.Pool{
    97  				New: func() any {
    98  					s := &numberValidator{}
    99  
   100  					return s
   101  				},
   102  			},
   103  		},
   104  		poolOfStringValidators: stringValidatorsPool{
   105  			Pool: &sync.Pool{
   106  				New: func() any {
   107  					s := &stringValidator{}
   108  
   109  					return s
   110  				},
   111  			},
   112  		},
   113  		poolOfSchemaPropsValidators: schemaPropsValidatorsPool{
   114  			Pool: &sync.Pool{
   115  				New: func() any {
   116  					s := &schemaPropsValidator{}
   117  
   118  					return s
   119  				},
   120  			},
   121  		},
   122  		poolOfFormatValidators: formatValidatorsPool{
   123  			Pool: &sync.Pool{
   124  				New: func() any {
   125  					s := &formatValidator{}
   126  
   127  					return s
   128  				},
   129  			},
   130  		},
   131  		poolOfTypeValidators: typeValidatorsPool{
   132  			Pool: &sync.Pool{
   133  				New: func() any {
   134  					s := &typeValidator{}
   135  
   136  					return s
   137  				},
   138  			},
   139  		},
   140  		poolOfSchemas: schemasPool{
   141  			Pool: &sync.Pool{
   142  				New: func() any {
   143  					s := &spec.Schema{}
   144  
   145  					return s
   146  				},
   147  			},
   148  		},
   149  		poolOfResults: resultsPool{
   150  			Pool: &sync.Pool{
   151  				New: func() any {
   152  					s := &Result{}
   153  
   154  					return s
   155  				},
   156  			},
   157  		},
   158  	}
   159  }
   160  
   161  type (
   162  	allPools struct {
   163  		// memory pools for all validator objects.
   164  		//
   165  		// Each pool can be borrowed from and redeemed to.
   166  		poolOfSchemaValidators      schemaValidatorsPool
   167  		poolOfObjectValidators      objectValidatorsPool
   168  		poolOfSliceValidators       sliceValidatorsPool
   169  		poolOfItemsValidators       itemsValidatorsPool
   170  		poolOfBasicCommonValidators basicCommonValidatorsPool
   171  		poolOfHeaderValidators      headerValidatorsPool
   172  		poolOfParamValidators       paramValidatorsPool
   173  		poolOfBasicSliceValidators  basicSliceValidatorsPool
   174  		poolOfNumberValidators      numberValidatorsPool
   175  		poolOfStringValidators      stringValidatorsPool
   176  		poolOfSchemaPropsValidators schemaPropsValidatorsPool
   177  		poolOfFormatValidators      formatValidatorsPool
   178  		poolOfTypeValidators        typeValidatorsPool
   179  		poolOfSchemas               schemasPool
   180  		poolOfResults               resultsPool
   181  	}
   182  
   183  	schemaValidatorsPool struct {
   184  		*sync.Pool
   185  	}
   186  
   187  	objectValidatorsPool struct {
   188  		*sync.Pool
   189  	}
   190  
   191  	sliceValidatorsPool struct {
   192  		*sync.Pool
   193  	}
   194  
   195  	itemsValidatorsPool struct {
   196  		*sync.Pool
   197  	}
   198  
   199  	basicCommonValidatorsPool struct {
   200  		*sync.Pool
   201  	}
   202  
   203  	headerValidatorsPool struct {
   204  		*sync.Pool
   205  	}
   206  
   207  	paramValidatorsPool struct {
   208  		*sync.Pool
   209  	}
   210  
   211  	basicSliceValidatorsPool struct {
   212  		*sync.Pool
   213  	}
   214  
   215  	numberValidatorsPool struct {
   216  		*sync.Pool
   217  	}
   218  
   219  	stringValidatorsPool struct {
   220  		*sync.Pool
   221  	}
   222  
   223  	schemaPropsValidatorsPool struct {
   224  		*sync.Pool
   225  	}
   226  
   227  	formatValidatorsPool struct {
   228  		*sync.Pool
   229  	}
   230  
   231  	typeValidatorsPool struct {
   232  		*sync.Pool
   233  	}
   234  
   235  	schemasPool struct {
   236  		*sync.Pool
   237  	}
   238  
   239  	resultsPool struct {
   240  		*sync.Pool
   241  	}
   242  )
   243  
   244  func (p schemaValidatorsPool) BorrowValidator() *SchemaValidator {
   245  	return p.Get().(*SchemaValidator)
   246  }
   247  
   248  func (p schemaValidatorsPool) RedeemValidator(s *SchemaValidator) {
   249  	// NOTE: s might be nil. In that case, Put is a noop.
   250  	p.Put(s)
   251  }
   252  
   253  func (p objectValidatorsPool) BorrowValidator() *objectValidator {
   254  	return p.Get().(*objectValidator)
   255  }
   256  
   257  func (p objectValidatorsPool) RedeemValidator(s *objectValidator) {
   258  	p.Put(s)
   259  }
   260  
   261  func (p sliceValidatorsPool) BorrowValidator() *schemaSliceValidator {
   262  	return p.Get().(*schemaSliceValidator)
   263  }
   264  
   265  func (p sliceValidatorsPool) RedeemValidator(s *schemaSliceValidator) {
   266  	p.Put(s)
   267  }
   268  
   269  func (p itemsValidatorsPool) BorrowValidator() *itemsValidator {
   270  	return p.Get().(*itemsValidator)
   271  }
   272  
   273  func (p itemsValidatorsPool) RedeemValidator(s *itemsValidator) {
   274  	p.Put(s)
   275  }
   276  
   277  func (p basicCommonValidatorsPool) BorrowValidator() *basicCommonValidator {
   278  	return p.Get().(*basicCommonValidator)
   279  }
   280  
   281  func (p basicCommonValidatorsPool) RedeemValidator(s *basicCommonValidator) {
   282  	p.Put(s)
   283  }
   284  
   285  func (p headerValidatorsPool) BorrowValidator() *HeaderValidator {
   286  	return p.Get().(*HeaderValidator)
   287  }
   288  
   289  func (p headerValidatorsPool) RedeemValidator(s *HeaderValidator) {
   290  	p.Put(s)
   291  }
   292  
   293  func (p paramValidatorsPool) BorrowValidator() *ParamValidator {
   294  	return p.Get().(*ParamValidator)
   295  }
   296  
   297  func (p paramValidatorsPool) RedeemValidator(s *ParamValidator) {
   298  	p.Put(s)
   299  }
   300  
   301  func (p basicSliceValidatorsPool) BorrowValidator() *basicSliceValidator {
   302  	return p.Get().(*basicSliceValidator)
   303  }
   304  
   305  func (p basicSliceValidatorsPool) RedeemValidator(s *basicSliceValidator) {
   306  	p.Put(s)
   307  }
   308  
   309  func (p numberValidatorsPool) BorrowValidator() *numberValidator {
   310  	return p.Get().(*numberValidator)
   311  }
   312  
   313  func (p numberValidatorsPool) RedeemValidator(s *numberValidator) {
   314  	p.Put(s)
   315  }
   316  
   317  func (p stringValidatorsPool) BorrowValidator() *stringValidator {
   318  	return p.Get().(*stringValidator)
   319  }
   320  
   321  func (p stringValidatorsPool) RedeemValidator(s *stringValidator) {
   322  	p.Put(s)
   323  }
   324  
   325  func (p schemaPropsValidatorsPool) BorrowValidator() *schemaPropsValidator {
   326  	return p.Get().(*schemaPropsValidator)
   327  }
   328  
   329  func (p schemaPropsValidatorsPool) RedeemValidator(s *schemaPropsValidator) {
   330  	p.Put(s)
   331  }
   332  
   333  func (p formatValidatorsPool) BorrowValidator() *formatValidator {
   334  	return p.Get().(*formatValidator)
   335  }
   336  
   337  func (p formatValidatorsPool) RedeemValidator(s *formatValidator) {
   338  	p.Put(s)
   339  }
   340  
   341  func (p typeValidatorsPool) BorrowValidator() *typeValidator {
   342  	return p.Get().(*typeValidator)
   343  }
   344  
   345  func (p typeValidatorsPool) RedeemValidator(s *typeValidator) {
   346  	p.Put(s)
   347  }
   348  
   349  func (p schemasPool) BorrowSchema() *spec.Schema {
   350  	return p.Get().(*spec.Schema)
   351  }
   352  
   353  func (p schemasPool) RedeemSchema(s *spec.Schema) {
   354  	p.Put(s)
   355  }
   356  
   357  func (p resultsPool) BorrowResult() *Result {
   358  	return p.Get().(*Result).cleared()
   359  }
   360  
   361  func (p resultsPool) RedeemResult(s *Result) {
   362  	if s == emptyResult {
   363  		return
   364  	}
   365  	p.Put(s)
   366  }
   367  

View as plain text