bug fix: fix concurrent map write error
package utils
+import (
+ "fmt"
+ "sync"
+)
+
var TaskExecChanMap = NewChanMap()
type ChanMap struct {
- m map[string]chan string
+ m sync.Map
}
func NewChanMap() *ChanMap {
- return &ChanMap{m: make(map[string]chan string)}
+ return &ChanMap{m: sync.Map{}}
}
func (cm *ChanMap) Chan(key string) chan string {
- if ch, ok := cm.m[key]; ok {
- return ch
+ if ch, ok := cm.m.Load(key); ok {
+ return ch.(interface{}).(chan string)
}
ch := make(chan string, 10)
- cm.m[key] = ch
+ cm.m.Store(key, ch)
return ch
}
func (cm *ChanMap) ChanBlocked(key string) chan string {
- if ch, ok := cm.m[key]; ok {
- return ch
+ if ch, ok := cm.m.Load(key); ok {
+ return ch.(interface{}).(chan string)
}
+ fmt.Println("@@####@@@@#@####$@@#")
ch := make(chan string)
- cm.m[key] = ch
+ cm.m.Store(key, ch)
return ch
}
