summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-09-27 15:42:00 +0600
committerunwox <me@unwox.com>2024-09-27 15:42:00 +0600
commite8d90bcd1d21359b2e733bd6abaa89886aff1fa0 (patch)
tree91f479fefaeffb417f49c6eb9e2d7fb89df5fa7e /main.go
parent61eb3bd95a6c723f51d910c4fb95e0ca1e716adc (diff)
close opened databases on exit
Diffstat (limited to 'main.go')
-rw-r--r--main.go25
1 files changed, 24 insertions, 1 deletions
diff --git a/main.go b/main.go
index 00629f3..ed9d203 100644
--- a/main.go
+++ b/main.go
@@ -38,6 +38,17 @@ func main() {
wrks := []*Worker{}
// track routes for mux to avoid registering the same route twice
routes := make(map[string]bool)
+ // track open dbs to close them on exit
+ dbs := []*sql.DB{}
+ dbmu := sync.Mutex{}
+ defer func() {
+ for _, db := range dbs {
+ if db == nil {
+ continue
+ }
+ db.Close()
+ }
+ }()
// define luna.router module
routeModule := make(map[string]any)
@@ -163,12 +174,16 @@ func main() {
// FIXME: handle.
return 0
}
- r, err := sql.Open("sqlite3", file)
+ db, err := sql.Open("sqlite3", file)
if err != nil {
// FIXME: handle.
return 0
}
h := cgo.NewHandle(r)
+ dbmu.Lock()
+ dbs = append(dbs, db)
+ dbmu.Unlock()
+ h := cgo.NewHandle(db)
l.PushNumber(int(h))
return 1
}
@@ -354,6 +369,14 @@ func main() {
// FIXME: handle.
return 0
}
+ dbmu.Lock()
+ for k, _db := range dbs {
+ if db == _db {
+ dbs[k] = nil
+ break
+ }
+ }
+ dbmu.Unlock()
handle.Delete()
return 0
}