Skip to content
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

Merged
merged 5 commits into from
Apr 18, 2024

Conversation

deniseli
Copy link
Contributor

@deniseli deniseli commented Apr 18, 2024

Fixes issue #1295

@deniseli deniseli requested a review from a team as a code owner April 18, 2024 04:08
@deniseli deniseli requested review from worstell and removed request for a team April 18, 2024 04:08
@github-actions github-actions bot changed the title Create Singleton func for convenient singleton construction feat: Create Singleton func for convenient singleton construction Apr 18, 2024
@alecthomas alecthomas mentioned this pull request Apr 18, 2024

type InputLambda[T any] func(context.Context) (T, error)

type SingletonConstructor[T any] struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this SingletonHandle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

"sync"
)

type InputLambda[T any] func(context.Context) (T, error)
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, removed

type SingletonConstructor[T any] struct {
Fn InputLambda[T]
Out T
Once sync.Once
Copy link
Collaborator

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).

Copy link
Collaborator

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.

Copy link
Contributor Author

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 Show resolved Hide resolved
var sf SingletonConstructor[T]
sf.Fn = fn
return sf
}
Copy link
Collaborator

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{}}

Copy link
Contributor Author

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!

ctx := context.Background()
once := Singleton[string](func(ctx context.Context) (string, error) {
panic("test panic")
return "only once", nil
Copy link
Collaborator

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?

Copy link
Contributor Author

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!

@deniseli deniseli merged commit 794a835 into main Apr 18, 2024
11 checks passed
@deniseli deniseli deleted the dli/singleton branch April 18, 2024 17:20
@worstell worstell added the approved Marks an already closed PR as approved label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Marks an already closed PR as approved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants