1package restful
2
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.
6
7import (
8	"compress/gzip"
9	"compress/zlib"
10)
11
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.
15type BoundedCachedCompressors struct {
16	gzipWriters     chan *gzip.Writer
17	gzipReaders     chan *gzip.Reader
18	zlibWriters     chan *zlib.Writer
19	writersCapacity int
20	readersCapacity int
21}
22
23// NewBoundedCachedCompressors returns a new, with filled cache,  BoundedCachedCompressors.
24func 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,
31	}
32	for ix := 0; ix < writersCapacity; ix++ {
33		b.gzipWriters <- newGzipWriter()
34		b.zlibWriters <- newZlibWriter()
35	}
36	for ix := 0; ix < readersCapacity; ix++ {
37		b.gzipReaders <- newGzipReader()
38	}
39	return b
40}
41
42// AcquireGzipWriter returns an resettable *gzip.Writer. Needs to be released.
43func (b *BoundedCachedCompressors) AcquireGzipWriter() *gzip.Writer {
44	var writer *gzip.Writer
45	select {
46	case writer, _ = <-b.gzipWriters:
47	default:
48		// return a new unmanaged one
49		writer = newGzipWriter()
50	}
51	return writer
52}
53
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.
56func (b *BoundedCachedCompressors) ReleaseGzipWriter(w *gzip.Writer) {
57	// forget the unmanaged ones
58	if len(b.gzipWriters) < b.writersCapacity {
59		b.gzipWriters <- w
60	}
61}
62
63// AcquireGzipReader returns a *gzip.Reader. Needs to be released.
64func (b *BoundedCachedCompressors) AcquireGzipReader() *gzip.Reader {
65	var reader *gzip.Reader
66	select {
67	case reader, _ = <-b.gzipReaders:
68	default:
69		// return a new unmanaged one
70		reader = newGzipReader()
71	}
72	return reader
73}
74
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.
77func (b *BoundedCachedCompressors) ReleaseGzipReader(r *gzip.Reader) {
78	// forget the unmanaged ones
79	if len(b.gzipReaders) < b.readersCapacity {
80		b.gzipReaders <- r
81	}
82}
83
84// AcquireZlibWriter returns an resettable *zlib.Writer. Needs to be released.
85func (b *BoundedCachedCompressors) AcquireZlibWriter() *zlib.Writer {
86	var writer *zlib.Writer
87	select {
88	case writer, _ = <-b.zlibWriters:
89	default:
90		// return a new unmanaged one
91		writer = newZlibWriter()
92	}
93	return writer
94}
95
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.
98func (b *BoundedCachedCompressors) ReleaseZlibWriter(w *zlib.Writer) {
99	// forget the unmanaged ones
100	if len(b.zlibWriters) < b.writersCapacity {
101		b.zlibWriters <- w
102	}
103}
104