From 85804d6f84fd723e3ea42cd6fb3cb9a4aedb8bdb Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Mon, 27 Mar 2023 06:18:32 -0600 Subject: [PATCH] lock on every http req BUT LOCKS ON TELEMETRY SO BEWARE --- src/device/input/parse/v01/config.go | 4 ++++ src/device/input/parse/v01/config_test.go | 4 ++++ src/device/input/parse/v01/server.go | 2 ++ src/device/input/parse/v01/server_test.go | 2 ++ src/device/input/parse/v01/v01.go | 4 ++++ 5 files changed, 16 insertions(+) diff --git a/src/device/input/parse/v01/config.go b/src/device/input/parse/v01/config.go index 4c62453..9b64907 100644 --- a/src/device/input/parse/v01/config.go +++ b/src/device/input/parse/v01/config.go @@ -2,12 +2,14 @@ package v01 import ( "encoding/json" + "sync" patch "github.com/evanphx/json-patch/v5" ) type ( config struct { + lock *sync.Mutex Feedback configFeedback Users map[string]configUser Players []configPlayer @@ -33,6 +35,8 @@ type ( ) func (cfg config) WithPatch(v interface{}) config { + cfg.lock.Lock() + defer cfg.lock.Unlock() originalData, _ := json.Marshal(cfg) patchData, _ := json.Marshal(v) patcher, err := patch.DecodePatch(patchData) diff --git a/src/device/input/parse/v01/config_test.go b/src/device/input/parse/v01/config_test.go index 859fdf9..7b21ebe 100644 --- a/src/device/input/parse/v01/config_test.go +++ b/src/device/input/parse/v01/config_test.go @@ -2,6 +2,7 @@ package v01 import ( "fmt" + "sync" "testing" ) @@ -70,7 +71,10 @@ func TestConfigPatch(t *testing.T) { for name, d := range cases { c := d t.Run(name, func(t *testing.T) { + c.cfg.lock = &sync.Mutex{} got := c.cfg.WithPatch(c.patch) + got.lock = nil + c.want.lock = nil if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) { t.Errorf("(%+v).Patch(%+v) want %+v, got %+v", c.cfg, c.patch, c.want, got) } diff --git a/src/device/input/parse/v01/server.go b/src/device/input/parse/v01/server.go index 72c5081..922eada 100644 --- a/src/device/input/parse/v01/server.go +++ b/src/device/input/parse/v01/server.go @@ -29,6 +29,8 @@ func (v01 *V01) _listen() { s := &http.Server{ Addr: v01.cfg.Feedback.Addr, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + v01.cfg.lock.Lock() + defer v01.cfg.lock.Unlock() if r.Method == http.MethodGet { mutex.Lock() defer mutex.Unlock() diff --git a/src/device/input/parse/v01/server_test.go b/src/device/input/parse/v01/server_test.go index c7d25ed..63a3de4 100644 --- a/src/device/input/parse/v01/server_test.go +++ b/src/device/input/parse/v01/server_test.go @@ -7,6 +7,7 @@ import ( "os" "path" "strings" + "sync" "testing" "gopkg.in/yaml.v2" @@ -53,6 +54,7 @@ func TestPatchConfig(t *testing.T) { FlagParseV01Config = p } v01 := &V01{cfg: c.was} + v01.cfg.lock = &sync.Mutex{} w := httptest.NewRecorder() r := httptest.NewRequest(http.MethodPatch, "/config", strings.NewReader(c.patch)) diff --git a/src/device/input/parse/v01/v01.go b/src/device/input/parse/v01/v01.go index 0041ec0..fcfa1c3 100644 --- a/src/device/input/parse/v01/v01.go +++ b/src/device/input/parse/v01/v01.go @@ -8,6 +8,7 @@ import ( "mayhem-party/src/device/input/button" "mayhem-party/src/device/input/raw" "os" + "sync" "time" "gopkg.in/yaml.v2" @@ -29,6 +30,7 @@ type ( func NewV01(ctx context.Context, src raw.Raw) *V01 { var cfg config + cfg.lock = &sync.Mutex{} b, _ := ioutil.ReadFile(FlagParseV01Config) yaml.Unmarshal(b, &cfg) ctx, can := context.WithCancel(ctx) @@ -70,6 +72,8 @@ func (v01 *V01) Read() []button.Button { } func (v01 *V01) telemetry(msg message) { + v01.cfg.lock.Lock() + defer v01.cfg.lock.Unlock() if v01.cfg.Users == nil { v01.cfg.Users = map[string]configUser{} }