Skip to content

Commit

Permalink
refine code
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 committed May 7, 2021
1 parent a37aa19 commit 6fcf57b
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions clients/naming_client/push_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ func (us *PushReceiver) tryListen() (*net.UDPConn, bool) {
return conn, true
}

func (us *PushReceiver) startServer() {
func (us *PushReceiver) getConn() *net.UDPConn {
var conn *net.UDPConn

for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
port := r.Intn(1000) + 54951
Expand All @@ -82,14 +81,18 @@ func (us *PushReceiver) startServer() {
if ok {
conn = conn1
logger.Infof("udp server start, port: " + strconv.Itoa(port))
break
return conn
}

if !ok && i == 2 {
logger.Errorf("failed to start udp server after trying 3 times.")
}
}
return nil
}

func (us *PushReceiver) startServer() {
conn := us.getConn()
go func() {
defer func() {
if conn != nil {
Expand All @@ -106,22 +109,9 @@ func (us *PushReceiver) handleClient(conn *net.UDPConn) {

if conn == nil {
time.Sleep(time.Second * 5)
for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
port := r.Intn(1000) + 54951
us.port = port
conn1, ok := us.tryListen()

if ok {
conn = conn1
logger.Infof("udp server start, port: " + strconv.Itoa(port))
break
}

if !ok && i == 2 {
logger.Errorf("failed to start udp server")
return
}
conn = us.getConn()
if conn == nil {
return
}
}

Expand Down

0 comments on commit 6fcf57b

Please sign in to comment.