mirror of
https://github.com/go-ping/ping.git
synced 2025-07-17 07:31:16 +00:00
refactor: simplify handling of done channel
This commit is contained in:
parent
dbd1b5c8a8
commit
8ef5fddf90
11
ping.go
11
ping.go
@ -96,7 +96,7 @@ func New(addr string) *Pinger {
|
|||||||
Size: timeSliceLength + trackerLength,
|
Size: timeSliceLength + trackerLength,
|
||||||
Timeout: time.Duration(math.MaxInt64),
|
Timeout: time.Duration(math.MaxInt64),
|
||||||
addr: addr,
|
addr: addr,
|
||||||
done: make(chan interface{}),
|
done: make(chan struct{}),
|
||||||
id: r.Intn(math.MaxUint16),
|
id: r.Intn(math.MaxUint16),
|
||||||
currentUUID: uuid.New(),
|
currentUUID: uuid.New(),
|
||||||
ipaddr: nil,
|
ipaddr: nil,
|
||||||
@ -190,7 +190,7 @@ type Pinger struct {
|
|||||||
Source string
|
Source string
|
||||||
|
|
||||||
// Channel and mutex used to communicate when the Pinger should stop between goroutines.
|
// Channel and mutex used to communicate when the Pinger should stop between goroutines.
|
||||||
done chan interface{}
|
done chan struct{}
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
|
||||||
ipaddr *net.IPAddr
|
ipaddr *net.IPAddr
|
||||||
@ -549,13 +549,10 @@ func (p *Pinger) Stop() {
|
|||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
open := true
|
|
||||||
select {
|
select {
|
||||||
case _, open = <-p.done:
|
case <-p.done:
|
||||||
|
return
|
||||||
default:
|
default:
|
||||||
}
|
|
||||||
|
|
||||||
if open {
|
|
||||||
close(p.done)
|
close(p.done)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user