當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Network.GetNodeFromAddr方法代碼示例

本文整理匯總了Golang中github.com/cockroachdb/cockroach/gossip/simulation.Network.GetNodeFromAddr方法的典型用法代碼示例。如果您正苦於以下問題:Golang Network.GetNodeFromAddr方法的具體用法?Golang Network.GetNodeFromAddr怎麽用?Golang Network.GetNodeFromAddr使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cockroachdb/cockroach/gossip/simulation.Network的用法示例。


在下文中一共展示了Network.GetNodeFromAddr方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: outputDotFile


//.........這裏部分代碼省略.........
	if err != nil {
		log.Fatalf("unable to create temp file: %s", err)
	}
	defer f.Close()

	// Determine maximum number of incoming connections. Create outgoing
	// edges, keeping track of which are new since last time (added=true).
	outgoingMap := make(edgeMap)
	var maxIncoming int
	// The order the graph file is written influences the arrangement
	// of nodes in the output image, so it makes sense to eliminate
	// randomness here. Unfortunately with graphviz it's fairly hard
	// to get a consistent ordering.
	for _, simNode := range network.Nodes {
		node := simNode.Gossip
		incoming := node.Incoming()
		for _, iAddr := range incoming {
			e := edge{dest: simNode.Addr.String()}
			key := fmt.Sprintf("%s:%s", iAddr.String(), simNode.Addr)
			if _, ok := edgeSet[key]; !ok {
				e.added = true
			}
			delete(edgeSet, key)
			outgoingMap.addEdge(iAddr.String(), e)
		}
		if len(incoming) > maxIncoming {
			maxIncoming = len(incoming)
		}
	}

	// Find all edges which were deleted.
	for key, e := range edgeSet {
		e.added = false
		e.deleted = true
		outgoingMap.addEdge(strings.Split(key, ":")[0], e)
		delete(edgeSet, key)
	}

	f.WriteString("digraph G {\n")
	f.WriteString("node [shape=record];\n")
	for _, simNode := range network.Nodes {
		node := simNode.Gossip
		var incomplete int
		var totalAge int64
		for _, addr := range network.Addrs {
			infoKey := addr.String()
			if infoKey == simNode.Addr.String() {
				continue // skip the node's own info
			}
			if val, err := node.GetInfo(infoKey); err != nil {
				log.Infof("error getting info for key %q: %s", infoKey, err)
				incomplete++
			} else {
				totalAge += int64(cycle) - val.(int64)
			}
		}

		var sentinelAge int64
		if val, err := node.GetInfo(gossip.KeySentinel); err != nil {
			log.Infof("error getting info for sentinel gossip key %q: %s", gossip.KeySentinel, err)
		} else {
			sentinelAge = int64(cycle) - val.(int64)
		}

		var age, nodeColor string
		if incomplete > 0 {
			nodeColor = "color=red,"
			age = fmt.Sprintf("missing %d", incomplete)
		} else {
			age = strconv.FormatFloat(float64(totalAge)/float64(len(network.Nodes)-1), 'f', 2, 64)
		}
		fontSize := minDotFontSize
		if maxIncoming > 0 {
			fontSize = minDotFontSize + int(math.Floor(float64(len(node.Incoming())*
				(maxDotFontSize-minDotFontSize))/float64(maxIncoming)))
		}
		f.WriteString(fmt.Sprintf("\t%s [%sfontsize=%d,label=\"{%s|MH=%d, AA=%s, SA=%d}\"]\n",
			node.Name, nodeColor, fontSize, node.Name, node.MaxHops(), age, sentinelAge))
		outgoing := outgoingMap[simNode.Addr.String()]
		for _, e := range outgoing {
			destSimNode, ok := network.GetNodeFromAddr(e.dest)
			if !ok {
				continue
			}
			dest := destSimNode.Gossip
			style := ""
			if e.added {
				style = " [color=green]"
			} else if e.deleted {
				style = " [color=red,style=dotted]"
			}
			f.WriteString(fmt.Sprintf("\t%s -> %s%s\n", node.Name, dest.Name, style))
			if !e.deleted {
				edgeSet[fmt.Sprintf("%s:%s", simNode.Addr, e.dest)] = e
			}
		}
	}
	f.WriteString("}\n")
	return f.Name()
}
開發者ID:josephwinston,項目名稱:cockroach,代碼行數:101,代碼來源:gossip.go


注:本文中的github.com/cockroachdb/cockroach/gossip/simulation.Network.GetNodeFromAddr方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。