Skip to content

Commit

Permalink
Merge pull request #15 from jakoblorz/f/aggressive-snapshotting
Browse files Browse the repository at this point in the history
Update snapshotting mechanism
  • Loading branch information
jakoblorz authored Aug 21, 2022
2 parents bba9295 + 5766340 commit 533da1a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ for the packet ids to select.
api := privateapi.New(token, baseURL)
defer api.Close()

db, err := streamdb.Open("autofone", &snapshotWriter{api}, streamdb.DebounceModeActive)
db, err := streamdb.Open("autofone", &snapshotWriter{api}, streamdb.DebounceModeDelay)
if err != nil {
log.Printf("%+v", err)
return
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/boltdb/bolt v1.3.1
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf
github.com/jmoiron/sqlx v1.3.5
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.14
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaBfAKrE1Cwb61YDtYq9JxChK1c7AKce7s=
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down
16 changes: 13 additions & 3 deletions pkg/streamdb/bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"time"

"github.com/jakoblorz/autofone/constants"
"github.com/jakoblorz/autofone/pkg/log"

"github.com/boltdb/bolt"
"github.com/inhies/go-bytesize"
)

type DebounceMode string
Expand All @@ -24,7 +26,7 @@ const (
)

const (
DebounceModeDelay_Interval time.Duration = 1 * time.Minute
DebounceModeDelay_Interval time.Duration = 25 * time.Second
DebounceModeActive_Interval time.Duration = 25 * time.Second
DebounceModeAggressive_Interval time.Duration = 250 * time.Millisecond
)
Expand Down Expand Up @@ -108,19 +110,26 @@ func (i *stream) notify() {
i.debounceMx.Lock()
defer i.debounceMx.Unlock()

if i.debounceMode == DebounceModeDelay && i.debounceTimer == nil {
i.debounceTimer = time.AfterFunc(DebounceModeDelay_Interval, i.rotate)
// DebounceModeDelay does not reset the timer, so we don't need to do anything.
// The timer MUST be reset stopped, then set to nil
if i.debounceMode == DebounceModeDelay {
if i.debounceTimer == nil {
i.debounceTimer = time.AfterFunc(DebounceModeDelay_Interval, i.rotate)
}
return
}

if i.debounceTimer != nil {
i.debounceTimer.Stop()
i.debounceTimer = nil
}
if i.debounceMode == DebounceModeActive {
i.debounceTimer = time.AfterFunc(DebounceModeActive_Interval, i.rotate)
return
}
if i.debounceMode == DebounceModeAggressive {
i.debounceTimer = time.AfterFunc(DebounceModeAggressive_Interval, i.rotate)
return
}
}

Expand Down Expand Up @@ -163,6 +172,7 @@ func (i *stream) rotate() {
rotateWithPriviledges(func() {
backup := &bytes.Buffer{}
err := i.handleDb.Update(func(tx *bolt.Tx) error {
log.Printf("snapshotting %s of data", bytesize.New(float64(tx.Size())))
_, err := tx.WriteTo(backup)
if err != nil {
return err
Expand Down

0 comments on commit 533da1a

Please sign in to comment.