Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVS-215, AVS-259: Add Subgraphs #233

Merged
merged 12 commits into from
May 29, 2024
48 changes: 48 additions & 0 deletions services/querier.go
Sidu28 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package thegraph

import (
"context"
"errors"
"time"
)

type RetryQuerier struct {
GraphQLQuerier
Backoff time.Duration
MaxRetries int
}

var _ GraphQLQuerier = (*RetryQuerier)(nil)

func NewRetryQuerier(q GraphQLQuerier, backoff time.Duration, maxRetries int) *RetryQuerier {
return &RetryQuerier{
GraphQLQuerier: q,
Backoff: backoff,
MaxRetries: maxRetries,
}
}

func (q *RetryQuerier) Query(ctx context.Context, query any, variables map[string]any) error {

retryCount := 0
backoff := q.Backoff
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if retryCount > q.MaxRetries {
return errors.New("max retries exceeded")
}
retryCount++

err := q.GraphQLQuerier.Query(ctx, query, variables)
if err == nil {
return nil
}

time.Sleep(backoff)
backoff *= 2
}
}
}
Loading