Skip to content

Commit

Permalink
Feature: fallback node
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Sep 8, 2024
1 parent d45f50f commit ae2aeb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ datasources:
node:
url: ${STARKNET_NODE_URL}
rps: ${STARKNET_NODE_RPS:-5}
fallback:
url: ${STARKNET_FALLBACK_NODE_URL}
rps: ${STARKNET_FALLBACK_NODE_RPS:-1}

database:
kind: postgres
Expand Down
11 changes: 10 additions & 1 deletion pkg/indexer/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Result struct {
// Receiver -
type Receiver struct {
api API
fallbackAPI API
result chan Result
pool *workerpool.Pool[uint64]
processing map[uint64]struct{}
Expand Down Expand Up @@ -60,6 +61,11 @@ func NewReceiver(cfg config.Config, ds map[string]ddConfig.DataSource) (*Receive
wg: new(sync.WaitGroup),
}

fallbackDs, ok := ds["fallback"]
if ok {
receiver.fallbackAPI = NewNode(fallbackDs)
}

if receiver.timeout == 0 {
receiver.timeout = 10 * time.Second
}
Expand Down Expand Up @@ -131,20 +137,23 @@ func (r *Receiver) worker(ctx context.Context, height uint64) {
go func(mx *sync.Mutex, wg *sync.WaitGroup) {
defer wg.Done()

api := r.api
for {
select {
case <-ctx.Done():
return
default:
}

response, err := r.api.TraceBlock(ctx, blockId)
response, err := api.TraceBlock(ctx, blockId)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
r.log.Err(err).Uint64("height", height).Msg("get block traces request")
time.Sleep(time.Second)
r.log.Warn().Msg("trying fallback node...")
api = r.fallbackAPI
continue
}

Expand Down

0 comments on commit ae2aeb6

Please sign in to comment.