mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-22 17:31:03 +01:00
Merge pull request #587 from hantmac/develop
bug fix: fix concurrent map write error
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"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)
|
||||
}
|
||||
ch := make(chan string)
|
||||
cm.m[key] = ch
|
||||
cm.m.Store(key, ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user