summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-pools/bufio.go
blob: 8c2ef9730af8fb40deb169d6666a844870d3bbf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package pools

import (
	"bufio"
	"io"
	"sync"
)

// BufioReaderPool is a pooled allocator for bufio.Reader objects
type BufioReaderPool interface {
	// Get fetches a bufio.Reader from pool and resets to supplied reader
	Get(io.Reader) *bufio.Reader

	// Put places supplied bufio.Reader back in pool
	Put(*bufio.Reader)
}

// NewBufioReaderPool returns a newly instantiated bufio.Reader pool
func NewBufioReaderPool(size int) BufioReaderPool {
	return &bufioReaderPool{
		Pool: sync.Pool{
			New: func() interface{} {
				return bufio.NewReaderSize(nil, size)
			},
		},
	}
}

// bufioReaderPool is our implementation of BufioReaderPool
type bufioReaderPool struct{ sync.Pool }

func (p *bufioReaderPool) Get(r io.Reader) *bufio.Reader {
	br := p.Pool.Get().(*bufio.Reader)
	br.Reset(r)
	return br
}

func (p *bufioReaderPool) Put(br *bufio.Reader) {
	br.Reset(nil)
	p.Pool.Put(br)
}

// BufioWriterPool is a pooled allocator for bufio.Writer objects
type BufioWriterPool interface {
	// Get fetches a bufio.Writer from pool and resets to supplied writer
	Get(io.Writer) *bufio.Writer

	// Put places supplied bufio.Writer back in pool
	Put(*bufio.Writer)
}

// NewBufioWriterPool returns a newly instantiated bufio.Writer pool
func NewBufioWriterPool(size int) BufioWriterPool {
	return &bufioWriterPool{
		Pool: sync.Pool{
			New: func() interface{} {
				return bufio.NewWriterSize(nil, size)
			},
		},
	}
}

// bufioWriterPool is our implementation of BufioWriterPool
type bufioWriterPool struct{ sync.Pool }

func (p *bufioWriterPool) Get(w io.Writer) *bufio.Writer {
	bw := p.Pool.Get().(*bufio.Writer)
	bw.Reset(w)
	return bw
}

func (p *bufioWriterPool) Put(bw *bufio.Writer) {
	bw.Reset(nil)
	p.Pool.Put(bw)
}