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_stream_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_stream_pool.go')
-rw-r--r-- | vendor/github.com/francoispqt/gojay/decode_stream_pool.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/vendor/github.com/francoispqt/gojay/decode_stream_pool.go b/vendor/github.com/francoispqt/gojay/decode_stream_pool.go new file mode 100644 index 00000000..8e1863b9 --- /dev/null +++ b/vendor/github.com/francoispqt/gojay/decode_stream_pool.go @@ -0,0 +1,59 @@ +package gojay + +import ( + "io" + "sync" +) + +var streamDecPool = sync.Pool{ + New: newStreamDecoderPool, +} + +// NewDecoder returns a new StreamDecoder. +// It takes an io.Reader implementation as data input. +// It initiates the done channel returned by Done(). +func (s stream) NewDecoder(r io.Reader) *StreamDecoder { + dec := NewDecoder(r) + streamDec := &StreamDecoder{ + Decoder: dec, + done: make(chan struct{}, 1), + mux: sync.RWMutex{}, + } + return streamDec +} +func newStreamDecoderPool() interface{} { + return Stream.NewDecoder(nil) +} + +// BorrowDecoder borrows a StreamDecoder from the pool. +// It takes an io.Reader implementation as data input. +// It initiates the done channel returned by Done(). +// +// If no StreamEncoder is available in the pool, it returns a fresh one +func (s stream) BorrowDecoder(r io.Reader) *StreamDecoder { + return s.borrowDecoder(r, 512) +} + +func (s stream) borrowDecoder(r io.Reader, bufSize int) *StreamDecoder { + streamDec := streamDecPool.Get().(*StreamDecoder) + streamDec.called = 0 + streamDec.keysDone = 0 + streamDec.cursor = 0 + streamDec.err = nil + streamDec.r = r + streamDec.length = 0 + streamDec.isPooled = 0 + streamDec.done = make(chan struct{}, 1) + if bufSize > 0 { + streamDec.data = make([]byte, bufSize) + } + return streamDec +} + +// 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 *StreamDecoder) Release() { + dec.isPooled = 1 + streamDecPool.Put(dec) +} |