From 7bb4a5d821ece520bab1c8a1c0858de837a2ab22 Mon Sep 17 00:00:00 2001 From: Balaji Vijayakumar Date: Wed, 1 Mar 2023 22:23:43 +0530 Subject: [PATCH] Improve rxStream performance using bufio reader Signed-off-by: Balaji Vijayakumar --- pkg/tap/protocol.go | 4 +++- pkg/tap/switch.go | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/tap/protocol.go b/pkg/tap/protocol.go index d32d859ac..4fe09fefe 100644 --- a/pkg/tap/protocol.go +++ b/pkg/tap/protocol.go @@ -1,6 +1,8 @@ package tap -import "encoding/binary" +import ( + "encoding/binary" +) type protocol interface { Stream() bool diff --git a/pkg/tap/switch.go b/pkg/tap/switch.go index 9b88896dd..a05b9ccbf 100644 --- a/pkg/tap/switch.go +++ b/pkg/tap/switch.go @@ -1,6 +1,7 @@ package tap import ( + "bufio" "context" "io" "net" @@ -220,6 +221,7 @@ loop: } func (e *Switch) rxStream(ctx context.Context, id int, conn net.Conn, sProtocol streamProtocol) error { + reader := bufio.NewReader(conn) sizeBuf := sProtocol.Buf() loop: for { @@ -229,14 +231,14 @@ loop: default: // passthrough } - _, err := io.ReadFull(conn, sizeBuf) + _, err := io.ReadFull(reader, sizeBuf) if err != nil { return errors.Wrap(err, "cannot read size from socket") } size := sProtocol.Read(sizeBuf) buf := make([]byte, size) - _, err = io.ReadFull(conn, buf) + _, err = io.ReadFull(reader, buf) if err != nil { return errors.Wrap(err, "cannot read packet from socket") }