mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-10 00:29:01 +02:00
33 lines
546 B
Go
33 lines
546 B
Go
package core
|
|
|
|
import "sync"
|
|
|
|
// A Pool is a type-safe wrapper around a sync.Pool.
|
|
type Pool struct {
|
|
p *sync.Pool
|
|
}
|
|
|
|
// NewPool constructs a new Pool.
|
|
func NewPool(size int) Pool {
|
|
if size == 0 {
|
|
size = _size
|
|
}
|
|
return Pool{p: &sync.Pool{
|
|
New: func() interface{} {
|
|
return &Buffer{bs: make([]byte, 0, size)}
|
|
},
|
|
}}
|
|
}
|
|
|
|
// Get retrieves a Buffer from the pool, creating one if necessary.
|
|
func (p Pool) Get() *Buffer {
|
|
buf := p.p.Get().(*Buffer)
|
|
buf.Reset()
|
|
buf.pool = p
|
|
return buf
|
|
}
|
|
|
|
func (p Pool) put(buf *Buffer) {
|
|
p.p.Put(buf)
|
|
}
|