diff options
author | Erwin Ticzon <eticzongh@gmail.com> | 2017-04-21 22:48:08 +1000 |
---|---|---|
committer | Erwin Ticzon <eticzongh@gmail.com> | 2017-04-21 22:48:08 +1000 |
commit | 64f2a5bd9223826afe0869813385f6b925a13fb5 (patch) | |
tree | 89de4e310cc07b40d3bf8face14a638b92e6cbd2 /system/db/backup.go | |
parent | d014fa69c51be5fa880d8ee5ae0ddbf4b2f5fd81 (diff) |
add context cancellation to backup routines
Diffstat (limited to 'system/db/backup.go')
-rw-r--r-- | system/db/backup.go | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/system/db/backup.go b/system/db/backup.go index 735abe4..4a2bf96 100644 --- a/system/db/backup.go +++ b/system/db/backup.go @@ -1,6 +1,7 @@ package db import ( + "context" "fmt" "net/http" "time" @@ -8,19 +9,29 @@ import ( "github.com/boltdb/bolt" ) -// Backup writes a snapshot of the system.db database to an HTTP response -func Backup(res http.ResponseWriter) error { - err := store.View(func(tx *bolt.Tx) error { - ts := time.Now().Unix() - disposition := `attachment; filename="system-%d.db.bak"` +// Backup writes a snapshot of the system.db database to an HTTP response. The +// output is discarded if we get a cancellation signal. +func Backup(ctx context.Context, res http.ResponseWriter) error { + errChan := make(chan error, 1) - res.Header().Set("Content-Type", "application/octet-stream") - res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts)) - res.Header().Set("Content-Length", fmt.Sprintf("%d", int(tx.Size()))) + go func() { + errChan <- store.View(func(tx *bolt.Tx) error { + ts := time.Now().Unix() + disposition := `attachment; filename="system-%d.db.bak"` - _, err := tx.WriteTo(res) - return err - }) + res.Header().Set("Content-Type", "application/octet-stream") + res.Header().Set("Content-Disposition", fmt.Sprintf(disposition, ts)) + res.Header().Set("Content-Length", fmt.Sprintf("%d", int(tx.Size()))) + + _, err := tx.WriteTo(res) + return err + }) + }() - return err + select { + case <-ctx.Done(): + return ctx.Err() + case err := <-errChan: + return err + } } |