fix too many open files bug

This commit is contained in:
p_caiwfeng
2021-12-06 14:44:40 +08:00
parent 7b2d699c46
commit c0daf721ea
2 changed files with 28 additions and 3 deletions

View File

@@ -4,11 +4,10 @@ import (
"bytes"
"context"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/wencaiwulue/kubevpn/util"
"net"
"time"
log "github.com/sirupsen/logrus"
)
type fakeUDPTunConnector struct {
@@ -236,3 +235,21 @@ func (c *fakeUDPTunnelConn) WriteTo(b []byte, addr net.Addr) (n int, err error)
}
return len(b), nil
}
func (c *fakeUDPTunnelConn) Close() error {
return c.Conn.Close()
}
func (c *fakeUDPTunnelConn) CloseWrite() error {
if cc, ok := c.Conn.(interface{ CloseWrite() error }); ok {
return cc.CloseWrite()
}
return nil
}
func (c *fakeUDPTunnelConn) CloseRead() error {
if cc, ok := c.Conn.(interface{ CloseRead() error }); ok {
return cc.CloseRead()
}
return nil
}

View File

@@ -135,7 +135,15 @@ func (h *tunHandler) findRouteFor(dst net.IP) net.Addr {
func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.Addr) error {
errChan := make(chan error, 2)
defer conn.Close()
defer func() {
if c, ok := conn.(interface{ CloseRead() error }); ok {
_ = c.CloseRead()
}
if c, ok := conn.(interface{ CloseWrite() error }); ok {
_ = c.CloseWrite()
}
_ = conn.Close()
}()
ctx, cancelFunc := context.WithCancel(context.Background())
remote.CancelFunctions = append(remote.CancelFunctions, cancelFunc)
go func() {