k3s/pkg/dqlite/pipe/pipe.go

47 lines
852 B
Go
Raw Normal View History

2019-10-31 02:05:40 +00:00
package pipe
import (
"io"
"net"
"github.com/lxc/lxd/shared/eagain"
"github.com/sirupsen/logrus"
)
func UnixPiper(srcs <-chan net.Conn, bindAddress string) {
for src := range srcs {
go Unix(src, bindAddress)
}
}
func Unix(src net.Conn, target string) error {
dst, err := net.Dial("unix", target)
if err != nil {
src.Close()
return err
}
Connect(src, dst)
return nil
}
func Connect(src net.Conn, dst net.Conn) {
go func() {
_, err := io.Copy(eagain.Writer{Writer: dst}, eagain.Reader{Reader: src})
if err != nil && err != io.EOF {
2019-11-11 22:18:43 +00:00
logrus.Debugf("copy pipe src->dst closed: %v", err)
2019-10-31 02:05:40 +00:00
}
src.Close()
dst.Close()
}()
go func() {
_, err := io.Copy(eagain.Writer{Writer: src}, eagain.Reader{Reader: dst})
if err != nil {
2019-11-11 22:18:43 +00:00
logrus.Debugf("copy pipe dst->src closed: %v", err)
2019-10-31 02:05:40 +00:00
}
src.Close()
dst.Close()
}()
}