tcp-shaker
tcp-shaker copied to clipboard
A problem occurs when coroutines are concurrent
When the number of coroutines is greater than 10000, there will be program errors and insufficient resources How to solve it?
@mythldy Can you provide details of the error and how the library is used?
package main
import ( "context" "fmt" "github.com/tevino/tcp-shaker" "runtime" "strconv" "strings" "sync" "time" )
var wgp sync.WaitGroup
func SynScan(ip string, port string) { c := tcp.NewChecker() host := fmt.Sprintf("%s:%s", ip, port) ctx, stopChecker := context.WithCancel(context.Background()) defer stopChecker() go func() { if err := c.CheckingLoop(ctx); err != nil { fmt.Println("checking loop stopped due to fatal error: ", err) } }()
<-c.WaitReady()
timeout := time.Millisecond * 200
err := c.CheckAddr(host, timeout)
switch err {
case tcp.ErrTimeout:
//fmt.Printf("%s Connect to Google timed out\n", port)
//stopChecker()
case nil:
fmt.Printf("%s Connect to Google succeeded\n", host)
//stopChecker()
default:
//fmt.Printf("%s Error occurred while connecting: %s\n", port, err.Error())
}
}
func Prod(ch chan string) {
for p := 0; p < 20000; p++ {
new_host_info := fmt.Sprintf("%s:%d", "10.10.124.153", p)
ch <- new_host_info
}
close(ch)
wgp.Done()
}
func Cons(ch chan string) {
defer wgp.Done()
//lock.Lock()
//rwlock.RLock()
for data := range ch {
ip := strings.Split(data, ":")[0]
port, _ := strconv.Atoi(strings.Split(data, ":")[1])
SynScan(ip, strconv.Itoa(port))
}
//rwlock.RUnlock()
//lock.Unlock()
}
func main() {
ss := time.Now()
//host := fmt.Sprintf("%s:%s", ip, port)
ch := make(chan string) //设置通道
runtime.GOMAXPROCS(10) //设置最大CPU数量
//debug.SetMaxThreads(20000)
wgp.Add(20000 + 1)
go Prod(ch) //协程池取一个协程
for i := 0; i < 20000; i++ { //开1000个goroutine
go Cons(ch)
}
wgp.Wait()
elapsed := time.Since(ss)
fmt.Println(elapsed)
//s := time.Now()
//for i := 0; i <= 15000; i++ {
// p := strconv.Itoa(i)
// SynScan("10.10.124.153", p)
//}
//elapsed := time.Since(s)
//fmt.Println("app elapsed:", elapsed.Seconds())
}
补充一部分报错信息 runtime/cgo: pthread_create failed: Resource temporarily unavailable SIGABRT: abort PC=0xffffe424
@mythldy The code called NewChecker 20,000 times.
The expected usage is to call it once and share the instance among goroutines.
https://github.com/tevino/tcp-shaker/commit/8d02d3d6396536cf2f04399084aa0f49150228c6