-
Notifications
You must be signed in to change notification settings - Fork 386
feat: optimize rpc calls #5394
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
Merged
Merged
feat: optimize rpc calls #5394
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
ad48778
feat: rpc calls optimisation
sbackend123 1da63cc
Merge branch 'master' into feat/rpc-calls-optimisation
sbackend123 68ecefb
fix: linter issues
sbackend123 04f6520
Merge branch 'master' into feat/rpc-calls-optimisation
sbackend123 1ff4f75
fix: enable cache metrics
sbackend123 095f4af
fix: review issues
sbackend123 e3b1aa5
fix: dead code
sbackend123 f8e3049
fix: update cache implementation
sbackend123 e6945ef
fix: wrapped backend test
sbackend123 a9fd888
fix: make linter happy
sbackend123 bed886c
chore: calculate blocks and sync with chain once in N seconds (#5422)
sbackend123 acabf46
Merge branch 'master' into feat/rpc-calls-optimisation
sbackend123 a1e3f83
fix: block sync interval flag
sbackend123 bf4a030
fix: review issues
sbackend123 54ee6af
fix: return dep
sbackend123 ba08feb
fix: remove extra metric test
sbackend123 2a11aa6
fix: simplify cache
sbackend123 e4befcf
fix: remove comments for linter
sbackend123 2e40f56
fix: metric naming
sbackend123 c7bd723
fix: remove expiresAt because there is no meaningful usage
sbackend123 76a6e1d
fix: prevent dividing to zero
sbackend123 ba744e9
fix: clean up
sbackend123 a858f42
fix: add env vars
sbackend123 433f3ff
fix: add new flag to all manifests
sbackend123 083da1f
Merge branch 'master' into feat/rpc-calls-optimisation
sbackend123 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| // Copyright 2026 The Swarm Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package cache | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
|
|
||
| "github.com/prometheus/client_golang/prometheus" | ||
| "resenje.org/singleflight" | ||
| ) | ||
|
|
||
| type Loader[T any] func() (T, error) | ||
| type ReuseEvaluator[T any] func(value T) bool | ||
|
|
||
| type SingleFlightCache[T any] struct { | ||
| mu sync.RWMutex | ||
| value T | ||
|
|
||
| group singleflight.Group[string, any] | ||
| key string | ||
| metrics metricSet | ||
| } | ||
|
|
||
| func NewSingleFlightCache[T any](metricsPrefix string) *SingleFlightCache[T] { | ||
| return &SingleFlightCache[T]{ | ||
| key: metricsPrefix, | ||
| metrics: newMetricSet(metricsPrefix), | ||
| } | ||
| } | ||
|
|
||
| func (c *SingleFlightCache[T]) Collectors() []prometheus.Collector { | ||
| return []prometheus.Collector{ | ||
| c.metrics.Hits, | ||
| c.metrics.Misses, | ||
| c.metrics.Loads, | ||
| c.metrics.SharedLoads, | ||
| c.metrics.LoadErrors, | ||
| } | ||
| } | ||
|
|
||
| func (c *SingleFlightCache[T]) Set(value T) { | ||
| c.mu.Lock() | ||
| defer c.mu.Unlock() | ||
|
|
||
| c.value = value | ||
| } | ||
|
|
||
| func (c *SingleFlightCache[T]) PeekOrLoad(ctx context.Context, canReuse ReuseEvaluator[T], loader Loader[T]) (T, error) { | ||
| c.mu.RLock() | ||
| value := c.value | ||
| c.mu.RUnlock() | ||
|
|
||
| if canReuse(value) { | ||
| c.metrics.Hits.Inc() | ||
| return value, nil | ||
| } | ||
|
|
||
| c.metrics.Misses.Inc() | ||
|
|
||
| result, shared, err := c.group.Do(ctx, c.key, func(ctx context.Context) (any, error) { | ||
| c.metrics.Loads.Inc() | ||
| value, err := loader() | ||
| if err != nil { | ||
| c.metrics.LoadErrors.Inc() | ||
| return value, err | ||
| } | ||
| c.Set(value) | ||
| return value, nil | ||
| }) | ||
|
|
||
| if shared { | ||
| c.metrics.SharedLoads.Inc() | ||
| } | ||
|
|
||
| if err != nil { | ||
| var zero T | ||
| return zero, err | ||
| } | ||
|
|
||
| return result.(T), nil | ||
| } |
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.
Please update the
packagingfolder with the new flag (indocker-compose,...)