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: trim down on extraneous calls to ConsumeUntilEmpty in event consumers #1550

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions events/event_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const (
DefaultEventLogSize = 20
)

type Handler func(ctx context.Context, e models.Event) error
Copy link
Member Author

Choose a reason for hiding this comment

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

type Handler is not required anymore since we merged postq into duty.


var SyncHandlers = utils.SyncedMap[string, func(ctx context.Context, e models.Event) error]{}
var SyncHandlers = utils.SyncedMap[string, postq.SyncEventHandlerFunc]{}
var AsyncHandlers = utils.SyncedMap[string, asyncHandlerData]{}

var consumers []*postq.PGConsumer
Expand All @@ -49,7 +47,7 @@ func RegisterAsyncHandler(fn func(ctx context.Context, e models.Events) models.E
}
}

func RegisterSyncHandler(fn Handler, events ...string) {
func RegisterSyncHandler(fn postq.SyncEventHandlerFunc, events ...string) {
for _, event := range events {
SyncHandlers.Append(event, fn)
}
Expand All @@ -74,11 +72,11 @@ func StartConsumers(ctx context.Context) {
notifyRouter := pg.NewNotifyRouter()
go notifyRouter.Run(ctx, eventQueueUpdateChannel)

SyncHandlers.Each(func(event string, handlers []func(ctx context.Context, e models.Event) error) {
SyncHandlers.Each(func(event string, handlers []postq.SyncEventHandlerFunc) {
log.Tracef("Registering %d sync event handlers for %s", len(handlers), event)
consumer := postq.SyncEventConsumer{
WatchEvents: []string{event},
Consumers: postq.SyncHandlers(handlers...),
Consumers: handlers,
ConsumerOption: &postq.ConsumerOption{
ErrorHandler: defaultLoggerErrorHandler,
},
Expand All @@ -87,7 +85,7 @@ func StartConsumers(ctx context.Context) {
if ec, err := consumer.EventConsumer(); err != nil {
log.Fatalf("failed to create event consumer: %s", err)
} else {
pgsyncNotifyChannel := notifyRouter.GetOrCreateChannel(event)
pgsyncNotifyChannel := notifyRouter.GetOrCreateBufferedChannel(0, event)
consumers = append(consumers, ec)
go ec.Listen(ctx, pgsyncNotifyChannel)
}
Expand Down Expand Up @@ -124,7 +122,7 @@ func StartConsumers(ctx context.Context) {
if ec, err := consumer.EventConsumer(); err != nil {
log.Fatalf("failed to create event consumer: %s", err)
} else {
pgasyncNotifyChannel := notifyRouter.GetOrCreateChannel(event)
pgasyncNotifyChannel := notifyRouter.GetOrCreateBufferedChannel(handler.numConsumers, event)
consumers = append(consumers, ec)
go ec.Listen(ctx, pgasyncNotifyChannel)
}
Expand Down
Loading