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

feat: parallel peer analysis #85

Merged
merged 1 commit into from
Oct 2, 2023
Merged
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
80 changes: 41 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/gdamore/tcell/v2"
Expand Down Expand Up @@ -1080,55 +1081,56 @@ func getPeerText(ctx context.Context) string {
sb.WriteString(fmt.Sprintf("%"+strconv.Itoa(printStart-1)+"s [blue]%"+strconv.Itoa(peerCount)+"s[white]/[green]%d[white]\n",
" ", "0", peerCount,
))
//var index int
var lastpeerIP string
// counters, etc.
var peerRTT int
var wg sync.WaitGroup
for _, v := range peersFiltered {
peerArr := strings.Split(v, ";")
peerIP := peerArr[0]
peerPORT := peerArr[1]
peerDIR := peerArr[2]
// increment waitgroup counter
wg.Add(1)
// Avoid re-use of v in all go-routines
// https://go.dev/doc/faq#closures_and_goroutines
v := v

go func() {
defer wg.Done()
peerArr := strings.Split(v, ";")
peerIP := peerArr[0]
peerPORT := peerArr[1]
peerDIR := peerArr[2]

// TODO: geolocation

if peerIP == lastpeerIP && peerRTT != 99999 {
peerStats.RTTSUM = peerStats.RTTSUM + peerRTT // skip RTT check and reuse old peerRTT
} else {
// Start RTT loop
// for tool in ... return peerRTT
sb.WriteString(fmt.Sprintf(" Getting RTT for: %s:%s\n", peerIP, peerPORT))
peerRTT = tcpinfoRtt(fmt.Sprintf("%s:%s", peerIP, peerPORT))
if peerRTT != 99999 {
peerStats.RTTSUM = peerStats.RTTSUM + peerRTT
}

}
lastpeerIP = peerIP
// Update counters
if peerRTT < 50 {
peerStats.CNT1 = peerStats.CNT1 + 1
} else if peerRTT < 100 {
peerStats.CNT2 = peerStats.CNT2 + 1
} else if peerRTT < 200 {
peerStats.CNT3 = peerStats.CNT3 + 1
} else if peerRTT < 99999 {
peerStats.CNT4 = peerStats.CNT4 + 1
} else {
peerStats.CNT0 = peerStats.CNT0 + 1
}
peerPort, err := strconv.Atoi(peerPORT)
if err != nil {
return fmt.Sprintf(" [red]%s[white]", "Unable to convert port to string!")
}
peerLocation := getGeoIP(ctx, peerIP)
peerStats.RTTresults = append(peerStats.RTTresults, Peer{
IP: peerIP,
Port: peerPort,
Direction: peerDIR,
RTT: peerRTT,
Location: peerLocation,
})
// Update counters
if peerRTT < 50 {
peerStats.CNT1 = peerStats.CNT1 + 1
} else if peerRTT < 100 {
peerStats.CNT2 = peerStats.CNT2 + 1
} else if peerRTT < 200 {
peerStats.CNT3 = peerStats.CNT3 + 1
} else if peerRTT < 99999 {
peerStats.CNT4 = peerStats.CNT4 + 1
} else {
peerStats.CNT0 = peerStats.CNT0 + 1
}
peerPort, err := strconv.Atoi(peerPORT)
if err != nil {
peerPort = 0
}
peerLocation := getGeoIP(ctx, peerIP)
peerStats.RTTresults = append(peerStats.RTTresults, Peer{
IP: peerIP,
Port: peerPort,
Direction: peerDIR,
RTT: peerRTT,
Location: peerLocation,
})
}()
wg.Wait()
sort.SliceStable(peerStats.RTTresults, func(i, j int) bool {
return peerStats.RTTresults[i].RTT < peerStats.RTTresults[j].RTT
})
Expand Down Expand Up @@ -1251,7 +1253,7 @@ func getPeerText(ctx context.Context) string {
// Divider
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width-1)))

sb.WriteString(fmt.Sprintf("[blue] # %24s I/O RTT Geolocation[white] ([green]Coming soon![white])\n", "REMOTE PEER"))
sb.WriteString(fmt.Sprintf("[blue] # %24s I/O RTT Geolocation[white]\n", "REMOTE PEER"))
peerNbrStart := 1
// peerLocationWidth := width - 41
for peerNbr, peer := range peerStats.RTTresults {
Expand Down
Loading