-
Notifications
You must be signed in to change notification settings - Fork 8
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: Create Singleton func for convenient singleton construction #1297
Conversation
go-runtime/ftl/singleton.go
Outdated
|
||
type InputLambda[T any] func(context.Context) (T, error) | ||
|
||
type SingletonConstructor[T any] struct { |
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.
Call this SingletonHandle
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.
Fixed
go-runtime/ftl/singleton.go
Outdated
"sync" | ||
) | ||
|
||
type InputLambda[T any] func(context.Context) (T, error) |
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.
Let's not worry about the intermediate type - the signature is so simple that I think it just adds noise rather than reducing it.
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.
Sounds good, removed
go-runtime/ftl/singleton.go
Outdated
type SingletonConstructor[T any] struct { | ||
Fn InputLambda[T] | ||
Out T | ||
Once sync.Once |
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.
These should be private (lowercase).
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.
BTW with the Go LSP there's usually a "Rename symbol" option which will correctly rename the symbol everywhere it's used.
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.
Fixed
var sf SingletonConstructor[T] | ||
sf.Fn = fn | ||
return sf | ||
} |
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.
To solve this you'll need to make once sync.Once
into a pointer -once *sync.Once
and construct it when you're creating the handle:
return SingletonConstructor[T]{fn: fn, once: &sync.Once{}}
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.
Ahhhh that's a much nicer way of doing it - thanks!
go-runtime/ftl/singleton_test.go
Outdated
ctx := context.Background() | ||
once := Singleton[string](func(ctx context.Context) (string, error) { | ||
panic("test panic") | ||
return "only once", nil |
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.
This should return an error to invoke the panic in the singleton?
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.
Whoops, thanks for catching that!
Co-authored-by: Alec Thomas <[email protected]>
Fixes issue #1295