Skip to content

Commit

Permalink
Improve rxStream performance using bufio reader
Browse files Browse the repository at this point in the history
Signed-off-by: Balaji Vijayakumar <[email protected]>
  • Loading branch information
balajiv113 authored and cfergeau committed Mar 10, 2023
1 parent f978779 commit 7bb4a5d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/tap/protocol.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tap

import "encoding/binary"
import (
"encoding/binary"
)

type protocol interface {
Stream() bool
Expand Down
6 changes: 4 additions & 2 deletions pkg/tap/switch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tap

import (
"bufio"
"context"
"io"
"net"
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down

0 comments on commit 7bb4a5d

Please sign in to comment.