-
Notifications
You must be signed in to change notification settings - Fork 259
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
pubsub: add MethodHandler #739
Merged
Merged
Changes from all commits
Commits
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,52 @@ | ||
run | ||
publish pointer '{"Data": "test"}' | ||
publish non-pointer '{"Data": "test"}' | ||
checklog '{"topic": "pointer", "subscription": "pointer", "event": {"Data": "test"}, "message": "pointer method"}' | ||
checklog '{"topic": "non-pointer", "subscription": "non-pointer", "event": {"Data": "test"}, "message": "non-pointer method"}' | ||
|
||
|
||
-- svc/svc.go -- | ||
package svc | ||
|
||
import ( | ||
"context" | ||
"encore.dev/rlog" | ||
"encore.dev/pubsub" | ||
) | ||
|
||
//encore:service | ||
type Service struct{} | ||
|
||
func (s *Service) PointerMethod(ctx context.Context, event *Event) error { | ||
rlog.Info("pointer method", "event", event) | ||
return nil | ||
} | ||
|
||
func (s Service) NonPointerMethod(ctx context.Context, event *Event) error { | ||
rlog.Info("non-pointer method", "event", event) | ||
return nil | ||
} | ||
|
||
type Event struct { | ||
Data string | ||
} | ||
|
||
var Pointer = pubsub.NewTopic[*Event]("pointer", pubsub.TopicConfig{ | ||
DeliveryGuarantee: pubsub.AtLeastOnce, | ||
}) | ||
|
||
var NonPointer = pubsub.NewTopic[*Event]("non-pointer", pubsub.TopicConfig{ | ||
DeliveryGuarantee: pubsub.AtLeastOnce, | ||
}) | ||
|
||
var _ = pubsub.NewSubscription(Pointer, "pointer", | ||
pubsub.SubscriptionConfig[*Event]{ | ||
Handler: pubsub.MethodHandler((*Service).PointerMethod), | ||
}, | ||
) | ||
|
||
var _ = pubsub.NewSubscription(NonPointer, "non-pointer", | ||
pubsub.SubscriptionConfig[*Event]{ | ||
Handler: pubsub.MethodHandler(Service.NonPointerMethod), | ||
}, | ||
) |
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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ package pubsub | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
|
@@ -49,7 +50,7 @@ type Subscription[T any] struct { | |
// | ||
// var Subscription = pubsub.NewSubscription(MyTopic, "my-subscription", pubsub.SubscriptionConfig[*MyEvent]{ | ||
// Handler: HandleEvent, | ||
// RetryPolicy: &pubsub.RetryPolicy { MaxRetries: 10 }, | ||
// RetryPolicy: &pubsub.RetryPolicy{MaxRetries: 10}, | ||
// }) | ||
// | ||
// func HandleEvent(ctx context.Context, event *MyEvent) error { | ||
|
@@ -278,3 +279,25 @@ func marshalParams[Resp any](json jsoniter.API, resp Resp) []byte { | |
data, _ := json.Marshal(resp) | ||
return data | ||
} | ||
|
||
// MethodHandler is used to define a subscription Handler that references a service struct method. | ||
// | ||
// Example Usage: | ||
// | ||
// //encore:service | ||
// type Service struct {} | ||
// | ||
// func (s *Service) Method(ctx context.Context, msg *Event) error { /* ... */ } | ||
// | ||
// var _ = pubsub.NewSubscription(Topic, "subscription-name", pubsub.SubscriptionConfig[*Event]{ | ||
// Handler: pubsub.MethodHandler((*MyService).MyMethod), | ||
// // ... | ||
// }) | ||
func MethodHandler[T, SvcStruct any](handler func(s SvcStruct, ctx context.Context, msg T) error) func(ctx context.Context, msg T) error { | ||
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. ❤️ Perfect, adding support while keeping type satefy in the IDE |
||
// The use of MethodHandler acts as a sentinel for the code generator, | ||
// which replaces the call with some generated code to initialize the service struct. | ||
// As such this function should never be called in practice. | ||
return func(ctx context.Context, msg T) error { | ||
return fmt.Errorf("pubsub.MethodHandler is not usable in this context") | ||
} | ||
} |
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.
Is there a better way to do this, than "100ms sleep" - this seems like we're just asking for trouble in the future; could there not be a
apisdk.WaitForInit()
method, which returns true once all services are init'ed