原创 聊聊nacos-coredns-plugin的UDPServer

发布时间:2021-06-24 12:11:47 浏览 449 来源:猿笔记 作者:go4it

    86


    ##序

    本文主要研究一下nacos-coredns-plugin的UDPServer

    ##UDPServer

    nacos-coredns-plugin/nacos/udp_server.gotypeUDPServerstruct{

    \tportint

    \thoststring

    \tvipClient*NacosClient

    }>UDPServer定义了port、host、vipClient属性

    ##StartServer

    nacos-coredns-plugin/nacos/udp_server.gofunc(us*UDPServer)StartServer(){

    \tvarconn*net.UDPConn

    \tfori:=0;i<3;i++{

    \t\tr:=rand.New(rand.NewSource(time.Now().UnixNano()))

    \t\tport:=r.Intn(1000)+54951

    \t\tus.port=port

    \t\tconn1,ok:=us.tryListen()

    \t\tifok{

    \t\t\tconn=conn1

    \t\t\tNacosClientLogger.Info("udpserverstart,port:"+strconv.Itoa(port))

    \t\t\tbreak

    \t\t}

    \t\tif!ok&&i==2{

    \t\t\tNacosClientLogger.Critical("failedtostartudpserveraftertrying3times.")

    \t\t\tos.Exit(1)

    \t\t}

    \t}

    \tUDP_Port=us.port

    \tdeferconn.Close()

    \tfor{

    \t\tus.handleClient(conn)

    \t}

    }>StartServer方法循环3次执行tryListen(),若都没有成功则exit;若成功则for循环执行handleClient

    ##tryListen

    nacos-coredns-plugin/nacos/udp_server.gofunc(us*UDPServer)tryListen()(*net.UDPConn,bool){

    \taddr,err:=net.ResolveUDPAddr("udp",us.host+":"+strconv.Itoa(us.port))

    \tiferr!=nil{

    \t\tNacosClientLogger.Error("Can'tresolveaddress:",err)

    \t\treturnnil,false

    \t}

    \tconn,err:=net.ListenUDP("udp",addr)

    \tiferr!=nil{

    \t\tNacosClientLogger.Error("Errorlistening:",err)

    \t\treturnnil,false

    \t}

    \treturnconn,true

    }>tryListen方法先执行net.ResolveUDPAddr,在执行net.ListenUDP

    ##handleClient

    nacos-coredns-plugin/nacos/udp_server.gotypePushDatastruct{

    \tPushTypestring`json:"type"`

    \tDatastring`json:"data"`

    \tLastRefTimeint64`json:"lastRefTime"`

    \t

    }

    func(us*UDPServer)handleClient(conn*net.UDPConn){

    \tdata:=make([]byte,4024)

    \tn,remoteAddr,err:=conn.ReadFromUDP(data)

    \tiferr!=nil{

    \t\tNacosClientLogger.Error("failedtoreadUDPmsgbecauseof",err)

    \t\treturn

    \t}

    \ts:=TryDecompressData(data[:n])

    \tNacosClientLogger.Info("receivepush:"+s+"from:",remoteAddr)

    \tvarpushDataPushData

    \terr1:=json.Unmarshal([]byte(s),&pushData)

    \tiferr1!=nil{

    \t\tNacosClientLogger.Warn("failedtoprocesspushdata,",err1)

    \t\treturn

    \t}

    \tdomain,err1:=ProcessDomainString(pushData.Data)

    \tNacosClientLogger.Info("receivedomain:",domain)

    \tiferr1!=nil{

    \t\tNacosClientLogger.Warn("failedtoprocesspushdata:"+s,err1)

    \t}

    \tkey:=GetCacheKey(domain.Name,LocalIP())

    \tus.vipClient.domainMap.Set(key,domain)

    \tack:=make(map[string]string)

    \tack["type"]="push-ack"

    \tack["lastRefTime"]=strconv.FormatInt(pushData.LastRefTime,10)

    \tack["data"]=""

    \tbs,_:=json.Marshal(ack)

    \tconn.WriteToUDP(bs,remoteAddr)

    }>handleClient方法执行conn.ReadFromUDP,然后通过TryDecompressData解压数据,然后Unmarshal为PushData类型,执行ProcessDomainString获取domain,执行us.vipClient.domainMap.Set(key,domain),最后构造返回数据ack回去

    ##小结

    nacos-coredns-plugin的UDPServer定义了port、host、vipClient属性;它提供了StartServer方法;StartServer方法循环3次执行tryListen(),若都没有成功则exit;若成功则for循环执行handleClient。

    ##doc

    -[nacos-coredns-plugin](

作者信息

go4it [等级:3]
发布了 1243 篇专栏 · 获得点赞 844 · 获得阅读 561230

相关推荐 更多