-
Notifications
You must be signed in to change notification settings - Fork 110
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
feat(pkg): add ticker
package
#2617
Conversation
Important Review skippedAuto incremental reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughWalkthroughThe changes implemented across the codebase introduce a new Changes
Sequence Diagram(s)sequenceDiagram
participant Observer
participant Ticker
participant Runner
Observer->>Ticker: New(interval, runner)
Ticker->>Runner: Execute immediately
loop Every interval
Ticker->>Runner: Execute
alt Error handling
Runner->>Ticker: Return error
Ticker->>Observer: Stop execution
end
alt Interval adjustment
Ticker->>Ticker: SetInterval(newInterval)
end
end
Ticker->>Observer: Stop signal received
Ticker->>Ticker: Cleanup
This diagram illustrates the interactions between the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #2617 +/- ##
===========================================
+ Coverage 70.55% 70.59% +0.04%
===========================================
Files 335 336 +1
Lines 18345 18384 +39
===========================================
+ Hits 12943 12978 +35
- Misses 4821 4823 +2
- Partials 581 583 +2
|
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.
Actionable comments posted: 12
Outside diff range, codebase verification and nitpick comments (7)
pkg/ticker/ticker.go (6)
1-11
: Improve package documentation.The package documentation is clear but can be enhanced by including information about thread safety and usage scenarios.
// Package ticker provides a dynamic ticker that can change its interval at runtime. // The ticker can be stopped gracefully and handles context-based termination. // // This package is useful for scenarios where periodic execution of a function is needed // and the interval might need to change dynamically based on runtime conditions. // // It also invokes a first tick immediately after the ticker starts. It's safe to use it concurrently. // // It also terminates gracefully when the context is done (return ctx.Err()) or when the stop signal is received. // // Example usage: // // ticker := New(time.Second, func(ctx context.Context, t *Ticker) error { // resp, err := client.GetPrice(ctx) // if err != nil { // logger.Err(err).Error().Msg("failed to get price") // return nil // } // // observer.SetPrice(resp.GasPrice) // t.SetInterval(resp.GasPriceInterval) // // return nil // }) // // err := ticker.Run(ctx)
38-39
: Clarify the Ticker struct documentation.The comment mentions that the ticker invokes "BEFORE" it starts, which is unclear. It should specify that the ticker invokes the runner function immediately upon starting.
// Ticker represents a ticker that will run a function periodically. // It also invokes the runner function immediately after the ticker starts.
46-47
: Clarify the purpose of runnerMu.The comment should clarify that
runnerMu
prevents concurrent runs of theRun
method.// runnerMu is a mutex to prevent concurrent runs of the Run method
49-50
: Clarify the purpose of stateMu.The comment should clarify that
stateMu
prevents concurrent calls toSetInterval
andStop
.// stateMu is a mutex to prevent concurrent calls to SetInterval and Stop
55-56
: Clarify the Runner type documentation.The comment should specify that the
Runner
function is called periodically by theTicker
.// Runner is a function that will be called periodically by the Ticker
68-71
: Improve utility function name.The function name
SecondsFromUint64
can be more descriptive. Consider renaming it toDurationFromSeconds
.-// SecondsFromUint64 converts uint64 to time.Duration in seconds. -func SecondsFromUint64(d uint64) time.Duration { +// DurationFromSeconds converts uint64 to time.Duration in seconds. +func DurationFromSeconds(d uint64) time.Duration {Tools
GitHub Check: codecov/patch
[warning] 69-70: pkg/ticker/ticker.go#L69-L70
Added lines #L69 - L70 were not covered by testspkg/ticker/ticker_test.go (1)
1-10
: Add missing imports.The test file is missing imports for
require
fromtestify
package, which is useful for making assertions.import ( "context" "fmt" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" )
Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Files selected for processing (3)
- pkg/ticker/ticker.go (1 hunks)
- pkg/ticker/ticker_test.go (1 hunks)
- zetaclient/chains/evm/observer/inbound.go (2 hunks)
Additional context used
Path-based instructions (3)
pkg/ticker/ticker.go (1)
Pattern
**/*.go
: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.pkg/ticker/ticker_test.go (1)
Pattern
**/*.go
: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.zetaclient/chains/evm/observer/inbound.go (1)
Pattern
**/*.go
: Review the Go code, point out issues relative to principles of clean code, expressiveness, and performance.
GitHub Check: codecov/patch
pkg/ticker/ticker.go
[warning] 69-70: pkg/ticker/ticker.go#L69-L70
Added lines #L69 - L70 were not covered by tests
[warning] 96-96: pkg/ticker/ticker.go#L96
Added line #L96 was not covered by tests
[warning] 120-120: pkg/ticker/ticker.go#L120
Added line #L120 was not covered by testszetaclient/chains/evm/observer/inbound.go
[warning] 48-51: zetaclient/chains/evm/observer/inbound.go#L48-L51
Added lines #L48 - L51 were not covered by tests
[warning] 53-53: zetaclient/chains/evm/observer/inbound.go#L53
Added line #L53 was not covered by tests
[warning] 55-56: zetaclient/chains/evm/observer/inbound.go#L55-L56
Added lines #L55 - L56 were not covered by tests
[warning] 60-61: zetaclient/chains/evm/observer/inbound.go#L60-L61
Added lines #L60 - L61 were not covered by tests
[warning] 64-65: zetaclient/chains/evm/observer/inbound.go#L64-L65
Added lines #L64 - L65 were not covered by tests
[warning] 67-67: zetaclient/chains/evm/observer/inbound.go#L67
Added line #L67 was not covered by tests
[warning] 70-75: zetaclient/chains/evm/observer/inbound.go#L70-L75
Added lines #L70 - L75 were not covered by tests
[warning] 77-77: zetaclient/chains/evm/observer/inbound.go#L77
Added line #L77 was not covered by tests
Additional comments not posted (2)
pkg/ticker/ticker.go (2)
127-139
: Optimize Stop method.The
Stop
method can be optimized by reducing the number of mutex locks and simplifying the logic.func (t *Ticker) Stop() { t.stateMu.Lock() defer t.stateMu.Unlock() // noop if t.stopped || t.signalChan == nil { return } close(t.signalChan) t.stopped = true }Likely invalid or redundant comment.
113-125
: Optimize SetInterval method.The
SetInterval
method can be optimized by reducing the number of mutex locks and simplifying the logic.func (t *Ticker) SetInterval(interval time.Duration) { t.stateMu.Lock() defer t.stateMu.Unlock() // noop if t.interval == interval || t.ticker == nil { return } t.interval = interval t.ticker.Reset(interval) }Likely invalid or redundant comment.
Tools
GitHub Check: codecov/patch
[warning] 120-120: pkg/ticker/ticker.go#L120
Added line #L120 was not covered by tests
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.
Trying to understand what is the motivation behind this package? Is there a specific functionality not present in the standard ticker/dynamic ticker that we needed ?
# Conflicts: # zetaclient/chains/evm/observer/inbound.go
|
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.
Lgtm ,
Can we add an issue to remove the DynamicTicker usage , if that's the next step for this task
* feat: parse inscription like witness data (#2524) * parse inscription like witness data * more comment * remove unused code * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * pull origin * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * review feedbacks * update review feedbacks * update make generate * fix linter * remove over flow * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * update review feedback * update code commnet * update comment * more comments * Update changelog.md --------- Co-authored-by: Dmitry S <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> fix version * feat: detect memo in btc txn from OP_RETURN and inscription (#2533) * parse inscription like witness data * more comment * remove unused code * parse inscription * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * pull origin * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * review feedbacks * update review feedbacks * add mainnet txn * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * parse inscription like witness data * more comment * remove unused code * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: Dmitry S <[email protected]> * Update zetaclient/chains/bitcoin/tx_script.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> * pull origin * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Dmitry S <[email protected]> * review feedbacks * update review feedbacks * update make generate * fix linter * remove over flow * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/tokenizer.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * update review feedback * update code commnet * update comment * more comments * Update changelog.md * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * Update zetaclient/chains/bitcoin/observer/inbound.go Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * clean up * format code --------- Co-authored-by: Dmitry S <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> * refactor(zetaclient)!: improve AppContext (#2568) * Implement chain registry * Rewrite test-cases for AppContext * Drop `supplychecker` * Refactor app ctx Update worker * Refactor orchestrator * Refactor observer&signer; DROP postBlockHeaders * Fix test cases [1] * Update changelog * Allow Zeta Chain in appContext; address PR comments [1] * Fix app context update * Check for `chain.IsZeta()` * Add AppContext.FilterChains * Fix test cases [2] * Fix test cases [3] * Address PR comments [1] * Address PR comments [2] * Add tests for `slices` * Fix e2e tests [1] * Fix e2e tests [2] * Resolve conflicts, converge codebase between PRs * Add lodash; remove slices pkg * Address PR comments * Minor logging fix * Address PR comments tmp * feat(zetaclient): add generic rpc metrics (#2597) * feat(zetaclient): add generic rpc metrics * feedback * changelog * fmt * fix(zetaclient): use name in pending tx metric (#2642) * feat(pkg): add `ticker` package (#2617) * Add `pkg/ticker` * Sample ticker usage in evm observer * Change naming * Address PR comments * Address PR comments * feat(zetaclient)!: Add support for EIP-1559 gas fees (#2634) * Add Gas struct * Add EIP-1559 fees * Update changelog * Add test cases for legacy vs dynamicFee txs * Fix typo; Add E2E coverage * Address PR comments * Address PR comments * Use gasFeeCap formula * Revert "Use gasFeeCap formula" This reverts commit 2260925. * Address PR comments * Fix e2e upgrade tests * fix: adjust evm outbound tracker reporter to avoid submitting invalid hashes (#2628) * refactor and fix evm outbound tracker reporter to avoid invalid hashes; print log when outbound tracker is full of invalid hashes * add changelog entry * used predefined log fields * remove repeated fields information from log message; Devops team would configure Datadog to show the fields * remove redundant fields in log message; unified logs * remove pending transaction map from observer; the outbound tracker reporter will no longer report pending hash * use bg.Work() to launch outbound tracker reporter goroutines * bring the checking EnsureNoTrackers() back * add more rationale to EVM outbound tracker submission * sync observer and signers without wait on startup * try fixing tss migration E2E failure by increase timeout * feat: Solana relayer (fee payer) key importer, encryption and decryption (#2673) * configure observer relayer key for Solana; remove hardcoded solana test key from zetaclient code * implementation of relayer key importer, encryption and decryption * integrate relayer key into E2E and Solana signer * add relayer_key_balance metrics and unit tests * use TrimSpace to trim password * add changelog entry * use relayer account array in E2E config; a few renaming; add private key validation when importing * fix linter * remove GetNetworkName method for simplification * added PromptPassword method to prompt single password * use network name as map index to store relayer key passwords * moved relayer passwords to chain registry * airdrop SOL token only if solana local node is available --------- Co-authored-by: Lucas Bertrand <[email protected]> * ci: Set Docker Workflow to use Go 1.22 (#2722) * Set go 1.22.2 * Set go 1.22.2 * Set go 1.22 * Set go 1.22 * Refactor contrib/rpc and contrib/docker-scripts to use snapshots API (#2724) Co-authored-by: Julian Rubino <[email protected]> --------- Co-authored-by: dev-bitSmiley <[email protected]> Co-authored-by: Dmitry S <[email protected]> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Francisco de Borja Aranda Castillejo <[email protected]> Co-authored-by: Charlie Chen <[email protected]> Co-authored-by: Lucas Bertrand <[email protected]> Co-authored-by: Charlie <[email protected]> Co-authored-by: Julian Rubino <[email protected]> Co-authored-by: Julian Rubino <[email protected]>
We have a lot of repetitive code chunks where we have the following logic:
foobar()
(first invokation)foobar()
everyinterval
interval
This PR implements exactly this logic with small and simple API in a declarative way.
Package
ticker
also handles context cancellation and panic recovery 😎Relates to #1611
before
after
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Refactor
WatchInbound
method to utilize the new ticker package, improving code readability and maintainability.