refactor(writer): Optimizes the FrameWriter write logic and cleans up irrelevant comments.

Redundant comments have been removed from the `WriteFrame` and heartbeat control methods, and logic for releasing unprocessed frames in the queue has been added to the `Close` method to ensure proper resource reclamation. Some unnecessary channel signal comments have also been removed, making the code more concise and clear.
This commit is contained in:
Gouryella
2025-12-03 18:32:38 +08:00
parent 35e6c86e1f
commit 8086657b7a

View File

@@ -128,15 +128,12 @@ func (w *FrameWriter) flushBatchLocked() {
for _, frame := range w.batch {
_ = WriteFrame(w.conn, frame)
// Release pooled buffer after writing
frame.Release()
}
w.batch = w.batch[:0]
}
// WriteFrame queues a frame to be written by the write loop.
// Blocks if the queue is full to ensure all writes go through the single write loop.
func (w *FrameWriter) WriteFrame(frame *Frame) error {
w.mu.Lock()
if w.closed {
@@ -163,6 +160,11 @@ func (w *FrameWriter) Close() error {
w.mu.Unlock()
close(w.queue)
for frame := range w.queue {
frame.Release()
}
close(w.done)
return nil
@@ -174,7 +176,6 @@ func (w *FrameWriter) Flush() {
w.flushBatchLocked()
}
// EnableHeartbeat enables automatic heartbeat sending in the write loop.
func (w *FrameWriter) EnableHeartbeat(interval time.Duration, callback func() *Frame) {
w.mu.Lock()
w.heartbeatInterval = interval
@@ -185,11 +186,9 @@ func (w *FrameWriter) EnableHeartbeat(interval time.Duration, callback func() *F
select {
case w.heartbeatControl <- struct{}{}:
default:
// Channel already has a pending signal, no need to send another
}
}
// DisableHeartbeat disables automatic heartbeat sending.
func (w *FrameWriter) DisableHeartbeat() {
w.mu.Lock()
w.heartbeatEnabled = false
@@ -198,6 +197,5 @@ func (w *FrameWriter) DisableHeartbeat() {
select {
case w.heartbeatControl <- struct{}{}:
default:
// Channel already has a pending signal, no need to send another
}
}