Skip to content

Commit 7a4be9b

Browse files
committed
concurrent fix: add RWlock
1 parent cdfe5b4 commit 7a4be9b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/scientiacoder/go-PingClient
33
go 1.15
44

55
require (
6-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b
6+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
7+
golang.org/x/sys v0.0.0-20210313202042-bd2e13477e9c // indirect
78
gopkg.in/yaml.v2 v2.4.0
89
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
22
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
3+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
4+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
35
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
46
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/sys v0.0.0-20210313202042-bd2e13477e9c h1:coiPEfMv+ThsjULRDygLrJVlNE1gDdL2g65s0LhV2os=
8+
golang.org/x/sys v0.0.0-20210313202042-bd2e13477e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
59
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
610
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
711
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

pingClient.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const (
9696
var (
9797
ipv4Proto = map[string]string{"icmp": "ip4:icmp", "udp": "udp4"}
9898
ipv6Proto = map[string]string{"icmp": "ip6:ipv6-icmp", "udp": "udp6"}
99+
globalmu sync.RWMutex
99100
)
100101

101102
// New returns a new PingClient struct pointer.
@@ -602,9 +603,13 @@ func (p *PingClient) processPacket(recv *packet) error {
602603
func (p *PingClient) sendICMP(conn, conn6 *icmp.PacketConn) error {
603604
wg := new(sync.WaitGroup)
604605
for _, addr := range p.IPs {
606+
globalmu.RLock()
605607
if !p.Continuous && p.PacketsSent[addr.IP.String()] >= p.Num {
608+
globalmu.RUnlock()
606609
continue
607610
}
611+
globalmu.RUnlock()
612+
608613
var cn *icmp.PacketConn
609614
var typ icmp.Type
610615
if isIPv4(addr.IP) {
@@ -661,7 +666,9 @@ func (p *PingClient) sendICMP(conn, conn6 *icmp.PacketConn) error {
661666
}
662667
break
663668
}
669+
globalmu.Lock()
664670
p.PacketsSent[ipStr]++
671+
globalmu.Unlock()
665672
wg.Done()
666673
}(cn, dst, ipStr, msgBytes)
667674
}
@@ -681,6 +688,8 @@ func (p *PingClient) listen(netProto string) (*icmp.PacketConn, error) {
681688
}
682689

683690
func (p *PingClient) initPacketsConfig() {
691+
globalmu.Lock()
692+
defer globalmu.Unlock()
684693
for _, addr := range p.IPs {
685694
p.PacketsSent[addr.IP.String()] = 0
686695
p.PacketsRecv[addr.IP.String()] = 0
@@ -691,6 +700,8 @@ func (p *PingClient) initPacketsConfig() {
691700

692701
// All checks whether all the map entry satisfies the function f
693702
func All(m map[string]int, f func(int, int) bool, num int) bool {
703+
globalmu.RLock()
704+
defer globalmu.RUnlock()
694705
for _, val := range m {
695706
if !f(val, num) {
696707
return false
@@ -729,7 +740,9 @@ func (p *PingClient) Statistics() []*Statistics {
729740
// StatisticsPerIP returns the statistics of the Ping info to the given IP address.
730741
func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics {
731742
var ipStr string = ipAddr.IP.String()
743+
globalmu.RLock()
732744
loss := float64(p.PacketsSent[ipStr]-p.PacketsRecv[ipStr]) / float64(p.PacketsSent[ipStr]) * 100
745+
globalmu.Unlock()
733746
var min, max, total time.Duration
734747
if len(p.rtts[ipStr]) > 0 {
735748
min = p.rtts[ipStr][0]
@@ -744,6 +757,7 @@ func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics {
744757
}
745758
total += rt
746759
}
760+
globalmu.RLock()
747761
s := Statistics{
748762
PacketsSent: p.PacketsSent[ipStr],
749763
PacketsRecv: p.PacketsRecv[ipStr],
@@ -755,6 +769,7 @@ func (p *PingClient) StatisticsPerIP(ipAddr *net.IPAddr) *Statistics {
755769
MaxRtt: max,
756770
MinRtt: min,
757771
}
772+
globalmu.RUnlock()
758773
if len(p.rtts[ipStr]) > 0 {
759774
s.AvgRtt = total / time.Duration(len(p.rtts[ipStr]))
760775
var sumsquares time.Duration

0 commit comments

Comments
 (0)