diff options
author | Wim <wim@42.be> | 2020-08-10 00:29:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-10 00:29:54 +0200 |
commit | 4e50fd864921c556988c919269448efdb90fa961 (patch) | |
tree | a3625f03f8de3c4f3841364000a4ea3aa42c1533 /vendor/github.com/francoispqt/gojay/decode_pool.go | |
parent | dfdffa0027334e55ce213fc6eb62206dbf48baf6 (diff) | |
download | matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.gz matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.tar.bz2 matterbridge-msglm-4e50fd864921c556988c919269448efdb90fa961.zip |
Use mattermost v5 module (#1192)
Diffstat (limited to 'vendor/github.com/francoispqt/gojay/decode_pool.go')
-rw-r--r-- | vendor/github.com/francoispqt/gojay/decode_pool.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/vendor/github.com/francoispqt/gojay/decode_pool.go b/vendor/github.com/francoispqt/gojay/decode_pool.go new file mode 100644 index 00000000..68c57138 --- /dev/null +++ b/vendor/github.com/francoispqt/gojay/decode_pool.go @@ -0,0 +1,64 @@ +package gojay + +import ( + "io" + "sync" +) + +var decPool = sync.Pool{ + New: newDecoderPool, +} + +func init() { + for i := 0; i < 32; i++ { + decPool.Put(NewDecoder(nil)) + } +} + +// NewDecoder returns a new decoder. +// It takes an io.Reader implementation as data input. +func NewDecoder(r io.Reader) *Decoder { + return &Decoder{ + called: 0, + cursor: 0, + keysDone: 0, + err: nil, + r: r, + data: make([]byte, 512), + length: 0, + isPooled: 0, + } +} +func newDecoderPool() interface{} { + return NewDecoder(nil) +} + +// BorrowDecoder borrows a Decoder from the pool. +// It takes an io.Reader implementation as data input. +// +// In order to benefit from the pool, a borrowed decoder must be released after usage. +func BorrowDecoder(r io.Reader) *Decoder { + return borrowDecoder(r, 512) +} +func borrowDecoder(r io.Reader, bufSize int) *Decoder { + dec := decPool.Get().(*Decoder) + dec.called = 0 + dec.keysDone = 0 + dec.cursor = 0 + dec.err = nil + dec.r = r + dec.length = 0 + dec.isPooled = 0 + if bufSize > 0 { + dec.data = make([]byte, bufSize) + } + return dec +} + +// Release sends back a Decoder to the pool. +// If a decoder is used after calling Release +// a panic will be raised with an InvalidUsagePooledDecoderError error. +func (dec *Decoder) Release() { + dec.isPooled = 1 + decPool.Put(dec) +} |