-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
fix: 🐛 Reduce the cache ttl to 10-20s for gauge list #11196
Open
chef-ryan
wants to merge
12
commits into
develop
Choose a base branch
from
fix/gauge-voting-feb-12
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7b75885
fix: 🐛 fix cache issues of gauges
chef-ryan 96c2304
fix: 🐛 fix
chef-ryan 020ab29
fix: 🐛 update pnpm
chef-ryan 4da9639
fix: 🐛 fix pnpm
chef-ryan 8a84750
fix: 🐛 remove json stringify
chef-ryan b25b4a5
feat: 🎸 save
chef-ryan 835d5fd
fix
chef-ryan 2b1e619
update deps
chef-ryan 41f0793
fix: 🐛 fix killed gaguges
chef-ryan 7bf7afe
fix: 🐛 add killed display
chef-ryan 801265b
using stringify from viem
chef-ryan 2f4e28b
feat: 🎸 fix voting issue
chef-ryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { LRUCache } from 'lru-cache' | ||
import { keccak256 } from 'viem' | ||
|
||
type AsyncFunction<T extends any[]> = (...args: T) => Promise<any> | ||
|
||
// Type definitions for the cache. | ||
type CacheOptions<T extends AsyncFunction<any>> = { | ||
name?: string | ||
ttl: number | ||
key: (params: Parameters<T>) => any | ||
} | ||
|
||
function calcCacheKey(args: any[], epoch: number) { | ||
const json = JSON.stringify(args) | ||
const r = keccak256(`0x${json}@${epoch}`) | ||
return r | ||
} | ||
|
||
export const cacheByLRU = <T extends AsyncFunction<any>>(fn: T, { ttl, key }: CacheOptions<T>) => { | ||
const cache = new LRUCache<string, Promise<any>>({ | ||
max: 1000, | ||
ttl, | ||
}) | ||
|
||
// function logger(...args: any[]) { | ||
// const nameStr = `${name || 'def'}` | ||
// console.log(`[${nameStr}]`, ...args) | ||
// } | ||
|
||
let startTime = 0 | ||
return async (...args: Parameters<T>): Promise<ReturnType<T>> => { | ||
// Start Time | ||
if (!startTime) { | ||
startTime = Date.now() | ||
} | ||
const epoch = (Date.now() - startTime) / ttl | ||
const halfTTS = epoch % 1 > 0.5 | ||
const epochId = Math.floor(epoch) | ||
|
||
// Setup next epoch cache if halfTTS passed | ||
if (halfTTS) { | ||
const nextKey = calcCacheKey(key(args), epochId + 1) | ||
if (!cache.has(nextKey)) { | ||
const nextPromise = fn(...args) | ||
cache.set(nextKey, nextPromise) | ||
} | ||
} | ||
|
||
const cacheKey = calcCacheKey(key(args), epochId) | ||
// logger(cacheKey, `exists=${cache.has(cacheKey)}`) | ||
if (cache.has(cacheKey)) { | ||
// logger('cache hit', cacheKey) | ||
return cache.get(cacheKey) | ||
} | ||
|
||
const promise = fn(...args) | ||
|
||
cache.set(cacheKey, promise) | ||
// logger(`cache set`, cacheKey) | ||
|
||
try { | ||
return await promise | ||
} catch (error) { | ||
// logger('error', cacheKey, error) | ||
cache.delete(cacheKey) | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,31 @@ | ||
import { cacheByLRU } from '../../cacheByLRU' | ||
import { GaugeConfig } from '../../types' | ||
import { GAUGES_API } from './endpoint' | ||
|
||
function createGaugeConfigFetcher() { | ||
let gauges: GaugeConfig[] | undefined | ||
let fetchRequest: Promise<GaugeConfig[]> | undefined | ||
|
||
return async function getGaugeConfig() { | ||
if (fetchRequest) return fetchRequest | ||
const fetchGaugeConfig = async () => { | ||
if (gauges) { | ||
return gauges | ||
} | ||
try { | ||
const response = await fetch(GAUGES_API, { | ||
signal: AbortSignal.timeout(3000), | ||
}) | ||
if (response.ok) { | ||
gauges = await response.json() | ||
if (!gauges) { | ||
throw new Error(`Unexpected empty gauges fetched from remote ${gauges}`) | ||
} | ||
return gauges | ||
} | ||
throw new Error(`Fetch failed with status: ${response.status}`) | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
throw new Error(`Fetch failed: ${e.message}`) | ||
} else { | ||
throw new Error(`Fetch failed: ${e}`) | ||
} | ||
} finally { | ||
fetchRequest = undefined | ||
const fetchGaugeConfig = async () => { | ||
try { | ||
const response = await fetch(GAUGES_API, { | ||
signal: AbortSignal.timeout(3000), | ||
}) | ||
if (response.ok) { | ||
const gauges: GaugeConfig[] = await response.json() | ||
if (!gauges) { | ||
throw new Error(`Unexpected empty gauges fetched from remote`) | ||
} | ||
return gauges | ||
} | ||
throw new Error(`Fetch failed with status: ${response.status}`) | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
throw new Error(`Fetch failed: ${e.message}`) | ||
} else { | ||
throw new Error(`Fetch failed: ${e}`) | ||
} | ||
fetchRequest = fetchGaugeConfig() | ||
return fetchRequest | ||
} | ||
} | ||
|
||
export const getGauges = createGaugeConfigFetcher() | ||
export const getGauges = cacheByLRU(fetchGaugeConfig, { | ||
name: 'getGaugesConfig', | ||
ttl: 10000, | ||
key: () => [], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args is any type, use JSON.stringify would be broken if args with some complex data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
stringify
from viem, it can resolve bigint, which JSON.stringify cannot , and wrap with try...catchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx guys. will try stringify