Fix data races
parent
c9f1b02251
commit
802ef17cb4
|
@ -386,8 +386,11 @@ func (s *Server) handleSubscribe(w http.ResponseWriter, r *http.Request, v *visi
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
var wlock sync.Mutex
|
||||||
poll := r.URL.Query().Has("poll")
|
poll := r.URL.Query().Has("poll")
|
||||||
sub := func(msg *message) error {
|
sub := func(msg *message) error {
|
||||||
|
wlock.Lock()
|
||||||
|
defer wlock.Unlock()
|
||||||
m, err := encoder(msg)
|
m, err := encoder(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ const (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
randomMutex = sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// FileExists checks if a file exists, and returns true if it does
|
// FileExists checks if a file exists, and returns true if it does
|
||||||
|
@ -23,6 +25,8 @@ func FileExists(filename string) bool {
|
||||||
|
|
||||||
// RandomString returns a random string with a given length
|
// RandomString returns a random string with a given length
|
||||||
func RandomString(length int) string {
|
func RandomString(length int) string {
|
||||||
|
randomMutex.Lock() // Who would have thought that random.Intn() is not thread-safe?!
|
||||||
|
defer randomMutex.Unlock()
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
|
b[i] = randomStringCharset[random.Intn(len(randomStringCharset))]
|
||||||
|
|
Loading…
Reference in New Issue