Skip to content

portfwd: Optimize errors.Is() checks #2969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions pkg/portfwd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@ func HandleTCPConnection(ctx context.Context, client *guestagentclient.GuestAgen
rw := &GrpcClientRW{stream: stream, id: id, addr: guestAddr}
g.Go(func() error {
_, err := io.Copy(rw, conn)
if errors.Is(err, io.EOF) {
return nil
}
return err
})
g.Go(func() error {
_, err := io.Copy(conn, rw)
if errors.Is(err, io.EOF) {
return nil
}
return err
})

Expand All @@ -64,10 +58,11 @@ func HandleUDPConnection(ctx context.Context, client *guestagentclient.GuestAgen
buf := make([]byte, 65507)
for {
n, addr, err := conn.ReadFrom(buf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callers should always process
// the n > 0 bytes returned before considering the error err.

https://pkg.go.dev/net#PacketConn

Seems this code is wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, this is a bug in the code, but it is not related this change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #2970

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixd by #2971, thanks for pointing out this isue!

if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
// https://pkg.go.dev/net#PacketConn does not mention io.EOF semantics.
if errors.Is(err, io.EOF) {
return nil
}
return err
}
msg := &api.TunnelMessage{
Expand All @@ -86,10 +81,10 @@ func HandleUDPConnection(ctx context.Context, client *guestagentclient.GuestAgen
g.Go(func() error {
for {
in, err := stream.Recv()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return err
}
addr, err := net.ResolveUDPAddr("udp", in.UdpTargetAddr)
Expand Down Expand Up @@ -134,10 +129,10 @@ func (g GrpcClientRW) Write(p []byte) (n int, err error) {

func (g GrpcClientRW) Read(p []byte) (n int, err error) {
in, err := g.stream.Recv()
if errors.Is(err, io.EOF) {
return 0, nil
}
if err != nil {
if errors.Is(err, io.EOF) {
return 0, nil
}
return 0, err
}
if len(in.Data) == 0 {
Expand Down
Loading