Skip to content

Commit

Permalink
fix: network connectivity speed
Browse files Browse the repository at this point in the history
Signed-off-by: renxiangyu <[email protected]>
  • Loading branch information
renxiangyu committed Jan 8, 2024
1 parent 88a3efe commit 4d8e043
Showing 1 changed file with 57 additions and 29 deletions.
86 changes: 57 additions & 29 deletions pkg/kosmosctl/floater/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"sync"

"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,6 +46,8 @@ type CommandCheckOptions struct {

SrcFloater *Floater
DstFloater *Floater

routinesMaxNum int
}

type PrintCheckData struct {
Expand Down Expand Up @@ -92,6 +95,7 @@ func NewCmdCheck() *cobra.Command {
flags.StringVar(&o.Port, "port", "8889", "Port used by floater.")
flags.IntVarP(&o.PodWaitTime, "pod-wait-time", "w", 30, "Time for wait pod(floater) launch.")
flags.StringVar(&o.Protocol, "protocol", string(TCP), "Protocol for the network problem.")
flags.IntVarP(&o.routinesMaxNum, "routines-max-number", "", 5, "Number of goroutines to use.")

return cmd
}
Expand Down Expand Up @@ -199,60 +203,84 @@ func (o *CommandCheckOptions) Run() error {
func (o *CommandCheckOptions) RunRange(iPodInfos []*FloatInfo, jPodInfos []*FloatInfo) []*PrintCheckData {
var resultData []*PrintCheckData

goroutinePool := make(chan int, o.routinesMaxNum)
waitGroup := sync.WaitGroup{}

if len(iPodInfos) > 0 && len(jPodInfos) > 0 {
for _, iPodInfo := range iPodInfos {
for _, jPodInfo := range jPodInfos {
for _, ip := range jPodInfo.PodIPs {
var targetIP string
var err error
var cmdResult *command.Result
if o.DstFloater != nil {
targetIP, err = netmap.NetMap(ip, o.DstFloater.CIDRsMap)
} else {
targetIP = ip
}
if err != nil {
cmdResult = command.ParseError(err)
} else {
// ToDo RunRange && RunNative func support multiple commands, and the code needs to be optimized
cmdObj := &command.Ping{
TargetIP: targetIP,
go func() {
waitGroup.Add(1)
goroutinePool <- 1
var targetIP string
var err error
var cmdResult *command.Result
if o.DstFloater != nil {
targetIP, err = netmap.NetMap(ip, o.DstFloater.CIDRsMap)
} else {
targetIP = ip
}

Check failure on line 223 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

SA2000: should call waitGroup.Add(1) before starting the goroutine to avoid a race (staticcheck)
if err != nil {
cmdResult = command.ParseError(err)
} else {
// ToDo RunRange && RunNative func support multiple commands, and the code needs to be optimized
cmdObj := &command.Ping{
TargetIP: targetIP,

Check failure on line 229 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable ip captured by func literal (govet)
}
cmdResult = o.SrcFloater.CommandExec(iPodInfo, cmdObj)

Check failure on line 231 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable ip captured by func literal (govet)
}
cmdResult = o.SrcFloater.CommandExec(iPodInfo, cmdObj)
}
resultData = append(resultData, &PrintCheckData{
*cmdResult,
iPodInfo.NodeName, jPodInfo.NodeName, targetIP,
})
resultData = append(resultData, &PrintCheckData{
*cmdResult,
iPodInfo.NodeName, jPodInfo.NodeName, targetIP,
})
defer func() {
<-goroutinePool
waitGroup.Done()
}()

Check failure on line 240 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable iPodInfo captured by func literal (govet)
}()
}
}
}

Check failure on line 244 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable iPodInfo captured by func literal (govet)
}

waitGroup.Wait()
return resultData
}

func (o *CommandCheckOptions) RunNative(iNodeInfos []*FloatInfo, jNodeInfos []*FloatInfo) []*PrintCheckData {
var resultData []*PrintCheckData

goroutinePool := make(chan int, o.routinesMaxNum)
waitGroup := sync.WaitGroup{}

if len(iNodeInfos) > 0 && len(jNodeInfos) > 0 {
for _, iNodeInfo := range iNodeInfos {
for _, jNodeInfo := range jNodeInfos {
for _, ip := range jNodeInfo.NodeIPs {
// ToDo RunRange && RunNative func support multiple commands, and the code needs to be optimized
cmdObj := &command.Ping{
TargetIP: ip,
}
cmdResult := o.SrcFloater.CommandExec(iNodeInfo, cmdObj)
resultData = append(resultData, &PrintCheckData{
*cmdResult,
iNodeInfo.NodeName, jNodeInfo.NodeName, ip,
})
go func() {
waitGroup.Add(1)
goroutinePool <- 1
// ToDo RunRange && RunNative func support multiple commands, and the code needs to be optimized
cmdObj := &command.Ping{
TargetIP: ip,
}
cmdResult := o.SrcFloater.CommandExec(iNodeInfo, cmdObj)
resultData = append(resultData, &PrintCheckData{
*cmdResult,
iNodeInfo.NodeName, jNodeInfo.NodeName, ip,

Check failure on line 271 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

SA2000: should call waitGroup.Add(1) before starting the goroutine to avoid a race (staticcheck)
})
defer func() {
<-goroutinePool
waitGroup.Done()

Check failure on line 275 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable ip captured by func literal (govet)
}()
}()

Check failure on line 277 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable iNodeInfo captured by func literal (govet)
}
}
}

Check failure on line 280 in pkg/kosmosctl/floater/check.go

View workflow job for this annotation

GitHub Actions / lint

loopclosure: loop variable iNodeInfo captured by func literal (govet)
}

waitGroup.Wait()
return resultData
}

Expand Down

0 comments on commit 4d8e043

Please sign in to comment.