2020-02-23 11:35:47 +00:00
|
|
|
package netstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2024-08-13 14:49:09 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2020-02-23 11:35:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetInsertCtx returns InsertCtx from the pool.
|
|
|
|
//
|
|
|
|
// Call PutInsertCtx for returning it to the pool.
|
|
|
|
func GetInsertCtx() *InsertCtx {
|
2024-08-13 14:49:09 +00:00
|
|
|
select {
|
|
|
|
case ctx := <-insertCtxPoolCh:
|
|
|
|
return ctx
|
|
|
|
default:
|
|
|
|
if v := insertCtxPool.Get(); v != nil {
|
|
|
|
return v.(*InsertCtx)
|
|
|
|
}
|
|
|
|
return &InsertCtx{}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutInsertCtx returns ctx to the pool.
|
|
|
|
//
|
|
|
|
// ctx cannot be used after the call.
|
|
|
|
func PutInsertCtx(ctx *InsertCtx) {
|
|
|
|
ctx.Reset()
|
2024-08-13 14:49:09 +00:00
|
|
|
select {
|
|
|
|
case insertCtxPoolCh <- ctx:
|
|
|
|
default:
|
|
|
|
insertCtxPool.Put(ctx)
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 14:49:09 +00:00
|
|
|
var (
|
|
|
|
insertCtxPool sync.Pool
|
|
|
|
insertCtxPoolCh = make(chan *InsertCtx, cgroup.AvailableCPUs())
|
|
|
|
)
|