Skip to content

Commit

Permalink
reset rate limit after sending heartbeats
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Jul 23, 2024
1 parent 5f73459 commit abba4cf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/heartbeat/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/wakatime/wakatime-cli/pkg/filestats"
"github.com/wakatime/wakatime-cli/pkg/filter"
"github.com/wakatime/wakatime-cli/pkg/heartbeat"
"github.com/wakatime/wakatime-cli/pkg/ini"
"github.com/wakatime/wakatime-cli/pkg/language"
_ "github.com/wakatime/wakatime-cli/pkg/lexer" // force to load all lexers
"github.com/wakatime/wakatime-cli/pkg/log"
Expand Down Expand Up @@ -153,6 +154,8 @@ func SendHeartbeats(v *viper.Viper, queueFilepath string) error {
}
}

ResetRateLimit(v)

return nil
}

Expand Down Expand Up @@ -197,6 +200,24 @@ func RateLimited(offline paramscmd.Offline) bool {
return time.Since(offline.LastSentAt) > offline.RateLimit
}

// ResetRateLimit updates the internal.heartbeats_last_sent_at timestamp
func ResetRateLimit(v *viper.Viper) error {
w, err := ini.NewWriter(v, ini.InternalFilePath)
if err != nil {
return fmt.Errorf("failed to parse config file: %s", err)
}

keyValue := map[string]string{
"heartbeats_last_sent_at": time.Now().Format(ini.DateFormat),
}

if err := w.Write("internal", keyValue); err != nil {
return fmt.Errorf("failed to write to internal config file: %s", err)
}

return nil
}

func buildHeartbeats(params paramscmd.Params) []heartbeat.Heartbeat {
heartbeats := []heartbeat.Heartbeat{}

Expand Down
2 changes: 2 additions & 0 deletions cmd/offlinesync/offlinesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,7 @@ func SyncOfflineActivity(v *viper.Viper, queueFilepath string) error {
return err
}

cmdheartbeat.ResetRateLimit(v)

return nil
}

0 comments on commit abba4cf

Please sign in to comment.