Skip to content

Commit

Permalink
Refactor the expiry time logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hellais committed Jun 1, 2018
1 parent 29e37af commit 17c8d9e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ func Start() {
return
}

// We setup the timers so that pending reports will expire the
// ExpiryTimeDuration after the server has been rebooted
for _, meta := range reportList {
report.ExpiryTimers[meta.ReportID] = time.AfterFunc(time.Duration(8)*time.Hour, func() {
report.ExpiryTimers[meta.ReportID] = time.AfterFunc(report.ExpiryTimeDuration, func() {
handler.CloseReport(store, meta.ReportID)
})
}
Expand Down
8 changes: 3 additions & 5 deletions collector/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ var log = apexLog.WithFields(apexLog.Fields{

const backendVersion = "2.0.0-alpha"

var reportExpiryTime = time.Duration(8) * time.Hour

func createNewReport(store *storage.Storage, req CreateReportRequest) (string, error) {
reportID := report.GenReportID(req.ProbeASN)
tmpPath := filepath.Join(paths.TempReportDir(), reportID)
Expand All @@ -44,7 +42,7 @@ func createNewReport(store *storage.Storage, req CreateReportRequest) (string, e
store.SetReport(&meta)
os.OpenFile(tmpPath, os.O_RDONLY|os.O_CREATE, 0700)

report.ExpiryTimers[reportID] = time.AfterFunc(reportExpiryTime, func() {
report.ExpiryTimers[reportID] = time.AfterFunc(report.ExpiryTimeDuration, func() {
CloseReport(store, reportID)
})

Expand Down Expand Up @@ -128,7 +126,7 @@ func addBackendExtra(meta *report.Metadata, entry *report.MeasurementEntry) {
}

func writeEntry(store *storage.Storage, entry *report.MeasurementEntry) error {
report.ExpiryTimers[entry.ReportID].Reset(reportExpiryTime)
report.ExpiryTimers[entry.ReportID].Reset(report.ExpiryTimeDuration)

meta, err := store.GetReport(entry.ReportID)
if err != nil {
Expand Down Expand Up @@ -212,7 +210,7 @@ func UpdateReportHandler(c *gin.Context) {

// CloseReport marks the report as closed and moves it into the final reports folder
func CloseReport(store *storage.Storage, reportID string) error {
report.ExpiryTimers[reportID].Reset(reportExpiryTime)
report.ExpiryTimers[reportID].Reset(report.ExpiryTimeDuration)

meta, err := store.GetReport(reportID)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions collector/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
// ensure that after a certain amount of time has elapsed reports are closed
var ExpiryTimers = make(map[string]*time.Timer)

// ExpiryTimeDuration is after how much time a report expires
var ExpiryTimeDuration = time.Duration(8) * time.Hour

// BackendExtra is serverside extra metadata
type BackendExtra struct {
SubmissionTime time.Time `json:"submission_time"`
Expand Down

0 comments on commit 17c8d9e

Please sign in to comment.