Skip to content

Commit

Permalink
chore: tlsEventHandler OnTraffic use cached buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
andyl committed Apr 7, 2024
1 parent a31400b commit 2933c91
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"time"

"github.com/panjf2000/gnet/v2/pkg/logging"
bbPool "github.com/panjf2000/gnet/v2/pkg/pool/bytebuffer"
"github.com/panjf2000/gnet/v2/pkg/tls"
)

type tlsConn struct {
raw Conn
rawTLSConn *tls.Conn
inboundBuffer *bytes.Buffer
buffer []byte // OnTraffic cache buffer
ctx interface{}
}

Expand Down Expand Up @@ -59,12 +61,12 @@ func (c *tlsConn) ReadFrom(r io.Reader) (n int64, err error) {
}

func (c *tlsConn) Writev(bs [][]byte) (n int, err error) {
// TODO:
var bb []byte
for _, b := range bs {
bb = append(bb, b...)
bb := bbPool.Get()
defer bbPool.Put(bb)
for i := range bs {
_, _ = bb.Write(bs[i])
}
return c.Write(bb)
return c.Write(bb.Bytes())
}

func (c *tlsConn) Flush() (err error) {
Expand Down Expand Up @@ -192,10 +194,14 @@ func (h *tlsEventHandler) OnTraffic(c Conn) (action Action) {

return None
}
// TODO: cache buffer
buffer := make([]byte, 1024*1024)

// grow tlsConn buffer size
if len(tc.buffer) < tc.raw.InboundBuffered() {
tc.buffer = make([]byte, tc.raw.InboundBuffered())
}

for {
n, err := tc.rawTLSConn.Read(buffer)
n, err := tc.rawTLSConn.Read(tc.buffer)
if errors.Is(err, tls.ErrNotEnough) || errors.Is(err, io.EOF) {
break
}
Expand All @@ -206,7 +212,7 @@ func (h *tlsEventHandler) OnTraffic(c Conn) (action Action) {
}

if n > 0 {
tc.inboundBuffer.Write(buffer[:n])
tc.inboundBuffer.Write(tc.buffer[:n])
if tc.raw.InboundBuffered() == 0 {
break
}
Expand Down

0 comments on commit 2933c91

Please sign in to comment.