`package main
import (
"fmt"
"net"
"sync"
"time"
"github.com/panjf2000/ants/v2"
)
func doPortScan(i interface{}) {
port := i.(int32)
_, err := net.DialTimeout("tcp", fmt.Sprintf("192.168.20.1:%d", port), time.Second*1)
if err != nil {
return
}
fmt.Printf("Port %d is open\n", port)
}
func main() {
defer ants.Release()
runTimes := 65535
// Use the common pool.
var wg sync.WaitGroup
// Use the pool with a method,
// set 10 to the capacity of goroutine pool and 1 second for expired duration.
p, _ := ants.NewPoolWithFunc(300, func(i interface{}) {
doPortScan(i)
wg.Done()
})
defer p.Release()
// Submit tasks one by one.
for i := 0; i < runTimes; i++ {
wg.Add(1)
_ = p.Invoke(int32(i))
}
wg.Wait()
fmt.Printf("running goroutines: %d\n", p.Running())
}
崩溃时的现象如下:
goroutine 543 [runnable]:
github.com/panjf2000/ants/v2.(*goWorkerWithFunc).run.func1(0xc000255e00)
/Users/mac/go/src/github.com/panjf2000/ants/worker_func.go:65 +0x85
created by github.com/panjf2000/ants/v2.(*goWorkerWithFunc).run
/Users/mac/go/src/github.com/panjf2000/ants/worker_func.go:49 +0x4c
goroutine 544 [runnable]:
syscall.syscall(0x108b820, 0x25, 0xc00048800c, 0x10, 0xffffffffffffffff, 0xffffffffffffffff, 0x24)
/usr/local/go/src/runtime/sys_darwin.go:63 +0x2e
syscall.connect(0x25, 0xc00048800c, 0x10, 0x0, 0x0)
/usr/local/go/src/syscall/zsyscall_darwin_amd64.go:91 +0x5c
syscall.Connect(0x25, 0x1158f40, 0xc000488000, 0x111ac80, 0x1)
/usr/local/go/src/syscall/syscall_unix.go:251 +0x62
net.(*netFD).connect(0xc0000e2180, 0x115a280, 0xc0003ff200, 0x0, 0x0, 0x1158f40, 0xc000488000, 0x0, 0x0, 0x0, ...)
/usr/local/go/src/net/fd_unix.go:70 +0x7b
net.(*netFD).dial(0xc0000e2180, 0x115a280, 0xc0003ff200, 0x115a7a0, 0x0, 0x115a7a0, 0xc000255e90, 0x0, 0x1, 0xc000061890)
/usr/local/go/src/net/sock_posix.go:149 +0xff
net.socket(0x115a280, 0xc0003ff200, 0x1133ff4, 0x3, 0x2, 0x1, 0x0, 0x0, 0x115a7a0, 0x0, ...)
/usr/local/go/src/net/sock_posix.go:70 +0x1c0
net.internetSocket(0x115a280, 0xc0003ff200, 0x1133ff4, 0x3, 0x115a7a0, 0x0, 0x115a7a0, 0xc000255e90, 0x1, 0x0, ...)
/usr/local/go/src/net/ipsock_posix.go:141 +0x141
net.(*sysDialer).doDialTCP(0xc0000e2100, 0x115a280, 0xc0003ff200, 0x0, 0xc000255e90, 0x110a680, 0x123ab20, 0x0)
/usr/local/go/src/net/tcpsock_posix.go:65 +0xc2
net.(*sysDialer).dialTCP(0xc0000e2100, 0x115a280, 0xc0003ff200, 0x0, 0xc000255e90, 0xc034fda0d3, 0x121ffc0, 0xc000061ad0)
/usr/local/go/src/net/tcpsock_posix.go:61 +0xd7
net.(*sysDialer).dialSingle(0xc0000e2100, 0x115a280, 0xc0003ff200, 0x11593c0, 0xc000255e90, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/net/dial.go:578 +0x36e
net.(*sysDialer).dialSerial(0xc0000e2100, 0x115a280, 0xc0003ff200, 0xc0000b38a0, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/net/dial.go:546 +0x221
net.(*Dialer).DialContext(0xc000061e40, 0x115a240, 0xc0000ce008, 0x1133ff4, 0x3, 0xc0000ce6f0, 0x10, 0x0, 0x0, 0x0, ...)
/usr/local/go/src/net/dial.go:424 +0x666
net.(*Dialer).Dial(...)
/usr/local/go/src/net/dial.go:347
net.DialTimeout(0x1133ff4, 0x3, 0xc0000ce6f0, 0x10, 0x3b9aca00, 0xc0000ce6f0, 0x10, 0x0, 0x0)
/usr/local/go/src/net/dial.go:333 +0xb5
main.doPortScan(0x10fe540, 0xc0000ce6d4)
/Users/mac/go/src/github.com/panjf2000/ants/examples/main.go:42 +0xd8
main.main.func1(0x10fe540, 0xc0000ce6d4)
/Users/mac/go/src/github.com/panjf2000/ants/examples/main.go:90 +0x3e
github.com/panjf2000/ants/v2.(*goWorkerWithFunc).run.func1(0xc000255e30)
/Users/mac/go/src/github.com/panjf2000/ants/worker_func.go:69 +0xbd
created by github.com/panjf2000/ants/v2.(*goWorkerWithFunc).run
/Users/mac/go/src/github.com/panjf2000/ants/worker_func.go:49 +0x4c
exit status 2`