-
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
109 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,71 @@ | ||
// 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, name string) error { | ||
if c, ok := c.(interface { | ||
CloseRead() error | ||
}); ok { | ||
if err := c.CloseRead(); err != nil { | ||
return fmt.Errorf("(%T).CloseRead(%s): %w", c, name, err) | ||
} | ||
} | ||
type closeWriter interface { | ||
return nil | ||
} | ||
|
||
func closeWrite(c io.ReadWriter, name string) error { | ||
if c, ok := c.(interface { | ||
CloseWrite() error | ||
} | ||
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") | ||
}); ok { | ||
if err := c.CloseWrite(); err != nil { | ||
return fmt.Errorf("(%T).CloseWrite(%s): %w", c, name, err) | ||
} | ||
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") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Avoid shadowing the built-in `close`. | ||
func ioClose(c io.ReadWriter, name string) error { | ||
if c, ok := c.(io.Closer); ok { | ||
if err := c.Close(); err != nil { | ||
return fmt.Errorf("(%T).Close(%s): %w", c, name, err) | ||
} | ||
wg.Done() | ||
} | ||
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", dstName, srcName, errCopy) | ||
} | ||
return errors.Join( | ||
errCopy, | ||
closeWrite(dst, dstName), | ||
closeRead(src, srcName), | ||
) | ||
} | ||
|
||
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") | ||
} | ||
} | ||
if yCloser, ok := y.(io.Closer); ok { | ||
if err := yCloser.Close(); err != nil { | ||
logrus.WithError(err).Debug("failed to call yCloser.Close") | ||
} | ||
} | ||
<-finish | ||
// TODO: return copied bytes | ||
go func() { | ||
errRight = broker(right, left, rightName, leftName) | ||
}() | ||
wg.Wait() | ||
return errors.Join( | ||
errLeft, | ||
errRight, | ||
ioClose(left, leftName), | ||
ioClose(right, rightName), | ||
) | ||
} |
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