pool.go 481 B

123456789101112131415161718192021222324252627282930
  1. package origin
  2. import (
  3. "sync"
  4. )
  5. type bufferPool struct {
  6. // A bufferPool must not be copied after first use.
  7. // https://golang.org/pkg/sync/#Pool
  8. buffers sync.Pool
  9. }
  10. func newBufferPool(bufferSize int) *bufferPool {
  11. return &bufferPool{
  12. buffers: sync.Pool{
  13. New: func() interface{} {
  14. return make([]byte, bufferSize)
  15. },
  16. },
  17. }
  18. }
  19. func (p *bufferPool) Get() []byte {
  20. return p.buffers.Get().([]byte)
  21. }
  22. func (p *bufferPool) Put(buf []byte) {
  23. p.buffers.Put(buf)
  24. }