你这个问题不是golang代码的问题,应该是Linux默认配置的问题:
net.DialTimeout这个函数最终其实就是调用linux socket,而linux中任何东西都是文件,同时linux默认允许同时打开的文件数是1024,可以用如下命令查看:
[root@aia-db /home/daik/test/src/scanport]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3757
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3757
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
可以看到open files那一行,默认是1024,所以当你启动远远大于1024个协程时(如你代码中设置的4500),就有可能同时打开超过1024个文件,导致socket链接建立失败,
我测试了下(注:我本地环境你用你的代码每次50051这个端口都打印不出来,所以选择这个端口做测试):
[root@scanport]# go run main.go
{127.0.0.1 25} open
{127.0.0.1 139} open
{127.0.0.1 111} open
{127.0.0.1 445} open
{127.0.0.1 22} open
{127.0.0.1 631} open
{127.0.0.1 9999} open
{127.0.0.1 9998} open
{127.0.0.1 9997} open
I am port 50051, error: dial tcp 127.0.0.1:50051: socket: too many open files
success is 9
fail is 65526
修改方法:
1、降低协程数量,协程数量真不是越多越高效,要根据实际情况
2、修改linux的配置限制,方法如下:
[root@aia-db /home/daik/test/src/scanport]# ulimit -SHn 10000
[root@aia-db /home/daik/test/src/scanport]# ulimit -n
10000
[root@aia-db /home/daik/test/src/scanport]# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 3757
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 10000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 3757
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
然后再测试你的代码应该OK了。
我的测试代码:
package main
import (
"fmt"
"net"
"time"
"sync"
)
type Job struct {
host string
port int
}
type Result struct {
job Job
status bool
}
var success uint32
var fail uint32
var successMux sync.Mutex
var failMux sync.Mutex
var jobs = make(chan Job)
var results = make(chan Result)
func worker(wg *sync.WaitGroup) {
for job := range jobs {
_, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", job.host, job.port), time.Millisecond*5500)
if err != nil {
failMux.Lock()
fail++
failMux.Unlock()
if job.port == 50051 {
fmt.Println("I am port 50051, error:", err.Error())
}
results <- Result{job, false}
} else {
successMux.Lock()
success++
successMux.Unlock()
results <- Result{job, true}
}
}
wg.Done()
}
const host = "127.0.0.1"
func main() {
wg := sync.WaitGroup{}
go func() {
for i := 1; i <= 65535; i++ {
jobs <- Job{host, i}
}
close(jobs)
}()
go func() {
for result := range results {
if result.status {
fmt.Println(result.job, "open")
}
}
}()
for i := 0; i < 4500; i++ {
wg.Add(1)
go worker(&wg)
}
wg.Wait()
fmt.Println("success is ", success)
fmt.Println("fail is ", fail)
}
另外,你文中提到的https://play.golang.org/p/_ZD...,
这个代码跟你的其实没有本质区别,应该也是linux的问题,所以才有在Windows和Linux上执行有不同结果的现象出现。