@@ -19,11 +19,9 @@ import (
1919 "github.com/thirdweb-dev/indexer/internal/worker"
2020)
2121
22- const DEFAULT_COMMITTER_TRIGGER_INTERVAL = 2000
2322const DEFAULT_BLOCKS_PER_COMMIT = 1000
2423
2524type Committer struct {
26- triggerIntervalMs int
2725 blocksPerCommit int
2826 storage storage.IStorage
2927 commitFromBlock * big.Int
@@ -39,11 +37,6 @@ type Committer struct {
3937type CommitterOption func (* Committer )
4038
4139func NewCommitter (rpc rpc.IRPCClient , storage storage.IStorage , poller * Poller , opts ... CommitterOption ) * Committer {
42- triggerInterval := config .Cfg .Committer .Interval
43- if triggerInterval == 0 {
44- triggerInterval = DEFAULT_COMMITTER_TRIGGER_INTERVAL
45- }
46-
4740 blocksPerCommit := config .Cfg .Committer .BlocksPerCommit
4841 if blocksPerCommit == 0 {
4942 blocksPerCommit = DEFAULT_BLOCKS_PER_COMMIT
@@ -56,7 +49,6 @@ func NewCommitter(rpc rpc.IRPCClient, storage storage.IStorage, poller *Poller,
5649
5750 commitFromBlock := big .NewInt (int64 (config .Cfg .Committer .FromBlock ))
5851 committer := & Committer {
59- triggerIntervalMs : triggerInterval ,
6052 blocksPerCommit : blocksPerCommit ,
6153 storage : storage ,
6254 commitFromBlock : commitFromBlock ,
@@ -78,8 +70,6 @@ func NewCommitter(rpc rpc.IRPCClient, storage storage.IStorage, poller *Poller,
7870}
7971
8072func (c * Committer ) Start (ctx context.Context ) {
81- interval := time .Duration (c .triggerIntervalMs ) * time .Millisecond
82-
8373 log .Debug ().Msgf ("Committer running" )
8474 chainID := c .rpc .GetChainID ()
8575
@@ -175,40 +165,35 @@ func (c *Committer) Start(ctx context.Context) {
175165
176166 if config .Cfg .Publisher .Mode == "parallel" {
177167 var wg sync.WaitGroup
178- publishInterval := interval / 2
179- if publishInterval <= 0 {
180- publishInterval = interval
181- }
182168 wg .Add (2 )
169+
183170 go func () {
184171 defer wg .Done ()
185- c .runPublishLoop (ctx , publishInterval )
172+ c .runPublishLoop (ctx )
186173 }()
187174
188- // allow the publisher to start before the committer
189- time .Sleep (publishInterval )
190175 go func () {
191176 defer wg .Done ()
192- c .runCommitLoop (ctx , interval )
177+ c .runCommitLoop (ctx )
193178 }()
194179
195180 <- ctx .Done ()
181+
196182 wg .Wait ()
197183 } else {
198- c .runCommitLoop (ctx , interval )
184+ c .runCommitLoop (ctx )
199185 }
200186
201187 log .Info ().Msg ("Committer shutting down" )
202188 c .publisher .Close ()
203189}
204190
205- func (c * Committer ) runCommitLoop (ctx context.Context , interval time. Duration ) {
191+ func (c * Committer ) runCommitLoop (ctx context.Context ) {
206192 for {
207193 select {
208194 case <- ctx .Done ():
209195 return
210196 default :
211- time .Sleep (interval )
212197 if c .commitToBlock .Sign () > 0 && c .lastCommittedBlock .Load () >= c .commitToBlock .Uint64 () {
213198 // Completing the commit loop if we've committed more than commit to block
214199 log .Info ().Msgf ("Committer reached configured toBlock %s, the last commit block is %d, stopping commits" , c .commitToBlock .String (), c .lastCommittedBlock .Load ())
@@ -231,13 +216,17 @@ func (c *Committer) runCommitLoop(ctx context.Context, interval time.Duration) {
231216 }
232217}
233218
234- func (c * Committer ) runPublishLoop (ctx context.Context , interval time. Duration ) {
219+ func (c * Committer ) runPublishLoop (ctx context.Context ) {
235220 for {
236221 select {
237222 case <- ctx .Done ():
238223 return
239224 default :
240- time .Sleep (interval )
225+ if c .commitToBlock .Sign () > 0 && c .lastPublishedBlock .Load () >= c .commitToBlock .Uint64 () {
226+ // Completing the publish loop if we've published more than commit to block
227+ log .Info ().Msgf ("Committer reached configured toBlock %s, the last publish block is %d, stopping publishes" , c .commitToBlock .String (), c .lastPublishedBlock .Load ())
228+ return
229+ }
241230 if err := c .publish (ctx ); err != nil {
242231 log .Error ().Err (err ).Msg ("Error publishing blocks" )
243232 }
@@ -397,8 +386,8 @@ func (c *Committer) getBlockToCommitUntil(ctx context.Context, latestCommittedBl
397386func (c * Committer ) fetchBlockData (ctx context.Context , blockNumbers []* big.Int ) ([]common.BlockData , error ) {
398387 blocksData := c .poller .Request (ctx , blockNumbers )
399388 if len (blocksData ) == 0 {
400- // TODO: should wait a little bit, as it may take time to load
401- log . Warn (). Msgf ( "Committer didn't find the following range: %v - %v" , blockNumbers [ 0 ]. Int64 (), blockNumbers [ len ( blockNumbers ) - 1 ]. Int64 ())
389+ log . Warn (). Msgf ( "Committer didn't find the following range: %v - %v. %v" , blockNumbers [ 0 ]. Int64 (), blockNumbers [ len ( blockNumbers ) - 1 ]. Int64 (), c . poller . GetPollerStatus ())
390+ time . Sleep ( 500 * time . Millisecond ) // TODO: wait for block time
402391 return nil , nil
403392 }
404393 return blocksData , nil
0 commit comments