-
Notifications
You must be signed in to change notification settings - Fork 16
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
Add util methods from core #295
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a8f43a5
Add hex package
dimriou c6cac32
Improvements
dimriou b20725f
Minor fixes
dimriou ff77a2a
Merge branch 'main' into hex_package
dimriou b106ff6
Add util methods
dimriou e77cfff
Add AllEqual
dimriou 7759983
Add SleeperTask and DependentAwaiter
dimriou 2d2f173
Merge branch 'main' into add_utils
dimriou 9a9ca79
Fixes
dimriou 87231db
Fix WrapIfError
dimriou 32aed04
Add WrapIfError test
dimriou bfe59b5
replace gomega with chans (#300)
jmank88 27df2e1
Tidy
dimriou 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,22 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
) | ||
|
||
// DecodeTOML decodes toml from r in to v. | ||
// Requires strict field matches and returns full toml.StrictMissingError details. | ||
func DecodeTOML(r io.Reader, v any) error { | ||
d := toml.NewDecoder(r).DisallowUnknownFields() | ||
if err := d.Decode(v); err != nil { | ||
var strict *toml.StrictMissingError | ||
if errors.As(err, &strict) { | ||
return errors.New(strict.String()) | ||
} | ||
return err | ||
} | ||
return nil | ||
} |
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,129 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/services" | ||
) | ||
|
||
// SleeperTask represents a task that waits in the background to process some work. | ||
type SleeperTask interface { | ||
Stop() error | ||
WakeUp() | ||
WakeUpIfStarted() | ||
} | ||
jmank88 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Worker is a simple interface that represents some work to do repeatedly | ||
type Worker interface { | ||
Work() | ||
Name() string | ||
} | ||
|
||
type sleeperTask struct { | ||
services.StateMachine | ||
worker Worker | ||
chQueue chan struct{} | ||
chStop chan struct{} | ||
chDone chan struct{} | ||
chWorkDone chan struct{} | ||
} | ||
|
||
// NewSleeperTask takes a worker and returns a SleeperTask. | ||
// | ||
// SleeperTask is guaranteed to call Work on the worker at least once for every | ||
// WakeUp call. | ||
// If the Worker is busy when WakeUp is called, the Worker will be called again | ||
// immediately after it is finished. For this reason you should take care to | ||
// make sure that Worker is idempotent. | ||
// WakeUp does not block. | ||
func NewSleeperTask(worker Worker) SleeperTask { | ||
s := &sleeperTask{ | ||
worker: worker, | ||
chQueue: make(chan struct{}, 1), | ||
chStop: make(chan struct{}), | ||
chDone: make(chan struct{}), | ||
chWorkDone: make(chan struct{}, 10), | ||
} | ||
|
||
_ = s.StartOnce("SleeperTask-"+worker.Name(), func() error { | ||
go s.workerLoop() | ||
return nil | ||
}) | ||
|
||
return s | ||
} | ||
|
||
// Stop stops the SleeperTask | ||
func (s *sleeperTask) Stop() error { | ||
return s.StopOnce("SleeperTask-"+s.worker.Name(), func() error { | ||
close(s.chStop) | ||
select { | ||
case <-s.chDone: | ||
case <-time.After(15 * time.Second): | ||
return fmt.Errorf("SleeperTask-%s took too long to stop", s.worker.Name()) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func (s *sleeperTask) WakeUpIfStarted() { | ||
s.IfStarted(func() { | ||
select { | ||
case s.chQueue <- struct{}{}: | ||
default: | ||
} | ||
}) | ||
} | ||
|
||
// WakeUp wakes up the sleeper task, asking it to execute its Worker. | ||
func (s *sleeperTask) WakeUp() { | ||
if !s.IfStarted(func() { | ||
select { | ||
case s.chQueue <- struct{}{}: | ||
default: | ||
} | ||
}) { | ||
panic("cannot wake up stopped sleeper task") | ||
} | ||
} | ||
|
||
func (s *sleeperTask) workDone() { | ||
select { | ||
case s.chWorkDone <- struct{}{}: | ||
default: | ||
} | ||
} | ||
|
||
// WorkDone isn't part of the SleeperTask interface, but can be | ||
// useful in tests to assert that the work has been done. | ||
func (s *sleeperTask) WorkDone() <-chan struct{} { | ||
return s.chWorkDone | ||
} | ||
|
||
func (s *sleeperTask) workerLoop() { | ||
defer close(s.chDone) | ||
|
||
for { | ||
select { | ||
case <-s.chQueue: | ||
s.worker.Work() | ||
s.workDone() | ||
case <-s.chStop: | ||
return | ||
} | ||
} | ||
} | ||
|
||
type sleeperTaskWorker struct { | ||
name string | ||
work func() | ||
} | ||
|
||
// SleeperFuncTask returns a Worker to execute the given work function. | ||
func SleeperFuncTask(work func(), name string) Worker { | ||
return &sleeperTaskWorker{name: name, work: work} | ||
} | ||
|
||
func (w *sleeperTaskWorker) Name() string { return w.name } | ||
func (w *sleeperTaskWorker) Work() { w.work() } |
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.
Oh shoot I forgot to tidy the go.mod on my branch
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.
That's ok, I'll clean up