1
0
mirror of https://github.com/fumiama/WireGold.git synced 2026-06-18 00:40:25 +08:00

feat(tcp): add config option dialtimeout

This commit is contained in:
源文雨
2024-07-16 22:16:01 +09:00
parent 739cf863f1
commit 5d04567ec9
3 changed files with 29 additions and 5 deletions

View File

@@ -19,6 +19,9 @@ func (l *Link) keepAlive(dur int64) {
logrus.Infoln("[nat] start to keep alive") logrus.Infoln("[nat] start to keep alive")
t := time.NewTicker(time.Second * time.Duration(dur)) t := time.NewTicker(time.Second * time.Duration(dur))
for range t.C { for range t.C {
if l.status == LINK_STATUS_DOWN || l.me.loop == nil {
return
}
n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, nil), false) n, err := l.WriteAndPut(head.NewPacket(head.ProtoHello, l.me.srcport, l.peerip, l.me.dstport, nil), false)
if err == nil { if err == nil {
logrus.Infoln("[nat] send", n, "bytes keep alive packet") logrus.Infoln("[nat] send", n, "bytes keep alive packet")
@@ -78,6 +81,11 @@ func (l *Link) onQuery(packet []byte) {
return return
} }
if l == nil || l.me == nil {
logrus.Errorln("[nat] nil link/me")
return
}
// 2. notify分发 // 2. notify分发
// ---- 封装 Notify 到 新的 packet // ---- 封装 Notify 到 新的 packet
// ---- 调用 l.Send 发送到对方 // ---- 调用 l.Send 发送到对方

View File

@@ -9,6 +9,7 @@ import (
) )
type Config struct { type Config struct {
DialTimeout time.Duration
PeersTimeout time.Duration PeersTimeout time.Duration
ReceiveChannelSize int ReceiveChannelSize int
} }
@@ -28,6 +29,7 @@ func newEndpoint(endpoint string, configs ...any) *EndPoint {
addr: net.TCPAddrFromAddrPort( addr: net.TCPAddrFromAddrPort(
netip.MustParseAddrPort(endpoint), netip.MustParseAddrPort(endpoint),
), ),
dialtimeout: cfg.DialTimeout,
peerstimeout: cfg.PeersTimeout, peerstimeout: cfg.PeersTimeout,
recvchansize: cfg.ReceiveChannelSize, recvchansize: cfg.ReceiveChannelSize,
} }

View File

@@ -5,6 +5,7 @@ import (
"io" "io"
"math/rand" "math/rand"
"net" "net"
"reflect"
"strconv" "strconv"
"time" "time"
@@ -16,6 +17,7 @@ import (
type EndPoint struct { type EndPoint struct {
addr *net.TCPAddr addr *net.TCPAddr
dialtimeout time.Duration
peerstimeout time.Duration peerstimeout time.Duration
recvchansize int recvchansize int
} }
@@ -45,9 +47,9 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
return nil, err return nil, err
} }
ep.addr = lstn.Addr().(*net.TCPAddr) ep.addr = lstn.Addr().(*net.TCPAddr)
timeout := ep.peerstimeout peerstimeout := ep.peerstimeout
if timeout < time.Second { if peerstimeout < time.Second {
timeout = time.Second peerstimeout = time.Second
} }
chansz := ep.recvchansize chansz := ep.recvchansize
if chansz < 32 { if chansz < 32 {
@@ -56,7 +58,7 @@ func (ep *EndPoint) Listen() (p2p.Conn, error) {
conn := &Conn{ conn := &Conn{
addr: ep, addr: ep,
lstn: lstn, lstn: lstn,
peers: ttl.NewCacheOn(timeout, [4]func(string, *net.TCPConn){ peers: ttl.NewCacheOn(peerstimeout, [4]func(string, *net.TCPConn){
nil, nil,
nil, nil,
func(s string, t *net.TCPConn) { func(s string, t *net.TCPConn) {
@@ -122,6 +124,7 @@ func (conn *Conn) accept() {
continue continue
} }
ep := newEndpoint(tcpconn.RemoteAddr().String(), &Config{ ep := newEndpoint(tcpconn.RemoteAddr().String(), &Config{
DialTimeout: conn.addr.dialtimeout,
PeersTimeout: conn.addr.peerstimeout, PeersTimeout: conn.addr.peerstimeout,
ReceiveChannelSize: conn.addr.recvchansize, ReceiveChannelSize: conn.addr.recvchansize,
}) })
@@ -203,11 +206,22 @@ func (conn *Conn) WriteToPeer(b []byte, ep p2p.EndPoint) (n int, err error) {
} }
tcpconn := conn.peers.Get(tcpep.String()) tcpconn := conn.peers.Get(tcpep.String())
if tcpconn == nil { if tcpconn == nil {
dialtimeout := tcpep.dialtimeout
if dialtimeout < time.Second {
dialtimeout = time.Second
}
logrus.Infoln("[tcp] dial to", tcpep.addr, "timeout", dialtimeout)
var cn net.Conn
// must use another port to send because there's no exsiting conn // must use another port to send because there's no exsiting conn
tcpconn, err = net.DialTCP(tcpep.Network(), nil, tcpep.addr) cn, err = net.DialTimeout(tcpep.Network(), tcpep.addr.String(), dialtimeout)
if err != nil { if err != nil {
return return
} }
tcpconn, ok = cn.(*net.TCPConn)
if !ok {
return 0, errors.New("expect *net.TCPConn but got " + reflect.ValueOf(cn).Type().String())
}
logrus.Infoln("[tcp] dial to", tcpep.addr, "success, local:", tcpconn.LocalAddr())
conn.peers.Set(tcpep.String(), tcpconn) conn.peers.Set(tcpep.String(), tcpconn)
} }
cnt, err := io.Copy(tcpconn, &packet{ cnt, err := io.Copy(tcpconn, &packet{