diff --git a/modules/sql/sql.go b/modules/sql/sql.go index febcd38..1492f97 100644 --- a/modules/sql/sql.go +++ b/modules/sql/sql.go @@ -137,6 +137,16 @@ func CloseConnection(id string) error { return err } +func CloseAllConnections() { + global.mu.Lock() + defer global.mu.Unlock() + + for id, conn := range global.conns { + conn.Close() + delete(global.conns, id) + } +} + // Lua function implementations func luaConnect(s *luajit.State) int { diff --git a/moonshark.go b/moonshark.go index 8ee6b6c..3c8103a 100644 --- a/moonshark.go +++ b/moonshark.go @@ -11,6 +11,7 @@ import ( "time" "Moonshark/modules/http" + "Moonshark/modules/sql" "Moonshark/state" ) @@ -64,6 +65,7 @@ func runOnce(scriptPath string) { luaState.Close() // Then stop servers (closes worker states) http.StopAllServers() + sql.CloseAllConnections() os.Exit(0) }() @@ -151,6 +153,7 @@ func runWithWatcher(scriptPath string) { fmt.Println("Files changed, restarting...") http.StopAllServers() luaState.Close() + sql.CloseAllConnections() time.Sleep(100 * time.Millisecond) // Brief pause for cleanup continue @@ -158,6 +161,7 @@ func runWithWatcher(scriptPath string) { fmt.Println("\nShutting down...") http.StopAllServers() luaState.Close() + sql.CloseAllConnections() return } }