Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | package 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 | |
| 7 | import ( |
| 8 | "compress/gzip" |
| 9 | "compress/zlib" |
| 10 | ) |
| 11 | |
| 12 | // CompressorProvider describes a component that can provider compressors for the std methods. |
| 13 | type CompressorProvider interface { |
| 14 | // Returns a *gzip.Writer which needs to be released later. |
| 15 | // Before using it, call Reset(). |
| 16 | AcquireGzipWriter() *gzip.Writer |
| 17 | |
| 18 | // Releases an acquired *gzip.Writer. |
| 19 | ReleaseGzipWriter(w *gzip.Writer) |
| 20 | |
| 21 | // Returns a *gzip.Reader which needs to be released later. |
| 22 | AcquireGzipReader() *gzip.Reader |
| 23 | |
| 24 | // Releases an acquired *gzip.Reader. |
| 25 | ReleaseGzipReader(w *gzip.Reader) |
| 26 | |
| 27 | // Returns a *zlib.Writer which needs to be released later. |
| 28 | // Before using it, call Reset(). |
| 29 | AcquireZlibWriter() *zlib.Writer |
| 30 | |
| 31 | // Releases an acquired *zlib.Writer. |
| 32 | ReleaseZlibWriter(w *zlib.Writer) |
| 33 | } |
| 34 | |
| 35 | // DefaultCompressorProvider is the actual provider of compressors (zlib or gzip). |
| 36 | var currentCompressorProvider CompressorProvider |
| 37 | |
| 38 | func init() { |
| 39 | currentCompressorProvider = NewSyncPoolCompessors() |
| 40 | } |
| 41 | |
| 42 | // CurrentCompressorProvider returns the current CompressorProvider. |
| 43 | // It is initialized using a SyncPoolCompessors. |
| 44 | func CurrentCompressorProvider() CompressorProvider { |
| 45 | return currentCompressorProvider |
| 46 | } |
| 47 | |
| 48 | // SetCompressorProvider sets the actual provider of compressors (zlib or gzip). |
| 49 | func SetCompressorProvider(p CompressorProvider) { |
| 50 | if p == nil { |
| 51 | panic("cannot set compressor provider to nil") |
| 52 | } |
| 53 | currentCompressorProvider = p |
| 54 | } |