Skip to content

Commit

Permalink
fix: a potential concurrency problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Jan 1, 2021
1 parent 670553d commit c6ec2c6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ func main() {
}

func listen(group *config.Group) {
mMutex.Lock()
if _, ok := mPortDispatcher[group.Port]; !ok {
mPortDispatcher[group.Port] = new([2]dispatcher.Dispatcher)
}
mMutex.Unlock()
err := listenWithProtocols(group, protocols[:])
if err != nil {
mMutex.Lock()
Expand All @@ -45,10 +40,17 @@ func listen(group *config.Group) {
}

func listenWithProtocols(group *config.Group, protocols []string) error {
mMutex.Lock()
if _, ok := mPortDispatcher[group.Port]; !ok {
mPortDispatcher[group.Port] = new([2]dispatcher.Dispatcher)
}
t := mPortDispatcher[group.Port]
mMutex.Unlock()

ch := make(chan error, len(protocols))
for i, protocol := range protocols {
d, _ := dispatcher.New(protocol, group)
(*mPortDispatcher[group.Port])[i] = d
(*t)[i] = d
go func() {
var err error
err = d.Listen()
Expand Down

0 comments on commit c6ec2c6

Please sign in to comment.