github-vet / github-vet/rangeloop-pointer-findings

LSvKing/centipede: centipede/centipede.go; 143 LoC

Open
#9,813 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [LSvKing/centipede](https://www.github.com/LSvKing/centipede) at [centipede/centipede.go](https://github.com/LSvKing/centipede/blob/5ba0be8e08116756e8a43f2235b425749d499a27/centipede/centipede.go#L70-L212)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable crawlerChan used in defer or goroutine at line 76

[Click here to see the code in its original context.](https://github.com/LSvKing/centipede/blob/5ba0be8e08116756e8a43f2235b425749d499a27/centipede/centipede.go#L70-L212)

Click here to show the 143 line(s) of Go which triggered the analyzer.

```go
for crawlerChan := range centipede.CrawlerJob {

Log.Debugln(crawlerChan.Option().Name)

go func() {

crawler := crawlerChan.CrawlerEr

//crawlerWait.Add(1)

go centipede.Pipeline.Run(crawler)

defer func() {
Log.Infoln(crawler.Option().Name, " Finish")
}()

Log.Infoln(crawlerChan.Option().Name + " 开始运行")

mc := resource_manage.NewResourceManageChan(crawler.Option().Thread)

crawler.Parse(crawler.Option().CallParams)

transport := &http.Transport{
DisableKeepAlives: true,
}

//funcMap := make(map[string]reflect.Value, 100)

var limiter *rate.Limiter

if crawler.Option().Limit > 0 {
limiter = rate.NewLimiter(crawler.Option().Limit, int(crawler.Option().Limit))
}

for {
if crawler.Option().Limit > 0 {
limiter.Wait(ctx)
}

if centipede.Scheduler.Count() == 0 && mc.Has() == 0 {
//crawlerWait.Done()
break
}

mc.GetOne()

req, _ := centipede.Scheduler.Poll()

if req == nil {
mc.FreeOne()
continue
}

go func() {

Log.Debug("start spider go")
Log.Debug(req.GetUrl())

defer func() {
mc.FreeOne()
}()

if crawler.Option().Timeout > 0 {
centipede.Downloader.Client.Timeout = crawler.Option().Timeout
}

if crawler.Option().DisableProxy {

if len(crawler.Option().ProxyList) < 1 {
Log.Error("代理池为空")
}

var rawProxy string

if crawler.Option().ProxyFun != nil {
rawProxy = crawler.Option().ProxyFun()
} else {

if len(crawler.Option().ProxyList) > 1 {
//从代理持随机取代理
rawProxy = crawler.Option().ProxyList[rand.Intn(len(crawler.Option().ProxyList))].ProxyURL

} else {
rawProxy = crawler.Option().ProxyList[0].ProxyURL
}
}

Log.Debugln(rawProxy)
proxy, err := url.Parse(rawProxy)

if err != nil {
Log.Error("代理转换格式失败")
}

transport.Proxy = http.ProxyURL(proxy)
centipede.Downloader.Client.Transport = transport
}

if resp, err := centipede.Downloader.Download(req); err != nil {
Log.WithField("type", "downloadReTry").WithField("Request", req).WithError(err).Error("下载重试")

if req.ReTry < 4 {
req.ReTry += 1

centipede.Scheduler.Push(req)
} else {
Log.WithField("type", "downloadError").WithField("Request", req).WithError(err).Error("重试失败")
}

} else {
defer func() {
if p := recover(); p != nil {
Log.WithField("trace", string(debug.Stack())).WithField("Request", req).Fatalf("蜘蛛异常错误 error: %v", p)
}

}()

Log.Debugln("get finsh")

//通过反射执行 回调函数
params := make([]reflect.Value, 0)

//取 resp 参数放入执行参数
params = append(params, reflect.ValueOf(resp))

//如果回调请求设置了 自定义参数 加入自定义参数到执行参数
if req.CallParams != nil {
params = append(params, reflect.ValueOf(req.CallParams))
}

//通过反射执行函数
reflect.ValueOf(crawler).MethodByName(req.Callback).Call(params)
}

Log.Debug("start spider go end")
}()

}
}()

//crawlerWait.Wait()

}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 5ba0be8e08116756e8a43f2235b425749d499a27

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.