about summary refs log tree commit diff stats
path: root/init.go
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-05-21 16:04:10 -0400
committerBen Morrison <ben@gbmor.dev>2019-05-21 16:04:10 -0400
commitb29e1c163f3cc88037f301c1cc1d71c7707ee64f (patch)
treeb06e51c3e27a8fb70f3801a27a957e5f7cae4bcb /init.go
parent137e237b17b770e9697e7367c1ee01f4f3bb5717 (diff)
downloadgetwtxt-b29e1c163f3cc88037f301c1cc1d71c7707ee64f.tar.gz
review of db functions
Diffstat (limited to 'init.go')
-rw-r--r--init.go16
1 files changed, 6 insertions, 10 deletions
diff --git a/init.go b/init.go
index 6896a1a..639a78b 100644
--- a/init.go
+++ b/init.go
@@ -33,11 +33,6 @@ var closelog = make(chan bool, 1)
 // initialization
 var dbChan = make(chan *leveldb.DB, 1)
 
-// provides access to the database so it
-// can be closed outside of the init function's
-// scope.
-var dbCloseChan = make(chan *leveldb.DB, 1)
-
 // templates
 var tmpls *template.Template
 
@@ -54,7 +49,6 @@ func init() {
 	initLogging()
 	tmpls = initTemplates()
 	initDatabase()
-	go cacheAndPush()
 	watchForInterrupt()
 }
 
@@ -218,17 +212,19 @@ func initTemplates() *template.Template {
 	return template.Must(template.ParseFiles("assets/tmpl/index.html"))
 }
 
-// Pull DB data into cache, if available
+// Pull DB data into cache, if available.
 func initDatabase() {
 	db, err := leveldb.OpenFile(confObj.dbPath, nil)
 	if err != nil {
 		log.Fatalf("%v\n", err)
 	}
 
+	// Send the database reference into
+	// the aether.
 	dbChan <- db
-	dbCloseChan <- db
 
 	pullDatabase()
+	go cacheAndPush()
 }
 
 // Watch for SIGINT aka ^C
@@ -245,7 +241,7 @@ func watchForInterrupt() {
 
 			// Close the database cleanly
 			log.Printf("Closing database connection to %v...\n", confObj.dbPath)
-			db := <-dbCloseChan
+			db := <-dbChan
 			if err := db.Close(); err != nil {
 				log.Printf("%v\n", err)
 			}
@@ -256,7 +252,7 @@ func watchForInterrupt() {
 			}
 
 			confObj.mu.RUnlock()
-			close(dbCloseChan)
+			close(dbChan)
 			close(closelog)
 
 			// Let everything catch up