diff --git a/metadapta.go b/metadapta.go index ab0866d..b9464ec 100644 --- a/metadapta.go +++ b/metadapta.go @@ -232,8 +232,8 @@ type transA struct { closed atomic.Bool closeErr error - currentReader io.Reader - currentWriter io.Closer + currentReader io.Reader + currentWriter usync.Monitor[io.Closer] deadline *time.Timer deadlineLock sync.Mutex @@ -270,10 +270,13 @@ func (this *transA) Send(method uint16, data []byte) error { } func (this *transA) SendWriter(method uint16) (io.WriteCloser, error) { + currentWriter, done := this.currentWriter.BorrowReturn() + defer done(¤tWriter) + // close previous writer if necessary - if this.currentWriter != nil { - this.currentWriter.Close() - this.currentWriter = nil + if currentWriter != nil { + currentWriter.Close() + currentWriter = nil } // create new writer @@ -284,7 +287,7 @@ func (this *transA) SendWriter(method uint16) (io.WriteCloser, error) { chunkSize: defaultChunkSize, open: true, } - this.currentWriter = writer + currentWriter = writer return writer, nil }