diff --git a/README.md b/README.md index 9c9b200..50a540c 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,8 @@ If the `CONFIG_FILE` environment variable is not set, the program will fall back - `CONFIG_GSHEET_ID` - Set this to your Google Sheet ID with the keys to whitelist. - `DELEGATION_WHITELIST_LIST` - Set this to your delegation whitelist sheet title where the whitelist keys are. - `DELEGATION_WHITELIST_COLUMN` - Set this to your delegation whitelist sheet column where the whitelist keys are. - - Or disable whitelisting alltogether by setting `DELEGATION_WHITELIST_DISABLED=1`. The previous four env variables are then ignored. + - `DELEGATION_WHITELIST_REFRESH_INTERVAL` - Whitelist refresh interval in minutes. If not set default value `10` is used. + - Or disable whitelisting alltogether by setting `DELEGATION_WHITELIST_DISABLED=1`. The previous env variables are then ignored. 3. **AWS S3 Configuration**: - `AWS_ACCOUNT_ID` - Your AWS Account ID. diff --git a/src/cmd/delegation_backend/main_bpu.go b/src/cmd/delegation_backend/main_bpu.go index 0fc0415..a4ab983 100644 --- a/src/cmd/delegation_backend/main_bpu.go +++ b/src/cmd/delegation_backend/main_bpu.go @@ -121,22 +121,30 @@ func main() { // Sheets service and whitelist loop app.WhitelistDisabled = appCfg.DelegationWhitelistDisabled if app.WhitelistDisabled { - log.Infof("delegation whitelist is disabled") + log.Infof("Delegation whitelist is disabled") } else { - log.Infof("delegation whitelist is enabled") sheetsService, err2 := sheets.NewService(ctx, option.WithScopes(sheets.SpreadsheetsReadonlyScope)) if err2 != nil { log.Fatalf("Error creating Sheets service: %v", err2) } - initWl := RetrieveWhitelist(sheetsService, log, appCfg) + initWl, err := RetrieveWhitelist(sheetsService, log, appCfg, 1) + if err != nil { + log.Fatalf("Failed to initialize whitelist: %v", err) + } wlMvar := new(WhitelistMVar) wlMvar.Replace(&initWl) app.Whitelist = wlMvar + log.Infof("Delegation whitelist is enabled") go func() { for { - wl := RetrieveWhitelist(sheetsService, log, appCfg) - wlMvar.Replace(&wl) - time.Sleep(WHITELIST_REFRESH_INTERVAL) + time.Sleep(SetWhitelistRefreshInterval(log)) + wl, err := RetrieveWhitelist(sheetsService, log, appCfg, 10) + if err != nil { + log.Errorf("Failed to refresh delegation whitelist, using previous one, error: %v", err) + } else { + wlMvar.Replace(&wl) + log.Infof("Delegation whitelist refreshed, number of BPs: %v", len(wl)) + } } }() } diff --git a/src/delegation_backend/constants.go b/src/delegation_backend/constants.go index 3793993..7f05c86 100644 --- a/src/delegation_backend/constants.go +++ b/src/delegation_backend/constants.go @@ -25,6 +25,25 @@ func NetworkId(networkName string) uint8 { return 0 } +func SetWhitelistRefreshInterval(log logging.StandardLogger) time.Duration { + var defaultValue time.Duration = time.Duration(10) * time.Minute + var whitelistRefreshInterval time.Duration + + envVarValue, exists := os.LookupEnv("DELEGATION_WHITELIST_REFRESH_INTERVAL") + if exists { + minutes, err := strconv.Atoi(envVarValue) + if err != nil { + log.Warnf("Error parsing DELEGATION_WHITELIST_REFRESH_INTERVAL, falling back to default value: %v, error: %v", defaultValue, err) + whitelistRefreshInterval = defaultValue + } + whitelistRefreshInterval = time.Duration(minutes) * time.Minute + } else { + whitelistRefreshInterval = defaultValue + } + log.Infof("Delegation whitelist refresh interval: %v", whitelistRefreshInterval) + return whitelistRefreshInterval +} + func SetRequestsPerPkHourly(log logging.StandardLogger) int { var defaultValue = 120 var requestsPerPkHourly int diff --git a/src/delegation_backend/sheets.go b/src/delegation_backend/sheets.go index ebc6261..a7dc0eb 100644 --- a/src/delegation_backend/sheets.go +++ b/src/delegation_backend/sheets.go @@ -27,7 +27,7 @@ func processRows(rows [][](interface{})) Whitelist { // Retrieve data from delegation program spreadsheet // and extract public keys out of the column containing // public keys of program participants. -func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, appCfg AppConfig) Whitelist { +func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, appCfg AppConfig, retries int) (Whitelist, error) { var resp *sheets.ValueRange var err error @@ -41,11 +41,11 @@ func RetrieveWhitelist(service *sheets.Service, log *logging.ZapEventLogger, app } return nil } - retries := 10 err = ExponentialBackoff(operation, retries, initialBackoff) if err != nil { log.Errorf("Unable to retrieve data from sheet after %v retries: %v", retries, err) + return nil, err } - return processRows(resp.Values) + return processRows(resp.Values), nil }