From 490ec2b1b3593dd6a5f8c31846d7195a6036154f Mon Sep 17 00:00:00 2001 From: Ryan Collingham Date: Wed, 13 Mar 2024 09:55:05 +0000 Subject: [PATCH 1/2] Change log text for backoff retry notice --- indexer/blocks.go | 2 +- indexer/logs.go | 2 +- indexer/transactions.go | 2 +- main.go | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/indexer/blocks.go b/indexer/blocks.go index 06937c5..4cff7e9 100644 --- a/indexer/blocks.go +++ b/indexer/blocks.go @@ -43,7 +43,7 @@ func (ci *BlockIndexer) fetchBlock(ctx context.Context, index *uint64) (block *t }, bOff, func(err error, d time.Duration) { - logger.Debug("BlockByNumber error: %s after %s", err, d) + logger.Debug("BlockByNumber error: %s. Will retry after %s", err, d) }, ) diff --git a/indexer/logs.go b/indexer/logs.go index fd45489..5f41385 100644 --- a/indexer/logs.go +++ b/indexer/logs.go @@ -84,7 +84,7 @@ func (ci *BlockIndexer) fetchLogsChunk( }, bOff, func(err error, d time.Duration) { - logger.Debug("FilterLogs error: %s after %s", err, d) + logger.Debug("FilterLogs error: %s. Will retry after %s", err, d) }, ) if err != nil { diff --git a/indexer/transactions.go b/indexer/transactions.go index 20ae003..89ecb81 100644 --- a/indexer/transactions.go +++ b/indexer/transactions.go @@ -79,7 +79,7 @@ func (ci *BlockIndexer) getTransactionsReceipt( }, bOff, func(err error, d time.Duration) { - logger.Error("TransactionReceipt error: %s after %s", err, d) + logger.Error("TransactionReceipt error: %s. Will retry after %s", err, d) }, ) diff --git a/main.go b/main.go index 4c6dfe9..ac4c8c1 100644 --- a/main.go +++ b/main.go @@ -80,7 +80,7 @@ func runIndexer(ctx context.Context, cfg *config.Config, db *gorm.DB, ethClient }, bOff, func(err error, d time.Duration) { - logger.Error("Index history error: %s after %s", err, d) + logger.Error("Index history error: %s. Will retry after %s", err, d) }, ) if err != nil { @@ -99,7 +99,7 @@ func runIndexer(ctx context.Context, cfg *config.Config, db *gorm.DB, ethClient }, bOff, func(err error, d time.Duration) { - logger.Error("Index continuous error: %s after %s", err, d) + logger.Error("Index continuous error: %s. Will retry after %s", err, d) }, ) if err != nil { From 15302debb0b1bc2ac7166dfa361b4ee21fca198f Mon Sep 17 00:00:00 2001 From: Ryan Collingham Date: Wed, 13 Mar 2024 10:38:10 +0000 Subject: [PATCH 2/2] add configuration option for timeout control --- README.md | 4 ++++ config.toml | 4 ++++ config/config.go | 27 ++++++++++++++++++++++++++- database/history_drop.go | 2 +- indexer/blocks.go | 2 +- indexer/logs.go | 2 +- indexer/transactions.go | 2 +- 7 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fbd7a3a..a0f9720 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,10 @@ console = true [chain] node_url = "http://127.0.0.1:8545/" + +[timeout] +backoff_max_elapsed_time_seconds = 300 # optional, defaults to 300s = 5 minutes. Affects how long the indexer will keep retrying in case of a complete outage of the node provider. Set to 0 to retry indefinitely. +timeout_milis = 1000 # optional, defaults to 1000ms = 1s. Try increasing if you see timeout errors often. ``` ### Database diff --git a/config.toml b/config.toml index 592e3b5..716dfed 100644 --- a/config.toml +++ b/config.toml @@ -60,3 +60,7 @@ console = true [chain] node_url = "http://127.0.0.1:8545/" + +[timeout] +backoff_max_elapsed_time_seconds = 300 +timeout_milis = 1000 diff --git a/config/config.go b/config/config.go index df22341..cac852e 100644 --- a/config/config.go +++ b/config/config.go @@ -13,13 +13,28 @@ import ( var ( BackoffMaxElapsedTime time.Duration = 5 * time.Minute - DefaultTimeout time.Duration = 1000 * time.Millisecond + Timeout time.Duration = 1000 * time.Millisecond GlobalConfigCallback ConfigCallback[GlobalConfig] = ConfigCallback[GlobalConfig]{} CfgFlag = flag.String("config", "config.toml", "Configuration file (toml format)") ) +func init() { + GlobalConfigCallback.AddCallback(func(config GlobalConfig) { + tCfg := config.TimeoutConfig() + + if tCfg.BackoffMaxElapsedTimeSeconds != nil { + BackoffMaxElapsedTime = time.Duration(*tCfg.BackoffMaxElapsedTimeSeconds) * time.Second + } + + if tCfg.TimeoutMillis > 0 { + Timeout = time.Duration(tCfg.TimeoutMillis) * time.Millisecond + } + }) +} + type GlobalConfig interface { LoggerConfig() LoggerConfig + TimeoutConfig() TimeoutConfig } type Config struct { @@ -27,6 +42,7 @@ type Config struct { Logger LoggerConfig `toml:"logger"` Chain ChainConfig `toml:"chain"` Indexer IndexerConfig `toml:"indexer"` + Timeout TimeoutConfig `toml:"timeout"` } type LoggerConfig struct { @@ -63,6 +79,11 @@ type IndexerConfig struct { CollectLogs []LogInfo `toml:"collect_logs"` } +type TimeoutConfig struct { + BackoffMaxElapsedTimeSeconds *int `toml:"backoff_max_elapsed_time_seconds"` + TimeoutMillis int `toml:"timeout_millis"` +} + type TransactionInfo struct { ContractAddress string `toml:"contract_address"` FuncSig string `toml:"func_sig"` @@ -106,6 +127,10 @@ func (c Config) LoggerConfig() LoggerConfig { return c.Logger } +func (c Config) TimeoutConfig() TimeoutConfig { + return c.Timeout +} + func (cc ChainConfig) FullNodeURL() (*url.URL, error) { u, err := url.Parse(cc.NodeURL) if err != nil { diff --git a/database/history_drop.go b/database/history_drop.go index d73b6e4..0e4df0d 100644 --- a/database/history_drop.go +++ b/database/history_drop.go @@ -114,7 +114,7 @@ func getBlockTimestamp(ctx context.Context, index *big.Int, client *ethclient.Cl var block *types.Block err := backoff.Retry( func() (err error) { - ctx, cancelFunc := context.WithTimeout(ctx, config.DefaultTimeout) + ctx, cancelFunc := context.WithTimeout(ctx, config.Timeout) defer cancelFunc() block, err = client.BlockByNumber(ctx, index) diff --git a/indexer/blocks.go b/indexer/blocks.go index 4cff7e9..980d86c 100644 --- a/indexer/blocks.go +++ b/indexer/blocks.go @@ -35,7 +35,7 @@ func (ci *BlockIndexer) fetchBlock(ctx context.Context, index *uint64) (block *t err = backoff.RetryNotify( func() error { - ctx, cancelFunc := context.WithTimeout(ctx, config.DefaultTimeout) + ctx, cancelFunc := context.WithTimeout(ctx, config.Timeout) defer cancelFunc() block, err = ci.client.BlockByNumber(ctx, indexBigInt) diff --git a/indexer/logs.go b/indexer/logs.go index 5f41385..68aea46 100644 --- a/indexer/logs.go +++ b/indexer/logs.go @@ -75,7 +75,7 @@ func (ci *BlockIndexer) fetchLogsChunk( err := backoff.RetryNotify( func() error { - ctx, cancelFunc := context.WithTimeout(ctx, config.DefaultTimeout) + ctx, cancelFunc := context.WithTimeout(ctx, config.Timeout) defer cancelFunc() var err error diff --git a/indexer/transactions.go b/indexer/transactions.go index 89ecb81..e4d9030 100644 --- a/indexer/transactions.go +++ b/indexer/transactions.go @@ -71,7 +71,7 @@ func (ci *BlockIndexer) getTransactionsReceipt( if policy.status || policy.collectEvents { err := backoff.RetryNotify( func() (err error) { - ctx, cancelFunc := context.WithTimeout(ctx, config.DefaultTimeout) + ctx, cancelFunc := context.WithTimeout(ctx, config.Timeout) defer cancelFunc() receipt, err = ci.client.TransactionReceipt(ctx, tx.Hash())