clean up sql connections at server shutdown

This commit is contained in:
Sky Johnson 2025-08-01 15:35:31 -05:00
parent 041b4a517d
commit 55e6b54789
2 changed files with 14 additions and 0 deletions

View File

@ -137,6 +137,16 @@ func CloseConnection(id string) error {
return err 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 // Lua function implementations
func luaConnect(s *luajit.State) int { func luaConnect(s *luajit.State) int {

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"Moonshark/modules/http" "Moonshark/modules/http"
"Moonshark/modules/sql"
"Moonshark/state" "Moonshark/state"
) )
@ -64,6 +65,7 @@ func runOnce(scriptPath string) {
luaState.Close() luaState.Close()
// Then stop servers (closes worker states) // Then stop servers (closes worker states)
http.StopAllServers() http.StopAllServers()
sql.CloseAllConnections()
os.Exit(0) os.Exit(0)
}() }()
@ -151,6 +153,7 @@ func runWithWatcher(scriptPath string) {
fmt.Println("Files changed, restarting...") fmt.Println("Files changed, restarting...")
http.StopAllServers() http.StopAllServers()
luaState.Close() luaState.Close()
sql.CloseAllConnections()
time.Sleep(100 * time.Millisecond) // Brief pause for cleanup time.Sleep(100 * time.Millisecond) // Brief pause for cleanup
continue continue
@ -158,6 +161,7 @@ func runWithWatcher(scriptPath string) {
fmt.Println("\nShutting down...") fmt.Println("\nShutting down...")
http.StopAllServers() http.StopAllServers()
luaState.Close() luaState.Close()
sql.CloseAllConnections()
return return
} }
} }