mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
1a237c6903
This can reduce memory usage on systems with enabled CPU limits. Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/946
37 lines
706 B
Go
37 lines
706 B
Go
package netstorage
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
|
)
|
|
|
|
// GetInsertCtx returns InsertCtx from the pool.
|
|
//
|
|
// Call PutInsertCtx for returning it to the pool.
|
|
func GetInsertCtx() *InsertCtx {
|
|
select {
|
|
case ctx := <-insertCtxPoolCh:
|
|
return ctx
|
|
default:
|
|
if v := insertCtxPool.Get(); v != nil {
|
|
return v.(*InsertCtx)
|
|
}
|
|
return &InsertCtx{}
|
|
}
|
|
}
|
|
|
|
// PutInsertCtx returns ctx to the pool.
|
|
//
|
|
// ctx cannot be used after the call.
|
|
func PutInsertCtx(ctx *InsertCtx) {
|
|
ctx.Reset()
|
|
select {
|
|
case insertCtxPoolCh <- ctx:
|
|
default:
|
|
insertCtxPool.Put(ctx)
|
|
}
|
|
}
|
|
|
|
var insertCtxPool sync.Pool
|
|
var insertCtxPoolCh = make(chan *InsertCtx, cgroup.AvailableCPUs())
|