diff options
author | Steve Manuel <nilslice@gmail.com> | 2016-11-04 14:00:54 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2016-11-04 14:00:54 -0700 |
commit | ba0bdb862ed6ad2b586365df7ae1db3ae0f004d4 (patch) | |
tree | d4717ace21b9ffddb6b6a2947c719d0b582d0133 /system/api/analytics/init.go | |
parent | 33e38247b580e9f5b38a4aa8a186f3ac2c814f05 (diff) |
adding batchPrune func to delete old analytics data, eventually would like to add a backup service. renaming recordChan for clarity
Diffstat (limited to 'system/api/analytics/init.go')
-rw-r--r-- | system/api/analytics/init.go | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/system/api/analytics/init.go b/system/api/analytics/init.go index fd65a79..722cf0e 100644 --- a/system/api/analytics/init.go +++ b/system/api/analytics/init.go @@ -27,8 +27,8 @@ type apiRequest struct { } var ( - store *bolt.DB - recordChan chan apiRequest + store *bolt.DB + requestChan chan apiRequest ) // Record queues an apiRequest for metrics @@ -45,8 +45,8 @@ func Record(req *http.Request) { External: external, } - // put r on buffered recordChan to take advantage of batch insertion in DB - recordChan <- r + // put r on buffered requestChan to take advantage of batch insertion in DB + requestChan <- r } // Close exports the abillity to close our db file. Should be called with defer @@ -67,7 +67,7 @@ func Init() { log.Fatalln(err) } - recordChan = make(chan apiRequest, 1024*64*runtime.NumCPU()) + requestChan = make(chan apiRequest, 1024*64*runtime.NumCPU()) go serve() @@ -77,31 +77,29 @@ func Init() { } func serve() { - // make timer to notify select to batch request insert from recordChan + // make timer to notify select to batch request insert from requestChan // interval: 30 seconds apiRequestTimer := time.NewTicker(time.Second * 30) // make timer to notify select to remove old analytics // interval: 2 weeks // TODO: enable analytics backup service to cloud - pruneDBTimer := time.NewTicker(time.Hour * 24 * 14) + pruneThreshold := time.Hour * 24 * 14 + pruneDBTimer := time.NewTicker(pruneThreshold) for { select { case <-apiRequestTimer.C: - var reqs []apiRequest - batchSize := len(recordChan) - - for i := 0; i < batchSize; i++ { - reqs = append(reqs, <-recordChan) - } - - err := batchInsert(reqs) + err := batchInsert(requestChan) if err != nil { log.Println(err) } case <-pruneDBTimer.C: + err := batchPrune(pruneThreshold) + if err != nil { + log.Println(err) + } case <-time.After(time.Second * 30): continue |