lock on every http req BUT LOCKS ON TELEMETRY SO BEWARE
parent
f14871218d
commit
85804d6f84
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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{}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue