Skip to content

Commit

Permalink
chore: make some trivial changes to ring-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jul 10, 2021
1 parent ee965a1 commit 1bdd3aa
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions connection_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
50 changes: 25 additions & 25 deletions ringbuffer/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions ringbuffer/ring_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand All @@ -324,32 +324,32 @@ 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)
}
_, _ = rb.Write([]byte(strings.Repeat("1234", 4)))
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))
}
if !(bytes.Equal(head, []byte(strings.Repeat("1234", 16))) && bytes.Equal(tail, []byte(strings.Repeat("1234", 4)))) {
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))
}
if !(bytes.Equal(head, []byte(strings.Repeat("1234", 16))) && bytes.Equal(tail, []byte(strings.Repeat("1234", 4)))) {
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")
}
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 1bdd3aa

Please sign in to comment.