From 3d8a093359df5e5462d1cb767ae9c5a9aabd9d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Nowotnik?= Date: Mon, 11 Jul 2022 10:48:56 +0200 Subject: [PATCH] remove mutex, comment fixes --- stream.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/stream.go b/stream.go index 75cf3f2..ff24d0f 100644 --- a/stream.go +++ b/stream.go @@ -198,23 +198,26 @@ type plainObjectStream struct { conn io.Closer decoder *json.Decoder encoder *json.Encoder - mu sync.Mutex } +// NewPlainObjectStream creates a buffered stream from a network +// connection (or other similar interface). The underlying +// objectStream produces plain JSON-RPC 2.0 objects without a header. func NewPlainObjectStream(conn io.ReadWriteCloser) ObjectStream { - os := &plainObjectStream{conn: conn} - os.encoder = json.NewEncoder(conn) - os.decoder = json.NewDecoder(conn) - return os + return &plainObjectStream{ + conn: conn, + encoder: json.NewEncoder(conn), + decoder: json.NewDecoder(conn), + } } func (os *plainObjectStream) ReadObject(v interface{}) error { return os.decoder.Decode(v) } +// WriteObject serializes a value to JSON and writes it to a stream. +// Not thread-safe, a user must synchronize writes in a multithreaded environment. func (os *plainObjectStream) WriteObject(v interface{}) error { - os.mu.Lock() - defer os.mu.Unlock() return os.encoder.Encode(v) }