sequence

This paper mainly studies nacOS-SDK-Go’s PushReceiver

PushReceiver

Nacos – SDK – go – v0.3.2 / clients/naming_client/push_receiver. Go

type PushReceiver struct {
	port        int
	host        string
	hostReactor *HostReactor
}
Copy the code
  • PushReceiver defines the port, host, and hostReactor properties

NewPushRecevier

Nacos – SDK – go – v0.3.2 / clients/naming_client/push_receiver. Go

func NewPushRecevier(hostReactor *HostReactor) *PushReceiver {
	pr := PushReceiver{
		hostReactor: hostReactor,
	}
	go pr.startServer()
	return &pr
}
Copy the code
  • The NewPushRecevier method creates PushReceiver and asynchronously executes pr.startServer()

startServer

Nacos – SDK – go – v0.3.2 / clients/naming_client/push_receiver. Go

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

	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
			log.Println("[INFO] udp server start, port: " + strconv.Itoa(port))
			break
		}

		if! ok && i == 2 { log.Panicf("failed to start udp server after trying 3 times.")
			//os.Exit(1)  //It is weird dangerous to invoke the os.Exit() as a Middleware.
		}
	}

	defer conn.Close()
	for {
		us.handleClient(conn)
	}
}
Copy the code
  • The startServer method randomly executes a port, then us.trylisten (), exits after three unsuccessful attempts, and us.handleclient (conn)

tryListen

Nacos – SDK – go – v0.3.2 / clients/naming_client/push_receiver. Go

func (us *PushReceiver) tryListen() (*net.UDPConn, bool) {
	addr, err := net.ResolveUDPAddr("udp", us.host+":"+strconv.Itoa(us.port))
	iferr ! = nil { log.Printf("[ERROR]: Can't resolve address,err: %s \n", err.Error())
		return nil, false
	}

	conn, err := net.ListenUDP("udp", addr)
	iferr ! = nil { log.Printf("Error listening %s:%d,err:%s \n", us.host, us.port, err.Error())
		return nil, false
	}

	return conn, true
}
Copy the code
  • The tryListen method executes net.resolveudpaddr and then executes Net.listenudp (“udp”, addr).

handleClient

Nacos – SDK – go – v0.3.2 / clients/naming_client/push_receiver. Go

func (us *PushReceiver) handleClient(conn *net.UDPConn) {
	data := make([]byte, 4024)
	n, remoteAddr, err := conn.ReadFromUDP(data)
	iferr ! = nil { log.Printf("[ERROR]:failed to read UDP msg because of %s \n", err.Error())
		return
	}

	s := utils.TryDecompressData(data[:n])
	log.Println("[INFO] receive push: "+s+" from: ", remoteAddr)

	var pushData PushData
	err1 := json.Unmarshal([]byte(s), &pushData)
	iferr1 ! = nil { log.Printf("[ERROR] failed to process push data.err:%s \n", err1.Error())
		return
	}
	ack := make(map[string]string)

	if pushData.PushType == "dom" || pushData.PushType == "service" {
		us.hostReactor.ProcessServiceJson(pushData.Data)

		ack["type"] = "push-ack"
		ack["lastRefTime"] = strconv.FormatInt(pushData.LastRefTime, 10)
		ack["data"] = ""

	} else if pushData.PushType == "dump" {
		ack["type"] = "dump-ack"
		ack["lastRefTime"] = strconv.FormatInt(pushData.LastRefTime, 10)
		ack["data"] = utils.ToJsonString(us.hostReactor.serviceInfoMap)
	} else {
		ack["type"] = "unknow-ack"
		ack["lastRefTime"] = strconv.FormatInt(pushData.LastRefTime, 10)
		ack["data"] = ""
	}

	bs, _ := json.Marshal(ack)
	conn.WriteToUDP(bs, remoteAddr)
}
Copy the code
  • The handleClient method accepts the data via conn.readfromudp (data) and decompresses it via utils.tryDecompressData, Unmarshal([]byte(s), &pushData); / / Conn. WriteToUDP(bs, remoteAddr)

summary

PushReceiver defines the port, host, and hostReactor attributes. It provides NewPushRecevier, startServer, handleClient, and other methods

doc

  • push_receiver