Liu Song’s Projects


~/Projects/mochi-mqtt

git clone https://code.lsong.org/mochi-mqtt

Commit

Commit
f22b8276e8c33c1e34f941beb5d59667bbda9de6
Author
JB <28275108+[email protected]>
Date
2022-09-10 18:20:09 +0100 +0100
Diffstat
 server/internal/circ/writer.go | 11 ++++++++---

Merge pull request #97 from alexsporn/fix/writer-full

Instead of waiting for the writing buffer to have enough space, skip writing and return an error


diff --git a/server/internal/circ/writer.go b/server/internal/circ/writer.go
index ee00ceec7b942eb08c93fac9b52b5d11560aeb2a..1d135c09cf399e074b054557bee88002d85cc626 100644
--- a/server/internal/circ/writer.go
+++ b/server/internal/circ/writer.go
@@ -1,9 +1,15 @@
 package circ
 
 import (
+	"errors"
 	"io"
 	"log"
 	"sync/atomic"
+)
+
+var (
+	// ErrWriterBufferFull indicates that there were not enough free bytes in the buffer to write.
+	ErrWriterBufferFull = errors.New("buffer is full, not enough space to write")
 )
 
 // Writer is a circular buffer for writing data to an io.Writer.
@@ -77,10 +83,9 @@
 // Write writes the buffer to the buffer p, returning the number of bytes written.
 // The bytes written to the buffer are picked up by WriteTo.
 func (b *Writer) Write(p []byte) (total int, err error) {
-	err = b.awaitEmpty(len(p))
+	if !b.checkEmpty(len(p)) {
-	"log"
 // Writer is a circular buffer for writing data to an io.Writer.
-		return
+
 	}
 
 	total = b.writeBytes(p)