Skip to content

Cache custom-query JSON responses to avoid API rate limits - #40

Merged
0xSpuddy merged 3 commits into
tellor-io:mainfrom
cryptoriums:feat/reporter-custom-query-cache
Jul 9, 2026
Merged

Cache custom-query JSON responses to avoid API rate limits#40
0xSpuddy merged 3 commits into
tellor-io:mainfrom
cryptoriums:feat/reporter-custom-query-cache

Conversation

@diorwave

Copy link
Copy Markdown
Contributor

No description provided.

@0xSpuddy 0xSpuddy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me. Offered a small waiting behavior fix, but should work well enough as is.

return nil, err
}
return v.([]byte), nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use sharedCache.group.DoChan instead of sharedCache.group.Do to avoid callers waiting longer than they need to when the leader's http call times out? (the code works as is, this is a small waiting behavior fix)

ch := sharedCache.group.DoChan(key, func() (interface{}, error) {
		// Re-check: a concurrent leader may have just populated the cache.
		if body, ok := sharedCache.get(key); ok {
			return body, nil
		}
		body, err := r.fetchWithRetry(ctx)
		if err != nil {
			return nil, err
		}
		sharedCache.set(key, body)
		return body, nil
	})

	select {
	case res := <-ch:
		if res.Err != nil {
			return nil, res.Err
		}
		return res.Val.([]byte), nil
	case <-ctx.Done():
		return nil, ctx.Err()
	}
}

@0xSpuddy 0xSpuddy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks cool, and it will be interesting to see if calls go down after the changes. Will merge into main now and then test it on palmito for a day before making a new release with all these pull requests in.

@0xSpuddy
0xSpuddy merged commit 0f7a23b into tellor-io:main Jul 9, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants