github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/pools/querypool/query.go (about)

     1  package querypool
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/scylladb/gocqlx/v2"
     7  )
     8  
     9  /*
    10     Creation Time: 2021 - Jul - 14
    11     Created by:  (ehsan)
    12     Maintainers:
    13        1.  Ehsan N. Moosa (E2)
    14     Auditor: Ehsan N. Moosa (E2)
    15     Copyright Ronak Software Group 2020
    16  */
    17  
    18  type QueryPool struct {
    19  	pool sync.Pool
    20  	gen  queryBuilderFunc
    21  }
    22  
    23  type queryBuilderFunc func() *gocqlx.Queryx
    24  
    25  // New creates a new query pool
    26  func New(genFunc queryBuilderFunc) *QueryPool {
    27  	return &QueryPool{
    28  		gen: genFunc,
    29  	}
    30  }
    31  
    32  func (qp *QueryPool) GetQuery() *gocqlx.Queryx {
    33  	q, ok := qp.pool.Get().(*gocqlx.Queryx)
    34  	if !ok {
    35  		q = qp.gen()
    36  
    37  		return q
    38  	}
    39  
    40  	return q
    41  }
    42  
    43  func (qp *QueryPool) Put(q *gocqlx.Queryx) {
    44  	qp.pool.Put(q)
    45  }