Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt at threading #11

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/checknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,9 @@ func CheckNode(nodeAddr string) {
unsuccessfulNodes.Unlock()
return
}
for _, peer := range netinfo.Result.Peers {
peer := peer
ProcessPeer(&peer)
}

ProcessPeers(netinfo.Result.Peers, 69)

}

func CheckNodeGRPC(nodeAddr string) {
Expand Down
47 changes: 36 additions & 11 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

var client = &http.Client{
Timeout: 3000 * time.Millisecond,
Timeout: 500 * time.Millisecond,
Transport: &http.Transport{
MaxIdleConns: 500,
IdleConnTimeout: 30 * time.Second,
Expand Down Expand Up @@ -47,7 +47,7 @@ func FetchStatus(nodeAddr string) (*types.StatusResponse, error) {
return &status, nil
}

func BuildRPCAddress(peer *types.Peer) string {
func BuildRPCAddress(peer types.Peer) string {
rpcAddr := peer.NodeInfo.Other.RPCAddress
rpcAddr = strings.TrimPrefix(rpcAddr, "tcp://")

Expand All @@ -74,27 +74,52 @@ func WriteSectionToToml(file *os.File, sectionName string, nodes map[string]int)
}
}

// Modify the function signature to:
func ProcessPeer(peer *types.Peer) {
func ProcessPeers(peers []types.Peer, workerCount int) {
// Create a buffered channel to manage workload.
jobs := make(chan *types.Peer, len(peers))

// Create a wait group.
var wg sync.WaitGroup

// Spawn worker goroutines.
for i := 0; i < workerCount; i++ {
go func() {
for peer := range jobs {
processSinglePeer(*peer)
wg.Done()
}
}()
}

// Queue jobs.
for _, peer := range peers {
wg.Add(1)
jobs <- &peer
}

// Close the job channel and wait for all jobs to finish.
close(jobs)
wg.Wait()
}

func processSinglePeer(peer types.Peer) {
rpcAddr := BuildRPCAddress(peer)
rpcAddr = NormalizeAddressWithRemoteIP(rpcAddr, peer.RemoteIP)
CheckNode("http://" + rpcAddr)

// Fetch network info
netInfo, err := FetchNetInfo("http://" + rpcAddr)
if err != nil {
// fmt.Println("Error fetching network info:", err)
// fmt.Println("Error fetching network info:", err)
return
}

// Process each peer
for _, peer := range netInfo.Result.Peers {
go func(peer types.Peer) {
if !IsNodeVisited(peer.NodeInfo.Other.RPCAddress) {
MarkNodeAsVisited(peer.NodeInfo.Other.RPCAddress)
ProcessPeer(&peer)
}
}(peer)
if !IsNodeVisited(peer.NodeInfo.Other.RPCAddress) {
MarkNodeAsVisited(peer.NodeInfo.Other.RPCAddress)
ProcessPeers([]types.Peer{peer}, 100) // Adjust workerCount as needed
}
}
}

Expand Down