Add a flag to unlock database after each API request

After the first API request, the database was locked as long as the API
server is running. This prevents a user to also use the command-line
client. This commit adds a new flag `-no-lock` that will close the
database after each API request.

Closes #234
This commit is contained in:
Vincent Bernat
2015-09-07 13:44:01 +02:00
parent 16101b56fe
commit 7f6a52019f
4 changed files with 87 additions and 5 deletions
+32 -2
View File
@@ -12,11 +12,41 @@ var context *ctx.AptlyContext
func Router(c *ctx.AptlyContext) http.Handler {
context = c
go cacheFlusher()
router := gin.Default()
router.Use(gin.ErrorLogger())
if context.Flags().Lookup("no-lock").Value.Get().(bool) {
// We use a goroutine to count the number of
// concurrent requests. When no more requests are
// running, we close the database to free the lock.
requests := make(chan int)
acks := make(chan error)
go acquireDatabase(requests, acks)
go cacheFlusher(requests, acks)
router.Use(func(c *gin.Context) {
requests <- ACQUIREDB
err := <-acks
if err != nil {
c.Fail(500, err)
return
}
defer func() {
requests <- RELEASEDB
err = <-acks
if err != nil {
c.Fail(500, err)
return
}
}()
c.Next()
})
} else {
go cacheFlusher(nil, nil)
}
root := router.Group("/api")
{