lock on every http req BUT LOCKS ON TELEMETRY SO BEWARE
parent
f14871218d
commit
85804d6f84
|
|
@ -2,12 +2,14 @@ package v01
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"sync"
|
||||||
|
|
||||||
patch "github.com/evanphx/json-patch/v5"
|
patch "github.com/evanphx/json-patch/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
config struct {
|
config struct {
|
||||||
|
lock *sync.Mutex
|
||||||
Feedback configFeedback
|
Feedback configFeedback
|
||||||
Users map[string]configUser
|
Users map[string]configUser
|
||||||
Players []configPlayer
|
Players []configPlayer
|
||||||
|
|
@ -33,6 +35,8 @@ type (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cfg config) WithPatch(v interface{}) config {
|
func (cfg config) WithPatch(v interface{}) config {
|
||||||
|
cfg.lock.Lock()
|
||||||
|
defer cfg.lock.Unlock()
|
||||||
originalData, _ := json.Marshal(cfg)
|
originalData, _ := json.Marshal(cfg)
|
||||||
patchData, _ := json.Marshal(v)
|
patchData, _ := json.Marshal(v)
|
||||||
patcher, err := patch.DecodePatch(patchData)
|
patcher, err := patch.DecodePatch(patchData)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package v01
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -70,7 +71,10 @@ func TestConfigPatch(t *testing.T) {
|
||||||
for name, d := range cases {
|
for name, d := range cases {
|
||||||
c := d
|
c := d
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
|
c.cfg.lock = &sync.Mutex{}
|
||||||
got := c.cfg.WithPatch(c.patch)
|
got := c.cfg.WithPatch(c.patch)
|
||||||
|
got.lock = nil
|
||||||
|
c.want.lock = nil
|
||||||
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
|
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)
|
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{
|
s := &http.Server{
|
||||||
Addr: v01.cfg.Feedback.Addr,
|
Addr: v01.cfg.Feedback.Addr,
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
v01.cfg.lock.Lock()
|
||||||
|
defer v01.cfg.lock.Unlock()
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
@ -53,6 +54,7 @@ func TestPatchConfig(t *testing.T) {
|
||||||
FlagParseV01Config = p
|
FlagParseV01Config = p
|
||||||
}
|
}
|
||||||
v01 := &V01{cfg: c.was}
|
v01 := &V01{cfg: c.was}
|
||||||
|
v01.cfg.lock = &sync.Mutex{}
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r := httptest.NewRequest(http.MethodPatch, "/config", strings.NewReader(c.patch))
|
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/button"
|
||||||
"mayhem-party/src/device/input/raw"
|
"mayhem-party/src/device/input/raw"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
@ -29,6 +30,7 @@ type (
|
||||||
|
|
||||||
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
func NewV01(ctx context.Context, src raw.Raw) *V01 {
|
||||||
var cfg config
|
var cfg config
|
||||||
|
cfg.lock = &sync.Mutex{}
|
||||||
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
b, _ := ioutil.ReadFile(FlagParseV01Config)
|
||||||
yaml.Unmarshal(b, &cfg)
|
yaml.Unmarshal(b, &cfg)
|
||||||
ctx, can := context.WithCancel(ctx)
|
ctx, can := context.WithCancel(ctx)
|
||||||
|
|
@ -70,6 +72,8 @@ func (v01 *V01) Read() []button.Button {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v01 *V01) telemetry(msg message) {
|
func (v01 *V01) telemetry(msg message) {
|
||||||
|
v01.cfg.lock.Lock()
|
||||||
|
defer v01.cfg.lock.Unlock()
|
||||||
if v01.cfg.Users == nil {
|
if v01.cfg.Users == nil {
|
||||||
v01.cfg.Users = map[string]configUser{}
|
v01.cfg.Users = map[string]configUser{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue