| 1 | package buffer |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "encoding/gob" |
| 7 | "io/ioutil" |
| 8 | "os" |
| 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | // Pool provides a way to Allocate and Release Buffer objects |
| 13 | // Pools mut be concurrent-safe for calls to Get() and Put(). |
| 14 | type Pool interface { |
| 15 | Get() (Buffer, error) // Allocate a Buffer |
| 16 | Put(buf Buffer) error // Release or Reuse a Buffer |
| 17 | } |
| 18 | |
| 19 | type pool struct { |
| 20 | pool sync.Pool |
| 21 | } |
| 22 | |
| 23 | // NewPool returns a Pool(), it's backed by a sync.Pool so its safe for concurrent use. |
| 24 | // Get() and Put() errors will always be nil. |
| 25 | // It will not work with gob. |
| 26 | func NewPool(New func() Buffer) Pool { |
| 27 | return &pool{ |
| 28 | pool: sync.Pool{ |
| 29 | New: func() interface{} { |
| 30 | return New() |
| 31 | }, |
| 32 | }, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (p *pool) Get() (Buffer, error) { |
| 37 | return p.pool.Get().(Buffer), nil |
| 38 | } |
| 39 | |
| 40 | func (p *pool) Put(buf Buffer) error { |
| 41 | buf.Reset() |
| 42 | p.pool.Put(buf) |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | type memPool struct { |
| 47 | N int64 |
| 48 | Pool |
| 49 | } |
| 50 | |
| 51 | // NewMemPool returns a Pool, Get() returns an in memory buffer of max size N. |
| 52 | // Put() returns the buffer to the pool after resetting it. |
| 53 | // Get() and Put() errors will always be nil. |
| 54 | func NewMemPool(N int64) Pool { |
| 55 | return &memPool{ |
| 56 | N: N, |
| 57 | Pool: NewPool(func() Buffer { |
| 58 | return New(N) |
| 59 | }), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func (m *memPool) MarshalBinary() ([]byte, error) { |
| 64 | buf := bytes.NewBuffer(nil) |
| 65 | err := binary.Write(buf, binary.LittleEndian, m.N) |
| 66 | return buf.Bytes(), err |
| 67 | } |
| 68 | |
| 69 | func (m *memPool) UnmarshalBinary(data []byte) error { |
| 70 | buf := bytes.NewReader(data) |
| 71 | err := binary.Read(buf, binary.LittleEndian, &m.N) |
| 72 | m.Pool = NewPool(func() Buffer { |
| 73 | return New(m.N) |
| 74 | }) |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | type filePool struct { |
| 79 | N int64 |
| 80 | Directory string |
| 81 | } |
| 82 | |
| 83 | // NewFilePool returns a Pool, Get() returns a file-based buffer of max size N. |
| 84 | // Put() closes and deletes the underlying file for the buffer. |
| 85 | // Get() may return an error if it fails to create a file for the buffer. |
| 86 | // Put() may return an error if it fails to delete the file. |
| 87 | func NewFilePool(N int64, dir string) Pool { |
| 88 | return &filePool{N: N, Directory: dir} |
| 89 | } |
| 90 | |
| 91 | func (p *filePool) Get() (Buffer, error) { |
| 92 | file, err := ioutil.TempFile(p.Directory, "buffer") |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | return NewFile(p.N, file), nil |
| 97 | } |
| 98 | |
| 99 | func (p *filePool) Put(buf Buffer) (err error) { |
| 100 | buf.Reset() |
| 101 | if fileBuf, ok := buf.(*fileBuffer); ok { |
| 102 | fileBuf.file.Close() |
| 103 | err = os.Remove(fileBuf.file.Name()) |
| 104 | } |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | func init() { |
| 109 | gob.Register(&memPool{}) |
| 110 | gob.Register(&filePool{}) |
| 111 | } |