Skip to content

Commit 486c9ac

Browse files
committed
put poolAt in their own file
1 parent b221575 commit 486c9ac

File tree

2 files changed

+111
-101
lines changed

2 files changed

+111
-101
lines changed

pool.go

-101
Original file line numberDiff line numberDiff line change
@@ -109,104 +109,3 @@ func init() {
109109
gob.Register(&memPool{})
110110
gob.Register(&filePool{})
111111
}
112-
113-
// PoolAt provides a way to Allocate and Release BufferAt objects
114-
// PoolAt's mut be concurrent-safe for calls to Get() and Put().
115-
type PoolAt interface {
116-
Get() (BufferAt, error) // Allocate a BufferAt
117-
Put(buf BufferAt) error // Release or Reuse a BufferAt
118-
}
119-
120-
type poolAt struct {
121-
poolAt sync.Pool
122-
}
123-
124-
// NewPoolAt returns a PoolAt(), it's backed by a sync.Pool so its safe for concurrent use.
125-
// Get() and Put() errors will always be nil.
126-
// It will not work with gob.
127-
func NewPoolAt(New func() BufferAt) PoolAt {
128-
return &poolAt{
129-
poolAt: sync.Pool{
130-
New: func() interface{} {
131-
return New()
132-
},
133-
},
134-
}
135-
}
136-
137-
func (p *poolAt) Get() (BufferAt, error) {
138-
return p.poolAt.Get().(BufferAt), nil
139-
}
140-
141-
func (p *poolAt) Put(buf BufferAt) error {
142-
buf.Reset()
143-
p.poolAt.Put(buf)
144-
return nil
145-
}
146-
147-
type memPoolAt struct {
148-
N int64
149-
PoolAt
150-
}
151-
152-
// NewMemPoolAt returns a PoolAt, Get() returns an in memory buffer of max size N.
153-
// Put() returns the buffer to the pool after resetting it.
154-
// Get() and Put() errors will always be nil.
155-
func NewMemPoolAt(N int64) PoolAt {
156-
return &memPoolAt{
157-
N: N,
158-
PoolAt: NewPoolAt(func() BufferAt {
159-
return New(N)
160-
}),
161-
}
162-
}
163-
164-
func (m *memPoolAt) MarshalBinary() ([]byte, error) {
165-
buf := bytes.NewBuffer(nil)
166-
err := binary.Write(buf, binary.LittleEndian, m.N)
167-
return buf.Bytes(), err
168-
}
169-
170-
func (m *memPoolAt) UnmarshalBinary(data []byte) error {
171-
buf := bytes.NewReader(data)
172-
err := binary.Read(buf, binary.LittleEndian, &m.N)
173-
m.PoolAt = NewPoolAt(func() BufferAt {
174-
return New(m.N)
175-
})
176-
return err
177-
}
178-
179-
type filePoolAt struct {
180-
N int64
181-
Directory string
182-
}
183-
184-
// NewFilePoolAt returns a PoolAt, Get() returns a file-based buffer of max size N.
185-
// Put() closes and deletes the underlying file for the buffer.
186-
// Get() may return an error if it fails to create a file for the buffer.
187-
// Put() may return an error if it fails to delete the file.
188-
func NewFilePoolAt(N int64, dir string) PoolAt {
189-
return &filePoolAt{N: N, Directory: dir}
190-
}
191-
192-
func (p *filePoolAt) Get() (BufferAt, error) {
193-
file, err := ioutil.TempFile(p.Directory, "buffer")
194-
if err != nil {
195-
return nil, err
196-
}
197-
return NewFile(p.N, file), nil
198-
}
199-
200-
func (p *filePoolAt) Put(buf BufferAt) (err error) {
201-
buf.Reset()
202-
if fileBuf, ok := buf.(*fileBuffer); ok {
203-
fileBuf.file.Close()
204-
err = os.Remove(fileBuf.file.Name())
205-
}
206-
return err
207-
}
208-
209-
func init() {
210-
gob.Register(&memPoolAt{})
211-
gob.Register(&filePoolAt{})
212-
}

pool_at.go

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}

0 commit comments

Comments
 (0)