From feaa07e251a9dc8ca70a07081044f678c9f6ff30 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Mon, 13 Nov 2023 14:55:48 -0700 Subject: [PATCH] add rate limit for refresh --- main.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/main.go b/main.go index 0193cae..9ac3d68 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( var ( configuredProviders []AccountProvider // Any account providers that are successfully configured get added to this slice ynabClient *ynab.Client // YNAB HTTP client + lastRefresh time.Time ) // Called at program startup or if SIGHUP is received @@ -61,6 +62,14 @@ func main() { } func refreshData() { + + // Only allow a refresh at most once every 5 minutes + if time.Now().Before(lastRefresh.Add(time.Minute * 5)) { + log.Printf("refresh rate limited") + return + } + lastRefresh = time.Now() + // Loop through each configured account provider and attempt to get the account balances, and update YNAB for _, p := range configuredProviders { balances, accountIDs, err := p.GetBalances()