mirror of
https://github.com/golang/go
synced 2024-11-05 11:46:12 -07:00
internal/lsp/protocol: make loggingStream log writes concurrency-safe
Per the documentation for jsonrpc2.Stream Write must be safe for concurrent use, but this isn't the case for the loggingStream. Guard it with a mutex. Change-Id: I384892b90cef950d518089421d05cf8040c6b233 Reviewed-on: https://go-review.googlesource.com/c/tools/+/227487 Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
parent
77362c5149
commit
97c4fbe514
@ -14,23 +14,28 @@ import (
|
||||
|
||||
type loggingStream struct {
|
||||
stream jsonrpc2.Stream
|
||||
logMu sync.Mutex
|
||||
log io.Writer
|
||||
}
|
||||
|
||||
// LoggingStream returns a stream that does LSP protocol logging too
|
||||
func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
|
||||
return &loggingStream{str, w}
|
||||
return &loggingStream{stream: str, log: w}
|
||||
}
|
||||
|
||||
func (s *loggingStream) Read(ctx context.Context) ([]byte, int64, error) {
|
||||
data, count, err := s.stream.Read(ctx)
|
||||
if err == nil {
|
||||
s.logMu.Lock()
|
||||
defer s.logMu.Unlock()
|
||||
logIn(s.log, data)
|
||||
}
|
||||
return data, count, err
|
||||
}
|
||||
|
||||
func (s *loggingStream) Write(ctx context.Context, data []byte) (int64, error) {
|
||||
s.logMu.Lock()
|
||||
defer s.logMu.Unlock()
|
||||
logOut(s.log, data)
|
||||
count, err := s.stream.Write(ctx, data)
|
||||
return count, err
|
||||
|
Loading…
Reference in New Issue
Block a user