added few buffer pools

This commit is contained in:
harshabose
2025-07-09 01:08:40 +05:30
parent 93e9f63548
commit 24ab32b5ee
4 changed files with 123 additions and 1 deletions

6
go.mod
View File

@@ -1,3 +1,7 @@
module github.com/harshabose/tools/buffer
go 1.23.3
go 1.23.3
require github.com/asticode/go-astiav v0.37.0
require github.com/asticode/go-astikit v0.42.0 // indirect

12
go.sum Normal file
View File

@@ -0,0 +1,12 @@
github.com/asticode/go-astiav v0.37.0 h1:Ph4usW4lulotVvne8hqZ1JCOHX1f8ces6yVKdg+PnyQ=
github.com/asticode/go-astiav v0.37.0/go.mod h1:GI0pHw6K2/pl/o8upCtT49P/q4KCwhv/8nGLlCsZLdA=
github.com/asticode/go-astikit v0.42.0 h1:pnir/2KLUSr0527Tv908iAH6EGYYrYta132vvjXsH5w=
github.com/asticode/go-astikit v0.42.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

53
pkg/frame_pool.go Normal file
View File

@@ -0,0 +1,53 @@
package buffer
import (
"sync"
"github.com/asticode/go-astiav"
)
type framePool struct {
pool sync.Pool
}
func CreateFramePool() Pool[*astiav.Frame] {
return &framePool{
pool: sync.Pool{
New: func() any {
return astiav.AllocFrame()
},
},
}
}
func (pool *framePool) Get() *astiav.Frame {
frame, ok := pool.pool.Get().(*astiav.Frame)
if frame == nil || !ok {
return astiav.AllocFrame()
}
return frame
}
func (pool *framePool) Put(frame *astiav.Frame) {
if frame == nil {
return
}
frame.Unref()
pool.pool.Put(frame)
}
func (pool *framePool) Release() {
for {
frame, ok := pool.pool.Get().(*astiav.Frame)
if frame == nil {
break
}
if !ok {
continue
}
frame.Free()
}
}

53
pkg/packet_pool.go Normal file
View File

@@ -0,0 +1,53 @@
package buffer
import (
"sync"
"github.com/asticode/go-astiav"
)
type packetPool struct {
pool sync.Pool
}
func CreatePacketPool() Pool[*astiav.Packet] {
return &packetPool{
pool: sync.Pool{
New: func() any {
return astiav.AllocPacket()
},
},
}
}
func (pool *packetPool) Get() *astiav.Packet {
packet, ok := pool.pool.Get().(*astiav.Packet)
if packet == nil || !ok {
return astiav.AllocPacket()
}
return packet
}
func (pool *packetPool) Put(packet *astiav.Packet) {
if packet == nil {
return
}
packet.Unref()
pool.pool.Put(packet)
}
func (pool *packetPool) Release() {
for {
packet, ok := pool.pool.Get().(*astiav.Packet)
if packet == nil {
break
}
if !ok {
continue
}
// fmt.Printf("🗑️ Releasing packet: ptr=%p\n", packet)
packet.Free()
}
}