]> Sergey Matveev's repositories - vors.git/blobdiff - cmd/client/main.go
Use mutexes over iterable maps for reliability
[vors.git] / cmd / client / main.go
index f485d513ffa4fa32d2a2bec43be19b9e8d779982e3da482a72dc56459931315d..d41b8d9987dc95236845cc7e2bdf8a9065bdd2b25d3cbaf947bd294be22db5e1 100644 (file)
@@ -29,6 +29,7 @@ import (
        "os/exec"
        "strconv"
        "strings"
+       "sync"
        "time"
 
        "github.com/dchest/siphash"
@@ -53,6 +54,7 @@ type Stream struct {
 
 var (
        Streams  = map[byte]*Stream{}
+       StreamsM sync.RWMutex
        Finish   = make(chan struct{})
        OurStats = &Stats{dead: make(chan struct{})}
        Name     = flag.String("name", "test", "username")
@@ -274,7 +276,9 @@ func main() {
                                        log.Fatalln("cookie acceptance failed:", string(args[1]))
                                case vors.CmdSID:
                                        sid = args[1][0]
+                                       StreamsM.Lock()
                                        Streams[sid] = &Stream{name: *Name, stats: OurStats}
+                                       StreamsM.Unlock()
                                default:
                                        log.Fatalln("unexpected post-cookie cmd:", cmd)
                                }
@@ -534,7 +538,9 @@ func main() {
                                        }
                                }()
                                go statsDrawer(stream)
+                               StreamsM.Lock()
                                Streams[sid] = stream
+                               StreamsM.Unlock()
                        case vors.CmdDel:
                                sid := args[1][0]
                                s := Streams[sid]
@@ -543,7 +549,9 @@ func main() {
                                        continue
                                }
                                log.Println("del", s.name, "sid:", sid)
+                               StreamsM.Lock()
                                delete(Streams, sid)
+                               StreamsM.Unlock()
                                close(s.in)
                                close(s.stats.dead)
                        case vors.CmdMuted: