From 5122791f85dc5d3f27c105d637502084377e3092 Mon Sep 17 00:00:00 2001 From: minie4 Date: Mon, 16 Dec 2024 21:29:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Do=20not=20save=20config?= =?UTF-8?q?=20changes=20to=20disk=20immediately?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.go | 40 +++++++++++++++++++++++++++------------- config.go | 22 ++++++++++++++++++++++ main.go | 15 +++++++++++++++ 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/api.go b/api.go index 9d4825d..45abe40 100644 --- a/api.go +++ b/api.go @@ -123,7 +123,7 @@ func createPort(body CreatePortRequest) (gin.H, gin.H) { registerPort(&newPort, "output") portConfig.Output = append(portConfig.Output, newPort) } - saveConfig("ports", &portConfig) + markConfig("ports") onPortListChange() return gin.H{"success": true, "UUID": id}, nil } @@ -155,7 +155,7 @@ func deletePort(id string) gin.H { // Unregister port unregisterPort(port) - saveConfig("ports", &portConfig) + markConfig("ports") onPortListChange() return nil } @@ -206,18 +206,25 @@ func setState(id string, body SetPortRequest) (gin.H, gin.H) { return nil, gin.H{"success": false, "error": "Port with provided UUID does not exist!"} } - if body.Mute != nil { + didChange := false + + if body.Mute != nil && port.State.Mute != *body.Mute { port.State.Mute = *body.Mute + didChange = true } - if body.Volume != nil { + if body.Volume != nil && port.State.Volume != *body.Volume { port.State.Volume = *body.Volume + didChange = true } - if body.Balance != nil { + if body.Balance != nil && port.State.Balance != *body.Balance { port.State.Balance = *body.Balance + didChange = true } - saveConfig("ports", &portConfig) + if didChange { + markConfig("ports") onPortChange(port) + } return gin.H{"success": true, "state": port.State}, nil } @@ -253,7 +260,7 @@ func editPort(id string, body EditPortRequest) (gin.H, gin.H) { port.Name = *body.Name } - saveConfig("ports", &portConfig) + markConfig("ports") onPortChange(port) return gin.H{"success": true, "port": port}, nil } @@ -291,7 +298,7 @@ func createRoute(id string, body CreateRouteRequest) gin.H { } from.Route = append(from.Route, PortRoute{ToUUID: body.To, Mute: true, Volume: 0, Balance: 0}) - saveConfig("ports", &portConfig) + markConfig("ports") onPortChange(from) return nil } @@ -331,18 +338,25 @@ func setRoute(fromId string, toId string, body SetRouteRequest) (gin.H, gin.H) { continue } - if body.Mute != nil { + didChange := false + + if body.Mute != nil && from.Route[i].Mute != *body.Mute { from.Route[i].Mute = *body.Mute + didChange = true } - if body.Volume != nil { + if body.Volume != nil && from.Route[i].Volume != *body.Volume { from.Route[i].Volume = *body.Volume + didChange = true } - if body.Balance != nil { + if body.Balance != nil && from.Route[i].Balance != *body.Balance { from.Route[i].Balance = *body.Balance + didChange = true } - saveConfig("ports", &portConfig) + if didChange { + markConfig("ports") onPortChange(from) + } return gin.H{"success": true, "state": from.Route[i]}, nil } @@ -372,7 +386,7 @@ func deleteRoute(fromId string, toId string) gin.H { } from.Route = append(from.Route[:i], from.Route[i+1:]...) - saveConfig("ports", &portConfig) + markConfig("ports") onPortListChange() return nil } diff --git a/config.go b/config.go index 0fd6e47..680b210 100644 --- a/config.go +++ b/config.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "sync" "github.com/pelletier/go-toml/v2" ) @@ -27,6 +28,11 @@ type Config struct { var config Config +var shouldSaveConfigsMutex = new(sync.Mutex) +var shouldSaveConfigs = map[string]bool{ + "ports": false, +} + // Copy default config if `{name}.toml` file does not exist func createDefaultConfig(name string) { dest_path := fmt.Sprintf("%s/%s.toml", CONFIG_DIR, name) @@ -59,6 +65,13 @@ func loadConfig(name string, dest interface{}) { } } +// Mark a config file as changed, so it gets saved to disk later +func markConfig(name string) { + shouldSaveConfigsMutex.Lock() + shouldSaveConfigs[name] = true + shouldSaveConfigsMutex.Unlock() +} + // Save struct as a `.toml` file func saveConfig(name string, dest interface{}) { bin, err := toml.Marshal(dest) @@ -70,3 +83,12 @@ func saveConfig(name string, dest interface{}) { fmt.Println(err) } } + +func saveConfigIfMarked(name string, dest interface{}) { + shouldSaveConfigsMutex.Lock() + if shouldSaveConfigs[name] { + shouldSaveConfigs[name] = false + saveConfig(name, dest) + } + shouldSaveConfigsMutex.Unlock() +} diff --git a/main.go b/main.go index 05586e3..83d36e3 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "os" "os/signal" "syscall" + "time" ) func main() { @@ -25,6 +26,20 @@ func main() { // Start audio routing startRouting() + // Periodically save config files + ticker := time.NewTicker(5 * time.Second) + go func() { + for { + select { + case <-ticker.C: + saveConfigIfMarked("ports", portConfig) + case <-end: + ticker.Stop() + return + } + } + }() + // Wait until SIGINT received <-end