-
Notifications
You must be signed in to change notification settings - Fork 609
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup: refactor and use bicopy everywhere
There are 3 instances of this pattern and each does this slightly differently. Clean up the implementation to return errors using `errors.Join` (which wasn't available when the original was written) and use it everywhere. This doesn't change behavior because the error return is always just logged (see the only called of `(*pseudoLoopbackForwarder).forward`. Note that the removal of the special handling of `io.EOF` returned from `io.Copy` doesn't change behavior because it can never happen per the latter's documentation. Signed-off-by: Tamir Duberstein <[email protected]>
- Loading branch information
Showing
4 changed files
with
68 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,72 @@ | ||
// From https://raw.githubusercontent.com/norouter/norouter/v0.6.5/pkg/agent/bicopy/bicopy.go | ||
/* | ||
Copyright (C) NoRouter authors. | ||
Copyright (C) libnetwork authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package bicopy | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"sync" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Bicopy is from https://github.com/rootless-containers/rootlesskit/blob/v0.10.1/pkg/port/builtin/parent/tcp/tcp.go#L73-L104 | ||
// (originally from libnetwork, Apache License 2.0). | ||
func Bicopy(x, y io.ReadWriter, quit <-chan struct{}) { | ||
type closeReader interface { | ||
func closeRead(c io.ReadWriter) error { | ||
if c, ok := c.(interface { | ||
CloseRead() error | ||
}); ok { | ||
return c.CloseRead() | ||
} | ||
type closeWriter interface { | ||
return nil | ||
} | ||
|
||
func closeWrite(c io.ReadWriter) error { | ||
if c, ok := c.(interface { | ||
CloseWrite() error | ||
}); ok { | ||
return c.CloseWrite() | ||
} | ||
var wg sync.WaitGroup | ||
broker := func(to, from io.ReadWriter) { | ||
if _, err := io.Copy(to, from); err != nil { | ||
logrus.WithError(err).Debug("failed to call io.Copy") | ||
} | ||
if fromCR, ok := from.(closeReader); ok { | ||
if err := fromCR.CloseRead(); err != nil { | ||
logrus.WithError(err).Debug("failed to call CloseRead") | ||
} | ||
} | ||
if toCW, ok := to.(closeWriter); ok { | ||
if err := toCW.CloseWrite(); err != nil { | ||
logrus.WithError(err).Debug("failed to call CloseWrite") | ||
} | ||
} | ||
wg.Done() | ||
return nil | ||
} | ||
|
||
// Avoid shadowing the built-in `close`. | ||
func ioClose(c io.ReadWriter) error { | ||
if c, ok := c.(io.Closer); ok { | ||
return c.Close() | ||
} | ||
return nil | ||
} | ||
|
||
func broker(dst, src io.ReadWriter, dstName, srcName string) error { | ||
_, errCopy := io.Copy(dst, src) | ||
if errCopy != nil { | ||
errCopy = fmt.Errorf("io.Copy(%s, %s): %w", srcName, dstName, errCopy) | ||
} | ||
errCloseRead := closeRead(src) | ||
if errCloseRead != nil { | ||
errCloseRead = fmt.Errorf("closeRead(%s): %w", srcName, errCloseRead) | ||
} | ||
errCloseWrite := closeWrite(dst) | ||
if errCloseWrite != nil { | ||
errCloseWrite = fmt.Errorf("closeWrite(%s): %w", dstName, errCloseWrite) | ||
} | ||
return errors.Join(errCopy, errCloseRead, errCloseWrite) | ||
} | ||
|
||
func Bicopy(left, right io.ReadWriter, leftName, rightName string) error { | ||
var wg sync.WaitGroup | ||
wg.Add(2) | ||
go broker(x, y) | ||
go broker(y, x) | ||
finish := make(chan struct{}) | ||
var errLeft, errRight error | ||
go func() { | ||
wg.Wait() | ||
close(finish) | ||
errLeft = broker(left, right, leftName, rightName) | ||
}() | ||
|
||
select { | ||
case <-quit: | ||
case <-finish: | ||
} | ||
if xCloser, ok := x.(io.Closer); ok { | ||
if err := xCloser.Close(); err != nil { | ||
logrus.WithError(err).Debug("failed to call xCloser.Close") | ||
} | ||
go func() { | ||
errRight = broker(right, left, rightName, leftName) | ||
}() | ||
wg.Wait() | ||
errCloseLeft := ioClose(left) | ||
if errCloseLeft != nil { | ||
errCloseLeft = fmt.Errorf("ioClose(%s): %w", leftName, errCloseLeft) | ||
} | ||
if yCloser, ok := y.(io.Closer); ok { | ||
if err := yCloser.Close(); err != nil { | ||
logrus.WithError(err).Debug("failed to call yCloser.Close") | ||
} | ||
errCloseRight := ioClose(right) | ||
if errCloseRight != nil { | ||
errCloseRight = fmt.Errorf("ioClose(%s): %w", rightName, errCloseRight) | ||
} | ||
<-finish | ||
// TODO: return copied bytes | ||
return errors.Join(errLeft, errRight, errCloseLeft, errCloseRight) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters