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
}
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 {

View File

@ -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
}
}