3 // Copyright 2015 Ernest Micklei. All rights reserved.
4 // Use of this source code is governed by a license
5 // that can be found in the LICENSE file.
12 // BoundedCachedCompressors is a CompressorProvider that uses a cache with a fixed amount
13 // of writers and readers (resources).
14 // If a new resource is acquired and all are in use, it will return a new unmanaged resource.
15 type BoundedCachedCompressors struct {
16 gzipWriters chan *gzip.Writer
17 gzipReaders chan *gzip.Reader
18 zlibWriters chan *zlib.Writer
23 // NewBoundedCachedCompressors returns a new, with filled cache, BoundedCachedCompressors.
24 func NewBoundedCachedCompressors(writersCapacity, readersCapacity int) *BoundedCachedCompressors {
25 b := &BoundedCachedCompressors{
26 gzipWriters: make(chan *gzip.Writer, writersCapacity),
27 gzipReaders: make(chan *gzip.Reader, readersCapacity),
28 zlibWriters: make(chan *zlib.Writer, writersCapacity),
29 writersCapacity: writersCapacity,
30 readersCapacity: readersCapacity,
32 for ix := 0; ix < writersCapacity; ix++ {
33 b.gzipWriters <- newGzipWriter()
34 b.zlibWriters <- newZlibWriter()
36 for ix := 0; ix < readersCapacity; ix++ {
37 b.gzipReaders <- newGzipReader()
42 // AcquireGzipWriter returns an resettable *gzip.Writer. Needs to be released.
43 func (b *BoundedCachedCompressors) AcquireGzipWriter() *gzip.Writer {
44 var writer *gzip.Writer
46 case writer, _ = <-b.gzipWriters:
48 // return a new unmanaged one
49 writer = newGzipWriter()
54 // ReleaseGzipWriter accepts a writer (does not have to be one that was cached)
55 // only when the cache has room for it. It will ignore it otherwise.
56 func (b *BoundedCachedCompressors) ReleaseGzipWriter(w *gzip.Writer) {
57 // forget the unmanaged ones
58 if len(b.gzipWriters) < b.writersCapacity {
63 // AcquireGzipReader returns a *gzip.Reader. Needs to be released.
64 func (b *BoundedCachedCompressors) AcquireGzipReader() *gzip.Reader {
65 var reader *gzip.Reader
67 case reader, _ = <-b.gzipReaders:
69 // return a new unmanaged one
70 reader = newGzipReader()
75 // ReleaseGzipReader accepts a reader (does not have to be one that was cached)
76 // only when the cache has room for it. It will ignore it otherwise.
77 func (b *BoundedCachedCompressors) ReleaseGzipReader(r *gzip.Reader) {
78 // forget the unmanaged ones
79 if len(b.gzipReaders) < b.readersCapacity {
84 // AcquireZlibWriter returns an resettable *zlib.Writer. Needs to be released.
85 func (b *BoundedCachedCompressors) AcquireZlibWriter() *zlib.Writer {
86 var writer *zlib.Writer
88 case writer, _ = <-b.zlibWriters:
90 // return a new unmanaged one
91 writer = newZlibWriter()
96 // ReleaseZlibWriter accepts a writer (does not have to be one that was cached)
97 // only when the cache has room for it. It will ignore it otherwise.
98 func (b *BoundedCachedCompressors) ReleaseZlibWriter(w *zlib.Writer) {
99 // forget the unmanaged ones
100 if len(b.zlibWriters) < b.writersCapacity {