gojay

high performance JSON encoder/decoder with stream API for Golang
git clone git://git.lair.cx/gojay
Log | Files | Refs | README | LICENSE

commit b9b0d4d1716630576e74d7083666eee62ae89eae
parent c839e7c615a3a1332753a97398ba4a28c9c7ee8b
Author: francoispqt <francois@parquet.ninja>
Date:   Mon, 21 May 2018 22:39:30 +0800

add lock in decode stream when accessing err

Diffstat:
Mdecode_stream.go | 9++++++++-
Mdecode_stream_pool.go | 1+
2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/decode_stream.go b/decode_stream.go @@ -1,6 +1,7 @@ package gojay import ( + "sync" "time" ) @@ -19,6 +20,7 @@ type stream struct{} // // It implements conext.Context and provide a channel to notify interruption. type StreamDecoder struct { + mux sync.RWMutex *Decoder done chan struct{} deadline *time.Time @@ -64,7 +66,10 @@ func (dec *StreamDecoder) DecodeStream(c UnmarshalerStream) error { } } close(dec.done) - return dec.raiseInvalidJSONErr(dec.cursor) + dec.mux.Lock() + err := dec.raiseInvalidJSONErr(dec.cursor) + dec.mux.Unlock() + return err } // context.Context implementation @@ -96,6 +101,8 @@ func (dec *StreamDecoder) SetDeadline(t time.Time) { func (dec *StreamDecoder) Err() error { select { case <-dec.done: + dec.mux.RLock() + defer dec.mux.RUnlock() return dec.err default: return nil diff --git a/decode_stream_pool.go b/decode_stream_pool.go @@ -17,6 +17,7 @@ func (s stream) NewDecoder(r io.Reader) *StreamDecoder { streamDec := &StreamDecoder{ Decoder: dec, done: make(chan struct{}, 1), + mux: sync.RWMutex{}, } return streamDec }