Skip to content

Commit

Permalink
feat: SUDP alway reconnect and print too much log when no data ready (f…
Browse files Browse the repository at this point in the history
…atedier#2844)

* feat: random sleep duration before reconnecting

* fix: bug
  • Loading branch information
bingtianbaihua authored Mar 17, 2022
1 parent 63efa6b commit 452fe25
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions client/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,39 +377,44 @@ func (sv *SUDPVisitor) Run() (err error) {
func (sv *SUDPVisitor) dispatcher() {
xl := xlog.FromContextSafe(sv.ctx)

var (
visitorConn net.Conn
err error

firstPacket *msg.UDPPacket
)

for {
// loop for get frpc to frps tcp conn
// setup worker
// wait worker to finished
// retry or exit
visitorConn, err := sv.getNewVisitorConn()
if err != nil {
// check if proxy is closed
// if checkCloseCh is close, we will return, other case we will continue to reconnect
select {
case <-sv.checkCloseCh:
select {
case firstPacket = <-sv.sendCh:
if firstPacket == nil {
xl.Info("frpc sudp visitor proxy is closed")
return
default:
}
case <-sv.checkCloseCh:
xl.Info("frpc sudp visitor proxy is closed")
return
}

time.Sleep(3 * time.Second)

visitorConn, err = sv.getNewVisitorConn()
if err != nil {
xl.Warn("newVisitorConn to frps error: %v, try to reconnect", err)
continue
}

sv.worker(visitorConn)
// visitorConn always be closed when worker done.
sv.worker(visitorConn, firstPacket)

select {
case <-sv.checkCloseCh:
return
default:
}
}

}

func (sv *SUDPVisitor) worker(workConn net.Conn) {
func (sv *SUDPVisitor) worker(workConn net.Conn, firstPacket *msg.UDPPacket) {
xl := xlog.FromContextSafe(sv.ctx)
xl.Debug("starting sudp proxy worker")

Expand Down Expand Up @@ -463,6 +468,14 @@ func (sv *SUDPVisitor) worker(workConn net.Conn) {
}()

var errRet error
if firstPacket != nil {
if errRet = msg.WriteMsg(conn, firstPacket); errRet != nil {
xl.Warn("sender goroutine for udp work connection closed: %v", errRet)
return
}
xl.Trace("send udp package to workConn: %s", firstPacket.Content)
}

for {
select {
case udpMsg, ok := <-sv.sendCh:
Expand Down

0 comments on commit 452fe25

Please sign in to comment.