| 321 | } |
| 322 | |
| 323 | func (r *Runner) RunWithCheck(ctx context.Context) { |
| 324 | // 仅check, 类似httpx |
| 325 | var err error |
| 326 | r.Pools, err = ants.NewPoolWithFunc(1, func(i interface{}) { |
| 327 | config := r.PrepareConfig() |
| 328 | |
| 329 | checkPool, err := pool.NewCheckPool(ctx, config) |
| 330 | if err != nil { |
| 331 | logs.Log.Error(err.Error()) |
| 332 | checkPool.Cancel() |
| 333 | r.poolwg.Done() |
| 334 | return |
| 335 | } |
| 336 | |
| 337 | ch := make(chan string) |
| 338 | var tasks []*Task |
| 339 | go func() { |
| 340 | for t := range r.Tasks.tasks { |
| 341 | ch <- t.baseUrl |
| 342 | tasks = append(tasks, t) |
| 343 | } |
| 344 | close(ch) |
| 345 | }() |
| 346 | checkPool.Worder = words.NewWorderWithChan(ch) |
| 347 | checkPool.Worder.Fns = r.Fns |
| 348 | checkPool.Bar = pkg.NewBar("check", r.Count-r.Offset, checkPool.Statistor, r.Progress) |
| 349 | checkPool.Run(ctx, r.Offset, r.Count) |
| 350 | |
| 351 | // 保存 check 模式的统计信息 |
| 352 | checkPool.Statistor.EndTime = time.Now().Unix() |
| 353 | checkPool.Statistor.End = r.Count |
| 354 | checkPool.Statistor.Total = r.Count |
| 355 | checkPool.Statistor.BaseUrl = r.Tasks.Name |
| 356 | checkPool.Statistor.ReqTotal = int32(checkPool.RequestCount()) |
| 357 | checkPool.Statistor.FailedNumber = int32(checkPool.FailedCount()) |
| 358 | if r.Color { |
| 359 | logs.Log.Important(checkPool.Statistor.ColorString()) |
| 360 | } else { |
| 361 | logs.Log.Important(checkPool.Statistor.String()) |
| 362 | } |
| 363 | r.saveStat(checkPool.Statistor.Json()) |
| 364 | r.recordStat(checkPool.Statistor) |
| 365 | |
| 366 | r.poolwg.Done() |
| 367 | }) |
| 368 | |
| 369 | // r.Pools 内部有 ticktock / purgeStaleWorkers 后台 goroutine, |
| 370 | // 必须 Release 才能回收, 否则 SDK 多次 Execute 会泄露. |
| 371 | defer func() { |
| 372 | r.Pools.Release() |
| 373 | r.Pools = nil |
| 374 | }() |
| 375 | |
| 376 | // 缓冲 1: ctx 先取消时主循环走 ctx.Done 分支退出, 这个 goroutine 不能阻塞在 send. |
| 377 | stopCh := make(chan struct{}, 1) |
| 378 | r.poolwg.Add(1) |
| 379 | err = r.Pools.Invoke(struct{}{}) |
| 380 | if err != nil { |