Files
lattigo/utils/structs/concurrent_buffer.go
2024-11-29 14:58:21 +01:00

29 lines
418 B
Go

package structs
import "sync"
type BufferPool[T any] interface {
Get() T
Put(T)
}
type SyncPool[T any] struct {
pool *sync.Pool
}
func NewSyncPool[T any](f func() T) *SyncPool[T] {
pool := &sync.Pool{
New: func() any {
return f()
},
}
return &SyncPool[T]{pool: pool}
}
func (spool *SyncPool[T]) Get() T {
return spool.pool.Get().(T)
}
func (spool *SyncPool[T]) Put(buff T) {
spool.pool.Put(buff)
}