From 1bdd3aa77da3827268e1145a07443460e1f01bd5 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 10 Jul 2021 20:44:46 +0800 Subject: [PATCH] chore: make some trivial changes to ring-buffer --- connection_unix.go | 4 +-- connection_windows.go | 4 +-- eventloop_unix.go | 6 ++-- ringbuffer/ring_buffer.go | 50 +++++++++++++++++----------------- ringbuffer/ring_buffer_test.go | 24 ++++++++-------- 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/connection_unix.go b/connection_unix.go index 0d4945105..c4b9f0e72 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -174,7 +174,7 @@ func (c *conn) ReadN(n int) (size int, buf []byte) { buf = c.buffer[:n] return } - head, tail := c.inboundBuffer.LazyRead(n) + head, tail := c.inboundBuffer.Peek(n) c.byteBuffer = bytebuffer.Get() _, _ = c.byteBuffer.Write(head) _, _ = c.byteBuffer.Write(tail) @@ -207,7 +207,7 @@ func (c *conn) ShiftN(n int) (size int) { c.byteBuffer = nil if inBufferLen >= n { - c.inboundBuffer.Shift(n) + c.inboundBuffer.Discard(n) return } c.inboundBuffer.Reset() diff --git a/connection_windows.go b/connection_windows.go index 943e60b3c..068372782 100644 --- a/connection_windows.go +++ b/connection_windows.go @@ -181,7 +181,7 @@ func (c *stdConn) ReadN(n int) (size int, buf []byte) { buf = c.buffer.B[:n] return } - head, tail := c.inboundBuffer.LazyRead(n) + head, tail := c.inboundBuffer.Peek(n) c.byteBuffer = bytebuffer.Get() _, _ = c.byteBuffer.Write(head) _, _ = c.byteBuffer.Write(tail) @@ -214,7 +214,7 @@ func (c *stdConn) ShiftN(n int) (size int) { c.byteBuffer = nil if inBufferLen >= n { - c.inboundBuffer.Shift(n) + c.inboundBuffer.Discard(n) return } c.inboundBuffer.Reset() diff --git a/eventloop_unix.go b/eventloop_unix.go index 277bd3ae4..b5330a649 100644 --- a/eventloop_unix.go +++ b/eventloop_unix.go @@ -185,9 +185,9 @@ func (el *eventloop) loopRead(c *conn) error { func (el *eventloop) loopWrite(c *conn) error { el.eventHandler.PreWrite() - head, tail := c.outboundBuffer.LazyReadAll() + head, tail := c.outboundBuffer.PeekAll() n, err := io.Writev(c.fd, [][]byte{head, tail}) - c.outboundBuffer.Shift(n) + c.outboundBuffer.Discard(n) switch err { case nil, gerrors.ErrShortWritev: // do nothing, just go on case unix.EAGAIN: @@ -214,7 +214,7 @@ func (el *eventloop) loopCloseConn(c *conn, err error) (rerr error) { if !c.outboundBuffer.IsEmpty() { el.eventHandler.PreWrite() - head, tail := c.outboundBuffer.LazyReadAll() + head, tail := c.outboundBuffer.PeekAll() if n, err := unix.Write(c.fd, head); err == nil { if n == len(head) && tail != nil { _, _ = unix.Write(c.fd, tail) diff --git a/ringbuffer/ring_buffer.go b/ringbuffer/ring_buffer.go index bc8f86d56..f8d6acc78 100644 --- a/ringbuffer/ring_buffer.go +++ b/ringbuffer/ring_buffer.go @@ -58,44 +58,44 @@ func New(size int) *RingBuffer { } } -// LazyRead reads the bytes with given length but will not move the pointer of "read". -func (r *RingBuffer) LazyRead(len int) (head []byte, tail []byte) { +// Peek returns the next n bytes without advancing the read pointer. +func (r *RingBuffer) Peek(n int) (head []byte, tail []byte) { if r.isEmpty { return } - if len <= 0 { + if n <= 0 { return } if r.w > r.r { - n := r.w - r.r // Length - if n > len { - n = len + m := r.w - r.r // length of ring-buffer + if m > n { + m = n } - head = r.buf[r.r : r.r+n] + head = r.buf[r.r : r.r+m] return } - n := r.size - r.r + r.w // Length - if n > len { - n = len + m := r.size - r.r + r.w // length of ring-buffer + if m > n { + m = n } - if r.r+n <= r.size { - head = r.buf[r.r : r.r+n] + if r.r+m <= r.size { + head = r.buf[r.r : r.r+m] } else { c1 := r.size - r.r head = r.buf[r.r:] - c2 := n - c1 + c2 := m - c1 tail = r.buf[:c2] } return } -// LazyReadAll reads the all bytes in this ring-buffer but will not move the pointer of "read". -func (r *RingBuffer) LazyReadAll() (head []byte, tail []byte) { +// PeekAll returns all bytes without advancing the read pointer. +func (r *RingBuffer) PeekAll() (head []byte, tail []byte) { if r.isEmpty { return } @@ -113,8 +113,8 @@ func (r *RingBuffer) LazyReadAll() (head []byte, tail []byte) { return } -// Shift shifts the "read" pointer. -func (r *RingBuffer) Shift(n int) { +// Discard skips the next n bytes by advancing the read pointer. +func (r *RingBuffer) Discard(n int) { if n <= 0 { return } @@ -177,7 +177,7 @@ func (r *RingBuffer) Read(p []byte) (n int, err error) { r.Reset() } - return n, err + return } // ReadByte reads and returns the next byte from the input or ErrIsEmpty. @@ -194,7 +194,7 @@ func (r *RingBuffer) ReadByte() (b byte, err error) { r.Reset() } - return b, err + return } // Write writes len(p) bytes from p to the underlying buf. @@ -206,7 +206,7 @@ func (r *RingBuffer) ReadByte() (b byte, err error) { func (r *RingBuffer) Write(p []byte) (n int, err error) { n = len(p) if n == 0 { - return 0, nil + return } free := r.Free() @@ -236,7 +236,7 @@ func (r *RingBuffer) Write(p []byte) (n int, err error) { r.isEmpty = false - return n, err + return } // WriteByte writes one byte into buffer. @@ -298,7 +298,7 @@ func (r *RingBuffer) Free() int { } // WriteString writes the contents of the string s to buffer, which accepts a slice of bytes. -func (r *RingBuffer) WriteString(s string) (n int, err error) { +func (r *RingBuffer) WriteString(s string) (int, error) { return r.Write(internal.StringToBytes(s)) } @@ -358,17 +358,17 @@ func (r *RingBuffer) WithByteBuffer(b []byte) *bytebuffer.ByteBuffer { return bb } -// IsFull returns this ringbuffer is full. +// IsFull tells if this ring-buffer is full. func (r *RingBuffer) IsFull() bool { return r.r == r.w && !r.isEmpty } -// IsEmpty returns this ringbuffer is empty. +// IsEmpty tells if this ring-buffer is empty. func (r *RingBuffer) IsEmpty() bool { return r.isEmpty } -// Reset the read pointer and writer pointer to zero. +// Reset the read pointer and write pointer to zero. func (r *RingBuffer) Reset() { r.isEmpty = true r.r, r.w = 0, 0 diff --git a/ringbuffer/ring_buffer_test.go b/ringbuffer/ring_buffer_test.go index ca8bf0088..5037de5ba 100644 --- a/ringbuffer/ring_buffer_test.go +++ b/ringbuffer/ring_buffer_test.go @@ -192,11 +192,11 @@ func TestRingBuffer_Write(t *testing.T) { func TestZeroRingBuffer(t *testing.T) { rb := New(0) - head, tail := rb.LazyRead(1) + head, tail := rb.Peek(1) if !(head == nil && tail == nil) { t.Fatal("expect head and tail are all nil") } - head, tail = rb.LazyReadAll() + head, tail = rb.PeekAll() if !(head == nil && tail == nil) { t.Fatal("expect head and tail are all nil") } @@ -218,7 +218,7 @@ func TestZeroRingBuffer(t *testing.T) { if !bytes.Equal(rb.ByteBuffer().Bytes(), buf) { t.Fatal("expect it is equal") } - rb.Shift(48) + rb.Discard(48) if !(rb.IsEmpty() && rb.r == 0 && rb.w == 0) { t.Fatalf("expect rb is empty and rb.r=rb.w=0, but got rb.r=%d and rb.w=%d", rb.r, rb.w) } @@ -314,7 +314,7 @@ func TestRingBuffer_Read(t *testing.T) { if rb.w != 0 { t.Fatalf("expect r.2=0 but got %d. r.r=%d", rb.w, rb.r) } - head, tail := rb.LazyRead(64) + head, tail := rb.Peek(64) if !(len(head) == 64 && tail == nil) { t.Fatalf("expect len(head)=64 and tail=nil, yet len(head)=%d and tail != nil", len(head)) } @@ -324,7 +324,7 @@ func TestRingBuffer_Read(t *testing.T) { if !bytes.Equal(head, []byte(strings.Repeat("1234", 16))) { t.Fatal("should be equal") } - rb.Shift(64) + rb.Discard(64) if rb.r != 64 { t.Fatalf("expect r.r=64 but got %d", rb.r) } @@ -332,7 +332,7 @@ func TestRingBuffer_Read(t *testing.T) { if rb.w != 16 { t.Fatalf("expect r.w=16 but got %d", rb.w) } - head, tail = rb.LazyRead(128) + head, tail = rb.Peek(128) if !(len(head) == 64 && len(tail) == 16) { t.Fatalf("expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail)) } @@ -340,7 +340,7 @@ func TestRingBuffer_Read(t *testing.T) { t.Fatalf("head: %s, tail: %s", string(head), string(tail)) } - head, tail = rb.LazyReadAll() + head, tail = rb.PeekAll() if !(len(head) == 64 && len(tail) == 16) { t.Fatalf("expect len(head)=64 and len(tail)=16, yet len(head)=%d and len(tail)=%d", len(head), len(tail)) } @@ -348,8 +348,8 @@ func TestRingBuffer_Read(t *testing.T) { t.Fatalf("head: %s, tail: %s", string(head), string(tail)) } - rb.Shift(64) - rb.Shift(16) + rb.Discard(64) + rb.Discard(16) if !rb.isEmpty { t.Fatal("should be empty") } @@ -482,14 +482,14 @@ func TestShrinkBuffer(t *testing.T) { rb := New(testCap) _, _ = rb.WriteString(testStr) - rb.LazyReadAll() - rb.Shift(len(testStr)) + rb.PeekAll() + rb.Discard(len(testStr)) if rb.Cap() != testCap/2 { t.Fatalf("expect buffer capacity %d, but got %d", testCap/2, rb.Cap()) } _, _ = rb.WriteString(testStr) - rb.LazyReadAll() + rb.PeekAll() rb.Reset() if rb.Cap() != testCap/4 { t.Fatalf("expect buffer capacity %d, but got %d", testCap/4, rb.Cap())