Skip to content

Commit

Permalink
fix plugin switch_legacy: concurrentmap write
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Jul 13, 2022
1 parent 2229312 commit a7b4082
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver v0.54.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kafkareceiver v0.54.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.54.0
github.com/orcaman/concurrent-map v1.0.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/percona/percona-toolkit v0.0.0-20211210121818-b2860eee3152
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,8 @@ github.com/openzipkin/zipkin-go v0.2.2/go.mod h1:NaW6tEwdmWMaCDZzg8sh+IBNOxHMPnh
github.com/openzipkin/zipkin-go v0.2.5/go.mod h1:KpXfKdgRDnnhsxw4pNIH9Md5lyFqKUa4YDFlwRYAMyE=
github.com/openzipkin/zipkin-go v0.4.0 h1:CtfRrOVZtbDj8rt1WXjklw0kqqJQwICrCKmlfUuBUUw=
github.com/openzipkin/zipkin-go v0.4.0/go.mod h1:4c3sLeE8xjNqehmF5RpAFLPLJxXscc0R4l6Zg0P1tTQ=
github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HDbW65HOY=
github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI=
github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIwwtUjcrb0b5/5kLM=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
Expand Down
15 changes: 8 additions & 7 deletions inputs/switch_legacy/switch_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"flashcat.cloud/categraf/pkg/runtimex"
"flashcat.cloud/categraf/types"
"github.com/gaochao1/sw"
cmap "github.com/orcaman/concurrent-map"
"github.com/toolkits/pkg/concurrent/semaphore"
"github.com/toolkits/pkg/container/list"
go_snmp "github.com/ulricqin/gosnmp"
Expand Down Expand Up @@ -602,11 +603,10 @@ func (ins *Instance) ifstat(wg *sync.WaitGroup, sema *semaphore.Semaphore, ip st
}

func (ins *Instance) gatherPing(ips []string, slist *list.SafeList) []string {
// init ping result
pingResult := make(map[string]bool)
pingResult := cmap.New()
for i := 0; i < len(ips); i++ {
// init ping result
pingResult[ips[i]] = false
pingResult.Set(ips[i], false)
}

wg := new(sync.WaitGroup)
Expand All @@ -620,9 +620,10 @@ func (ins *Instance) gatherPing(ips []string, slist *list.SafeList) []string {
wg.Wait()

ips = make([]string, 0, len(ips))
for ip, succ := range pingResult {

for ip, succ := range pingResult.Items() {
val := 0
if succ {
if succ.(bool) {
val = 1
ips = append(ips, ip)
}
Expand All @@ -647,7 +648,7 @@ func (ins *Instance) parseIPs() (lst []string) {
return
}

func (ins *Instance) ping(wg *sync.WaitGroup, sema *semaphore.Semaphore, ip string, result map[string]bool) {
func (ins *Instance) ping(wg *sync.WaitGroup, sema *semaphore.Semaphore, ip string, result cmap.ConcurrentMap) {
defer func() {
sema.Release()
wg.Done()
Expand All @@ -656,7 +657,7 @@ func (ins *Instance) ping(wg *sync.WaitGroup, sema *semaphore.Semaphore, ip stri
for i := 0; i < ins.PingRetries; i++ {
succ := sw.Ping(ip, int(ins.PingTimeoutMs), ins.PingModeFastping)
if succ {
result[ip] = succ
result.Set(ip, succ)
break
}
}
Expand Down

0 comments on commit a7b4082

Please sign in to comment.