mirror of
https://github.com/taigrr/go-fastping
synced 2025-01-18 05:03:15 -08:00
Add NumGoroutines parameter to Pinger struct
This adds NumGoroutines parameter to Pinger struct to control how many goroutines should be used to send and receive packets. This parameter's default is runtime.NumCPU().
This commit is contained in:
parent
022e2c3afe
commit
496f96870c
21
fastping.go
21
fastping.go
@ -106,10 +106,6 @@ func ipv4Payload(b []byte) []byte {
|
|||||||
return b[hdrlen:]
|
return b[hdrlen:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func numGoRoutines() int {
|
|
||||||
return runtime.NumCPU() * 4
|
|
||||||
}
|
|
||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
stop chan bool
|
stop chan bool
|
||||||
done chan bool
|
done chan bool
|
||||||
@ -149,6 +145,10 @@ type Pinger struct {
|
|||||||
OnRecv func(*net.IPAddr, time.Duration)
|
OnRecv func(*net.IPAddr, time.Duration)
|
||||||
// OnIdle is called when MaxRTT time passed
|
// OnIdle is called when MaxRTT time passed
|
||||||
OnIdle func()
|
OnIdle func()
|
||||||
|
// NumGoroutines defines how many goroutines are used when sending ICMP
|
||||||
|
// packets and receiving IPv4/IPv6 ICMP responses. Its default is
|
||||||
|
// runtime.NumCPU().
|
||||||
|
NumGoroutines int
|
||||||
// If Debug is true, it prints debug messages to stdout.
|
// If Debug is true, it prints debug messages to stdout.
|
||||||
Debug bool
|
Debug bool
|
||||||
}
|
}
|
||||||
@ -169,6 +169,7 @@ func NewPinger() *Pinger {
|
|||||||
MaxRTT: time.Second,
|
MaxRTT: time.Second,
|
||||||
OnRecv: nil,
|
OnRecv: nil,
|
||||||
OnIdle: nil,
|
OnIdle: nil,
|
||||||
|
NumGoroutines: runtime.NumCPU(),
|
||||||
Debug: false,
|
Debug: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -421,7 +422,9 @@ func (p *Pinger) run(once bool) {
|
|||||||
|
|
||||||
p.debugln("Run(): call recvICMP()")
|
p.debugln("Run(): call recvICMP()")
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
routines := runtime.NumCPU()
|
p.mu.Lock()
|
||||||
|
routines := p.NumGoroutines
|
||||||
|
p.mu.Unlock()
|
||||||
wg.Add(routines)
|
wg.Add(routines)
|
||||||
for i := 0; i < routines; i++ {
|
for i := 0; i < routines; i++ {
|
||||||
go p.recvICMP(conn, recvCtx, wg)
|
go p.recvICMP(conn, recvCtx, wg)
|
||||||
@ -429,7 +432,9 @@ func (p *Pinger) run(once bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if conn6 != nil {
|
if conn6 != nil {
|
||||||
routines := runtime.NumCPU()
|
p.mu.Lock()
|
||||||
|
routines := p.NumGoroutines
|
||||||
|
p.mu.Unlock()
|
||||||
wg.Add(routines)
|
wg.Add(routines)
|
||||||
for i := 0; i < routines; i++ {
|
for i := 0; i < routines; i++ {
|
||||||
go p.recvICMP(conn6, recvCtx, wg)
|
go p.recvICMP(conn6, recvCtx, wg)
|
||||||
@ -580,7 +585,9 @@ func (p *Pinger) sendICMP(conn, conn6 *icmp.PacketConn) error {
|
|||||||
p.debugln("sendICMP(): End sender goroutine")
|
p.debugln("sendICMP(): End sender goroutine")
|
||||||
}
|
}
|
||||||
|
|
||||||
routines := numGoRoutines()
|
p.mu.Lock()
|
||||||
|
routines := p.NumGoroutines
|
||||||
|
p.mu.Unlock()
|
||||||
wg.Add(routines)
|
wg.Add(routines)
|
||||||
for i := 0; i < routines; i++ {
|
for i := 0; i < routines; i++ {
|
||||||
go sendPacket(addrs, results)
|
go sendPacket(addrs, results)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user