fix plugin switch_legacy: concurrentmap write

This commit is contained in:
Ulric Qin 2022-07-13 18:16:31 +08:00
parent 2229312e4b
commit a7b4082266
3 changed files with 11 additions and 7 deletions

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=

View File

@ -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"
@ -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)
@ -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)
}
@ -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()
@ -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
}
}