-
Notifications
You must be signed in to change notification settings - Fork 34
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
break: refreshable/v2 with Generic type handling #252
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
64aa0ff
break: refreshable/v2 with Generic type handling
bmoylan 3ac12b0
wip
bmoylan 34e6a19
Merge branch 'master' into bm/refreshable-v2
bmoylan 96db647
more
bmoylan dfa88d4
Merge branch 'master' into bm/refreshable-v2
bmoylan 615f9a9
updates
bmoylan 6fc9aa0
MapContext
bmoylan 61e1002
Merge branch 'master' into bm/refreshable-v2
bmoylan 92654cd
Merge branch 'master' into bm/refreshable-v2
bmoylan 7d46259
Merge branch 'master' into bm/refreshable-v2
bmoylan 477adc2
Mapped refreshables do not export Update
bmoylan bf2145b
Merge branch 'master' into bm/refreshable-v2
bmoylan 606674a
call subscribe function on registration
bmoylan 037e8cd
pass ctx to NewFromTickerFunc
bmoylan 41f56e1
comments
bmoylan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) 2022 Palantir Technologies. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package refreshable | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
type ready[T any] struct { | ||
in Updatable[T] | ||
readyC <-chan struct{} | ||
cancel context.CancelFunc | ||
} | ||
|
||
func newReady[T any](in Updatable[T]) *ready[T] { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
return &ready[T]{ | ||
in: in, | ||
readyC: ctx.Done(), | ||
cancel: cancel, | ||
} | ||
} | ||
|
||
func (r *ready[T]) Current() T { | ||
return r.in.Current() | ||
} | ||
|
||
func (r *ready[T]) Subscribe(consumer func(T)) UnsubscribeFunc { | ||
return r.in.Subscribe(consumer) | ||
} | ||
|
||
func (r *ready[T]) ReadyC() <-chan struct{} { | ||
return r.readyC | ||
} | ||
|
||
func (r *ready[T]) Update(val T) { | ||
r.cancel() | ||
r.in.Update(val) | ||
} | ||
|
||
// NewFromChannel populates an Updatable with the values channel. | ||
// If an element is already available, the returned Value is guaranteed to be populated. | ||
// The channel should be closed when no longer used to avoid leaking resources. | ||
func NewFromChannel[T any](values <-chan T) Ready[T] { | ||
out := newReady(newZero[T]()) | ||
select { | ||
case initial, ok := <-values: | ||
if !ok { | ||
return out // channel already closed | ||
} | ||
out.Update(initial) | ||
default: | ||
} | ||
go func() { | ||
for value := range values { | ||
out.Update(value) | ||
} | ||
}() | ||
return out | ||
} | ||
|
||
// NewFromTickerFunc returns a Ready Refreshable populated by the result of the provider called each interval. | ||
// If the providers bool return is false, the value is ignored. | ||
// The result's ReadyC channel is closed when a new value is populated. | ||
func NewFromTickerFunc[T any](interval time.Duration, provider func() (T, bool)) (Ready[T], UnsubscribeFunc) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we pass context into the provider function so that clients can make use of it (e.g. for logging unexpected behaviour) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done! |
||
out := newReady(newZero[T]()) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
values := make(chan T) | ||
go func() { | ||
ticker := time.NewTicker(interval) | ||
defer ticker.Stop() | ||
defer close(values) | ||
for { | ||
if value, ok := provider(); ok { | ||
out.Update(value) | ||
} | ||
select { | ||
case <-ticker.C: | ||
continue | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
return out, UnsubscribeFunc(cancel) | ||
} | ||
|
||
// Wait waits until the Ready has a current value or the context expires. | ||
func Wait[T any](ctx context.Context, ready Ready[T]) (T, bool) { | ||
select { | ||
case <-ready.ReadyC(): | ||
return ready.Current(), true | ||
case <-ctx.Done(): | ||
var zero T | ||
return zero, false | ||
} | ||
} |
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,4 +1,4 @@ | ||
module github.com/palantir/pkg/refreshable | ||
module github.com/palantir/pkg/refreshable/v2 | ||
|
||
go 1.19 | ||
|
||
|
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.
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.
Shouldn't the order here be reversed?
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.
yep, fixed, thanks!