Skip to content

Commit

Permalink
修复 l2l 监听地址和客户端localaddr不匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
456vv committed Apr 26, 2024
1 parent 6479aca commit f85b467
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 42 additions & 0 deletions forward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,45 @@ func Test_L2L_0(t *testing.T) {
time.Sleep(time.Second)
as.Equal(birdge.ConnNum(), 0)
}

func Test_L2L_1(t *testing.T) {
as := assert.New(t, true)

ll := new(L2L)
ll.MaxConn(0)
ll.KeptIdeConn(1)
defer ll.Close()

birdge, err := ll.Transport(addra, addrb)
as.NotError(err)
defer birdge.Close()

addra.Local = ll.alisten.Addr()
addrb.Local = ll.blisten.Addr()

go func() {
defer birdge.Close()

conna, err := net.Dial(addra.Network, addra.Local.String())
as.NotError(err)
defer conna.Close()

connb, err := net.Dial(addrb.Network, addrb.Local.String())
as.NotError(err)
defer connb.Close()

// 发送
b := make([]byte, 10)
rand.Reader.Read(b)
conna.Write(b)

connb.SetReadDeadline(time.Now().Add(time.Second))
p := make([]byte, 10)
n, err := connb.Read(p)
as.NotError(err).Equal(b[:n], p[:n])
}()

birdge.Swap()
time.Sleep(time.Second)
as.Equal(birdge.ConnNum(), 0)
}
8 changes: 4 additions & 4 deletions l2l.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (T *L2LSwap) Swap() error {
// T.ll.logf("%s 池中读取连接错误: %s", T.ll.blisten.Addr().String(), err)
atomic.AddInt32(&T.ll.currUseConn, -2)
// 重新进池
err = T.ll.acp.Put(conna, conna.LocalAddr())
err = T.ll.acp.Put(conna, T.ll.alisten.Addr())
if err != nil {
// T.ll.logf("%s 连接加入 %s 池中读取连接错误: %s", conna.RemoteAddr().String(), conna.LocalAddr().String(), err)
conna.Close()
Expand Down Expand Up @@ -227,11 +227,11 @@ func (T *L2L) bufConn(l net.Listener, cp *vconnpool.ConnPool, verify *func(net.C
}
tempDelay = 0

go T.examineConn(conn, verify, cp)
go T.examineConn(conn, l.Addr(), verify, cp)
}
}

func (T *L2L) examineConn(conn net.Conn, verify *func(net.Conn) bool, cp *vconnpool.ConnPool) {
func (T *L2L) examineConn(conn net.Conn, addr net.Addr, verify *func(net.Conn) bool, cp *vconnpool.ConnPool) {
// 连接最大限制,正在使用+池中空闲
if cp.MaxConn != 0 && T.currUseConns()+cp.ConnNum() >= cp.MaxConn {
// T.logf("%s 池中数量达到最大 %s 连接不能入池", conn.LocalAddr().String(), conn.RemoteAddr().String())
Expand All @@ -245,7 +245,7 @@ func (T *L2L) examineConn(conn net.Conn, verify *func(net.Conn) bool, cp *vconnp
return
}

if err := cp.Put(conn, conn.LocalAddr()); err != nil {
if err := cp.Put(conn, addr); err != nil {
// 池中受最大连接限制,无法加入池中。
// T.logf("%s 连接加入 %s 池中读取连接错误: %s", conn.RemoteAddr().String(), conn.LocalAddr().String(), err)
conn.Close()
Expand Down

0 comments on commit f85b467

Please sign in to comment.