ProbeIPs runs ProbeIP concurrently. Output order matches input.
(ctx context.Context, ips []string, port uint16, timeout time.Duration, concurrency int, onResult func(IPResult))
| 47 | |
| 48 | // ProbeIPs runs ProbeIP concurrently. Output order matches input. |
| 49 | func ProbeIPs(ctx context.Context, ips []string, port uint16, timeout time.Duration, |
| 50 | concurrency int, onResult func(IPResult)) []IPResult { |
| 51 | if concurrency <= 0 { |
| 52 | concurrency = 16 |
| 53 | } |
| 54 | sem := make(chan struct{}, concurrency) |
| 55 | out := make([]IPResult, len(ips)) |
| 56 | var wg sync.WaitGroup |
| 57 | for i, s := range ips { |
| 58 | addr, err := netip.ParseAddr(s) |
| 59 | if err != nil { |
| 60 | out[i] = IPResult{Err: "parse: " + err.Error()} |
| 61 | continue |
| 62 | } |
| 63 | i, addr := i, addr |
| 64 | wg.Add(1) |
| 65 | sem <- struct{}{} |
| 66 | go func() { |
| 67 | defer wg.Done() |
| 68 | defer func() { <-sem }() |
| 69 | r := ProbeIP(ctx, addr, port, timeout) |
| 70 | out[i] = r |
| 71 | if onResult != nil { |
| 72 | onResult(r) |
| 73 | } |
| 74 | }() |
| 75 | } |
| 76 | wg.Wait() |
| 77 | return out |
| 78 | } |
| 79 | |
| 80 | // RankIPs sorts reachable IPs by RTT ascending. Unreachable are dropped. |
| 81 | func RankIPs(in []IPResult) []IPResult { |
no test coverage detected