Skip to content

Commit 0c0fb74

Browse files
authored
Merge pull request #661 from panjf2000/dev
patch: v2.6.2
2 parents c5090c2 + 451f015 commit 0c0fb74

File tree

5 files changed

+49
-63
lines changed

5 files changed

+49
-63
lines changed

connection_unix.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type conn struct {
4949
pollAttachment netpoll.PollAttachment // connection attachment for poller
5050
inboundBuffer elastic.RingBuffer // buffer for leftover data from the remote
5151
buffer []byte // buffer for the latest bytes
52+
cache []byte // temporary cache for the inbound data
5253
isDatagram bool // UDP protocol
5354
opened bool // connection opened event fired
5455
isEOF bool // whether the connection has reached EOF
@@ -290,6 +291,7 @@ func (c *conn) sendTo(buf []byte) error {
290291
func (c *conn) resetBuffer() {
291292
c.buffer = c.buffer[:0]
292293
c.inboundBuffer.Reset()
294+
c.inboundBuffer.Done()
293295
}
294296

295297
func (c *conn) Read(p []byte) (n int, err error) {
@@ -325,22 +327,9 @@ func (c *conn) Next(n int) (buf []byte, err error) {
325327
return
326328
}
327329

328-
head, tail := c.inboundBuffer.Peek(n)
329-
defer c.inboundBuffer.Discard(n) //nolint:errcheck
330-
c.loop.cache.Reset()
331-
c.loop.cache.Write(head)
332-
if len(head) == n {
333-
return c.loop.cache.Bytes(), err
334-
}
335-
c.loop.cache.Write(tail)
336-
if inBufferLen >= n {
337-
return c.loop.cache.Bytes(), err
338-
}
339-
340-
remaining := n - inBufferLen
341-
c.loop.cache.Write(c.buffer[:remaining])
342-
c.buffer = c.buffer[remaining:]
343-
return c.loop.cache.Bytes(), err
330+
buf = bsPool.Get(n)
331+
_, err = c.Read(buf)
332+
return
344333
}
345334

346335
func (c *conn) Peek(n int) (buf []byte, err error) {
@@ -359,25 +348,31 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
359348
if len(head) == n {
360349
return head, err
361350
}
362-
c.loop.cache.Reset()
363-
c.loop.cache.Write(head)
364-
c.loop.cache.Write(tail)
351+
buf = bsPool.Get(n)[:0]
352+
buf = append(buf, head...)
353+
buf = append(buf, tail...)
365354
if inBufferLen >= n {
366-
return c.loop.cache.Bytes(), err
355+
return
367356
}
368357

369358
remaining := n - inBufferLen
370-
c.loop.cache.Write(c.buffer[:remaining])
371-
return c.loop.cache.Bytes(), err
359+
buf = append(buf, c.buffer[:remaining]...)
360+
c.cache = buf
361+
return
372362
}
373363

374364
func (c *conn) Discard(n int) (int, error) {
365+
if len(c.cache) > 0 {
366+
bsPool.Put(c.cache)
367+
c.cache = nil
368+
}
369+
375370
inBufferLen := c.inboundBuffer.Buffered()
376-
tempBufferLen := len(c.buffer)
377-
if inBufferLen+tempBufferLen < n || n <= 0 {
371+
if totalLen := inBufferLen + len(c.buffer); n >= totalLen || n <= 0 {
378372
c.resetBuffer()
379-
return inBufferLen + tempBufferLen, nil
373+
return totalLen, nil
380374
}
375+
381376
if c.inboundBuffer.IsEmpty() {
382377
c.buffer = c.buffer[n:]
383378
return n, nil

connection_windows.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
2828
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
2929
bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer"
30+
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
3031
goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine"
3132
)
3233

@@ -54,6 +55,7 @@ type conn struct {
5455
ctx any // user-defined context
5556
loop *eventloop // owner event-loop
5657
buffer *bbPool.ByteBuffer // reuse memory of inbound data as a temporary buffer
58+
cache []byte // temporary cache for the inbound data
5759
rawConn net.Conn // original connection
5860
localAddr net.Addr // local server addr
5961
remoteAddr net.Addr // remote addr
@@ -116,6 +118,7 @@ func newUDPConn(el *eventloop, pc net.PacketConn, localAddr, remoteAddr net.Addr
116118
func (c *conn) resetBuffer() {
117119
c.buffer.Reset()
118120
c.inboundBuffer.Reset()
121+
c.inboundBuffer.Done()
119122
}
120123

121124
func (c *conn) Read(p []byte) (n int, err error) {
@@ -149,22 +152,10 @@ func (c *conn) Next(n int) (buf []byte, err error) {
149152
c.buffer.B = c.buffer.B[n:]
150153
return
151154
}
152-
head, tail := c.inboundBuffer.Peek(n)
153-
defer c.inboundBuffer.Discard(n) //nolint:errcheck
154-
c.loop.cache.Reset()
155-
c.loop.cache.Write(head)
156-
if len(head) == n {
157-
return c.loop.cache.Bytes(), err
158-
}
159-
c.loop.cache.Write(tail)
160-
if inBufferLen >= n {
161-
return c.loop.cache.Bytes(), err
162-
}
163155

164-
remaining := n - inBufferLen
165-
c.loop.cache.Write(c.buffer.B[:remaining])
166-
c.buffer.B = c.buffer.B[remaining:]
167-
return c.loop.cache.Bytes(), err
156+
buf = bsPool.Get(n)
157+
_, err = c.Read(buf)
158+
return
168159
}
169160

170161
func (c *conn) Peek(n int) (buf []byte, err error) {
@@ -181,25 +172,31 @@ func (c *conn) Peek(n int) (buf []byte, err error) {
181172
if len(head) == n {
182173
return head, err
183174
}
184-
c.loop.cache.Reset()
185-
c.loop.cache.Write(head)
186-
c.loop.cache.Write(tail)
175+
buf = bsPool.Get(n)[:0]
176+
buf = append(buf, head...)
177+
buf = append(buf, tail...)
187178
if inBufferLen >= n {
188-
return c.loop.cache.Bytes(), err
179+
return
189180
}
190181

191182
remaining := n - inBufferLen
192-
c.loop.cache.Write(c.buffer.B[:remaining])
193-
return c.loop.cache.Bytes(), err
183+
buf = append(buf, c.buffer.B[:remaining]...)
184+
c.cache = buf
185+
return
194186
}
195187

196188
func (c *conn) Discard(n int) (int, error) {
189+
if len(c.cache) > 0 {
190+
bsPool.Put(c.cache)
191+
c.cache = nil
192+
}
193+
197194
inBufferLen := c.inboundBuffer.Buffered()
198-
tempBufferLen := c.buffer.Len()
199-
if inBufferLen+tempBufferLen < n || n <= 0 {
195+
if totalLen := inBufferLen + c.buffer.Len(); n >= totalLen || n <= 0 {
200196
c.resetBuffer()
201-
return inBufferLen + tempBufferLen, nil
197+
return totalLen, nil
202198
}
199+
203200
if c.inboundBuffer.IsEmpty() {
204201
c.buffer.B = c.buffer.B[n:]
205202
return n, nil

eventloop_unix.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package gnet
1919

2020
import (
21-
"bytes"
2221
"context"
2322
"errors"
2423
"fmt"
@@ -39,7 +38,6 @@ import (
3938
type eventloop struct {
4039
listeners map[int]*listener // listeners
4140
idx int // loop index in the engine loops list
42-
cache bytes.Buffer // temporary buffer for scattered bytes
4341
engine *engine // engine in loop
4442
poller *netpoll.Poller // epoll or kqueue
4543
buffer []byte // read packet buffer whose capacity is set by user, default value is 64KB

eventloop_windows.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package gnet
1616

1717
import (
18-
"bytes"
1918
"context"
2019
"errors"
2120
"fmt"
@@ -31,7 +30,6 @@ type eventloop struct {
3130
ch chan any // channel for event-loop
3231
idx int // index of event-loop in event-loops
3332
eng *engine // engine in loop
34-
cache bytes.Buffer // temporary buffer for scattered bytes
3533
connCount int32 // number of active connections in event-loop
3634
connections map[*conn]struct{} // TCP connection map: fd -> conn
3735
eventHandler EventHandler // user eventHandler

pkg/buffer/linkedlist/linked_list_buffer.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (b *node) len() int {
3232

3333
// Buffer is a linked list of node.
3434
type Buffer struct {
35-
bs [][]byte
3635
head *node
3736
tail *node
3837
size int
@@ -123,19 +122,19 @@ func (llb *Buffer) Peek(maxBytes int) ([][]byte, error) {
123122
} else if maxBytes > llb.Buffered() {
124123
return nil, io.ErrShortBuffer
125124
}
126-
llb.bs = llb.bs[:0]
125+
var bs [][]byte
127126
var cum int
128127
for iter := llb.head; iter != nil; iter = iter.next {
129128
offset := iter.len()
130129
if cum+offset > maxBytes {
131130
offset = maxBytes - cum
132131
}
133-
llb.bs = append(llb.bs, iter.buf[:offset])
132+
bs = append(bs, iter.buf[:offset])
134133
if cum += offset; cum == maxBytes {
135134
break
136135
}
137136
}
138-
return llb.bs, nil
137+
return bs, nil
139138
}
140139

141140
// PeekWithBytes is like Peek but accepts [][]byte and puts them onto head.
@@ -145,17 +144,17 @@ func (llb *Buffer) PeekWithBytes(maxBytes int, bs ...[]byte) ([][]byte, error) {
145144
} else if maxBytes > llb.Buffered() {
146145
return nil, io.ErrShortBuffer
147146
}
148-
llb.bs = llb.bs[:0]
147+
var bss [][]byte
149148
var cum int
150149
for _, b := range bs {
151150
if n := len(b); n > 0 {
152151
offset := n
153152
if cum+offset > maxBytes {
154153
offset = maxBytes - cum
155154
}
156-
llb.bs = append(llb.bs, b[:offset])
155+
bss = append(bss, b[:offset])
157156
if cum += offset; cum == maxBytes {
158-
return llb.bs, nil
157+
return bss, nil
159158
}
160159
}
161160
}
@@ -164,12 +163,12 @@ func (llb *Buffer) PeekWithBytes(maxBytes int, bs ...[]byte) ([][]byte, error) {
164163
if cum+offset > maxBytes {
165164
offset = maxBytes - cum
166165
}
167-
llb.bs = append(llb.bs, iter.buf[:offset])
166+
bss = append(bss, iter.buf[:offset])
168167
if cum += offset; cum == maxBytes {
169168
break
170169
}
171170
}
172-
return llb.bs, nil
171+
return bss, nil
173172
}
174173

175174
// Discard removes some nodes based on n bytes.
@@ -266,7 +265,6 @@ func (llb *Buffer) Reset() {
266265
llb.tail = nil
267266
llb.size = 0
268267
llb.bytes = 0
269-
llb.bs = nil
270268
}
271269

272270
// pop returns and removes the head of l. If l is empty, it returns nil.

0 commit comments

Comments
 (0)