Skip to content

Commit

Permalink
update: multi threading mode update, supported -t and -n to specific …
Browse files Browse the repository at this point in the history
…threading
  • Loading branch information
Esonhugh committed Apr 15, 2024
1 parent 1101e39 commit f601f2f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions cmd/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var AllCmd = &cobra.Command{
log.Warnf("ParseStringToIPNet failed: %v", err)
return
}
if command.Opts.BatchMode {
RunBatch(ipNets)
if command.Opts.MultiThreadingMode {
RunMultiThread(ipNets, command.Opts.ThreadingNum)
} else {
Run(ipNets)
}
Expand All @@ -55,8 +55,8 @@ func Run(net *net.IPNet) {
printResult(records)
}

func RunBatch(net *net.IPNet) {
scan := mutli.ScanAll(net)
func RunMultiThread(net *net.IPNet, count int) {
scan := mutli.ScanAll(net, count)
var finalRecord []define.Record
for r := range scan {
finalRecord = append(finalRecord, r...)
Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var Opts = struct {
OutputFile string
Verbose string

BatchMode bool
MultiThreadingMode bool
ThreadingNum int
}{}

func init() {
Expand All @@ -29,7 +30,8 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&Opts.Zone, "zone", "z", "cluster.local", "zone")
RootCmd.PersistentFlags().StringVarP(&Opts.OutputFile, "output-file", "o", "", "output file")
RootCmd.PersistentFlags().StringVarP(&Opts.Verbose, "verbose", "v", "info", "log level (debug,info,trace,warn,error,fatal,panic)")
RootCmd.PersistentFlags().BoolVarP(&Opts.BatchMode, "batch-mode", "b", false, "batch mode")
RootCmd.PersistentFlags().BoolVarP(&Opts.MultiThreadingMode, "thread", "t", false, "multi threading mode, work pair with -n")
RootCmd.PersistentFlags().IntVarP(&Opts.ThreadingNum, "thread-num", "n", 16, "threading num, default 16")
}

var RootCmd = &cobra.Command{
Expand Down
8 changes: 4 additions & 4 deletions cmd/subnet/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ var SubNetCmd = &cobra.Command{
log.Warnf("ParseStringToIPNet failed: %v", err)
return
}
if command.Opts.BatchMode {
BatchRun(ipNets)
if command.Opts.MultiThreadingMode {
RunMultiThread(ipNets, command.Opts.ThreadingNum)
} else {
Run(ipNets)
}
Expand All @@ -47,8 +47,8 @@ func Run(net *net.IPNet) {
printResult(records)
}

func BatchRun(net *net.IPNet) {
scan := mutli.NewSubnetScanner()
func RunMultiThread(net *net.IPNet, num int) {
scan := mutli.NewSubnetScanner(num)
var finalRecord []define.Record
for r := range scan.ScanSubnet(net) {
finalRecord = append(finalRecord, r...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/mutli/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/esonhugh/k8spider/define"
)

func ScanAll(subnet *net.IPNet) (result <-chan []define.Record) {
subs := NewSubnetScanner()
func ScanAll(subnet *net.IPNet, num int) (result <-chan []define.Record) {
subs := NewSubnetScanner(num)
result = ScanServiceWithChan(subs.ScanSubnet(subnet))
return result
}
21 changes: 16 additions & 5 deletions pkg/mutli/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ import (
)

type SubnetScanner struct {
wg *sync.WaitGroup
wg *sync.WaitGroup
count int
}

func NewSubnetScanner() *SubnetScanner {
return &SubnetScanner{
wg: new(sync.WaitGroup),
func NewSubnetScanner(threading ...int) *SubnetScanner {
if len(threading) == 0 {
return &SubnetScanner{
wg: new(sync.WaitGroup),
}
} else {
return &SubnetScanner{
wg: new(sync.WaitGroup),
count: threading[0],
}
}
}

Expand All @@ -29,9 +37,12 @@ func (s *SubnetScanner) ScanSubnet(subnet *net.IPNet) <-chan []define.Record {
out := make(chan []define.Record, 100)
go func() {
log.Debugf("splitting subnet into 16 pices")
if subnets, err := pkg.SubnetShift(subnet, 4); err != nil {
// if subnets, err := pkg.SubnetShift(subnet, 4); err != nil {
if subnets, err := pkg.SubnetInto(subnet, s.count); err != nil {
log.Errorf("Subnet split into %v failed, fallback to single mode, reason: %v", s.count, err)
go s.scan(subnet, out)
} else {
log.Debugf("Subnet split into %v success", len(subnets))
for _, sn := range subnets {
go s.scan(sn, out)
}
Expand Down

0 comments on commit f601f2f

Please sign in to comment.