Skip to content

Commit

Permalink
added querier
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidu28 committed May 6, 2024
1 parent f6eb7a0 commit b66c56d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions services/querier.go
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
}
}
}

0 comments on commit b66c56d

Please sign in to comment.