| 1 | package buffer |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/binary" |
| 6 | "encoding/gob" |
| 7 | "io/ioutil" |
| 8 | "os" |
| 9 | "sync" |
| 10 | ) |
| 11 | |
| 12 | // PoolAt provides a way to Allocate and Release BufferAt objects |
| 13 | // PoolAt's mut be concurrent-safe for calls to Get() and Put(). |
| 14 | type PoolAt interface { |
| 15 | Get() (BufferAt, error) // Allocate a BufferAt |
| 16 | Put(buf BufferAt) error // Release or Reuse a BufferAt |
| 17 | } |
| 18 | |
| 19 | type poolAt struct { |
| 20 | poolAt sync.Pool |
| 21 | } |
| 22 | |
| 23 | // NewPoolAt returns a PoolAt(), 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 NewPoolAt(New func() BufferAt) PoolAt { |
| 27 | return &poolAt{ |
| 28 | poolAt: sync.Pool{ |
| 29 | New: func() interface{} { |
| 30 | return New() |
| 31 | }, |
| 32 | }, |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func (p *poolAt) Get() (BufferAt, error) { |
| 37 | return p.poolAt.Get().(BufferAt), nil |
| 38 | } |
| 39 | |
| 40 | func (p *poolAt) Put(buf BufferAt) error { |
| 41 | buf.Reset() |
| 42 | p.poolAt.Put(buf) |
| 43 | return nil |
| 44 | } |
| 45 | |
| 46 | type memPoolAt struct { |
| 47 | N int64 |
| 48 | PoolAt |
| 49 | } |
| 50 | |
| 51 | // NewMemPoolAt returns a PoolAt, 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 NewMemPoolAt(N int64) PoolAt { |
| 55 | return &memPoolAt{ |
| 56 | N: N, |
| 57 | PoolAt: NewPoolAt(func() BufferAt { |
| 58 | return New(N) |
| 59 | }), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func (m *memPoolAt) 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 *memPoolAt) UnmarshalBinary(data []byte) error { |
| 70 | buf := bytes.NewReader(data) |
| 71 | err := binary.Read(buf, binary.LittleEndian, &m.N) |
| 72 | m.PoolAt = NewPoolAt(func() BufferAt { |
| 73 | return New(m.N) |
| 74 | }) |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | type filePoolAt struct { |
| 79 | N int64 |
| 80 | Directory string |
| 81 | } |
| 82 | |
| 83 | // NewFilePoolAt returns a PoolAt, 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 NewFilePoolAt(N int64, dir string) PoolAt { |
| 88 | return &filePoolAt{N: N, Directory: dir} |
| 89 | } |
| 90 | |
| 91 | func (p *filePoolAt) Get() (BufferAt, 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 *filePoolAt) Put(buf BufferAt) (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(&memPoolAt{}) |
| 110 | gob.Register(&filePoolAt{}) |
| 111 | } |