feat(dispatch): queue async events durably

This commit is contained in:
2026-06-05 16:10:47 +08:00
parent 2cb02c8a91
commit dc48b5cd1f
6 changed files with 311 additions and 34 deletions
+55 -16
View File
@@ -2,12 +2,18 @@ package dispatch
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/gochat/gochat/internal/channel"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/worker"
applogger "github.com/gochat/gochat/pkg/logger"
)
const TaskTypeEventListenerDispatch = "event:listener_dispatch"
// EventDispatcher wraps the channel.Dispatcher and adds:
// - Sync/async split: sync listeners run immediately; async listeners are queued
// - Event name routing: listeners subscribe to specific event names
@@ -53,6 +59,7 @@ type EventDispatcher struct {
channelDispatcher *channel.Dispatcher
registry *ListenerRegistry
entries map[string]listenerEntry // listener name → entry
worker *worker.WorkerPool
}
// NewEventDispatcher creates a new EventDispatcher wrapping the given
@@ -65,6 +72,13 @@ func NewEventDispatcher(cd *channel.Dispatcher) *EventDispatcher {
}
}
func (ed *EventDispatcher) SetWorkerPool(wp *worker.WorkerPool) {
ed.worker = wp
if wp != nil {
wp.Register(TaskTypeEventListenerDispatch, ed.performListenerJob)
}
}
// RegisterSync adds a sync-mode listener for the given event names.
// If eventNames is empty, the listener receives all events.
func (ed *EventDispatcher) RegisterSync(listener channel.EventListener, eventNames ...string) {
@@ -112,14 +126,7 @@ func (ed *EventDispatcher) Dispatch(ctx context.Context, event *channel.ChannelE
}
}
} else {
// Async: run in background goroutine
go func(l channel.EventListener, e *channel.ChannelEvent) {
asyncCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := l.OnEvent(asyncCtx, e); err != nil {
applogger.L().Errorf("async listener %s error on event %s: %v", l.Name(), e.Type, err)
}
}(entry.listener, event)
ed.dispatchListenerAsync(ctx, entry.listener.Name(), event, "async listener")
}
}
@@ -136,16 +143,48 @@ func (ed *EventDispatcher) DispatchAsync(ctx context.Context, event *channel.Cha
if !ok {
continue
}
go func(l channel.EventListener, e *channel.ChannelEvent) {
asyncCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := l.OnEvent(asyncCtx, e); err != nil {
applogger.L().Errorf("async dispatch: listener %s error on event %s: %v", l.Name(), e.Type, err)
}
}(entry.listener, event)
ed.dispatchListenerAsync(ctx, entry.listener.Name(), event, "async dispatch")
}
}
type listenerJobPayload struct {
ListenerName string `json:"listener_name"`
Event *channel.ChannelEvent `json:"event"`
}
func (ed *EventDispatcher) dispatchListenerAsync(ctx context.Context, listenerName string, event *channel.ChannelEvent, logPrefix string) {
if ed.worker != nil {
payload := listenerJobPayload{ListenerName: listenerName, Event: event}
if _, err := ed.worker.Enqueue(ctx, TaskTypeEventListenerDispatch, payload, worker.WithQueue("events"), worker.WithMaxAttempts(3)); err != nil {
applogger.L().Errorf("%s %s enqueue error on event %s: %v", logPrefix, listenerName, event.Type, err)
}
return
}
entry := ed.entries[listenerName]
go func(l channel.EventListener, e *channel.ChannelEvent) {
asyncCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := l.OnEvent(asyncCtx, e); err != nil {
applogger.L().Errorf("%s: listener %s error on event %s: %v", logPrefix, l.Name(), e.Type, err)
}
}(entry.listener, event)
}
func (ed *EventDispatcher) performListenerJob(ctx context.Context, job *model.BackgroundJob) error {
var payload listenerJobPayload
if err := json.Unmarshal(job.Payload, &payload); err != nil {
return fmt.Errorf("unmarshal listener dispatch payload: %w", err)
}
entry, ok := ed.entries[payload.ListenerName]
if !ok {
return fmt.Errorf("listener %q not registered", payload.ListenerName)
}
if payload.Event == nil {
return fmt.Errorf("listener %q job missing event", payload.ListenerName)
}
return entry.listener.OnEvent(ctx, payload.Event)
}
// ChannelDispatcher returns the underlying channel.Dispatcher for direct access
// if needed (e.g. for channel-level dispatch without the enhanced routing).
func (ed *EventDispatcher) ChannelDispatcher() *channel.Dispatcher {
@@ -155,4 +194,4 @@ func (ed *EventDispatcher) ChannelDispatcher() *channel.Dispatcher {
// Registry returns the listener registry for inspection/testing.
func (ed *EventDispatcher) Registry() *ListenerRegistry {
return ed.registry
}
}
+107
View File
@@ -0,0 +1,107 @@
package dispatch
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/gochat/gochat/internal/channel"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/worker"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type dispatchWorkerListener struct {
name string
count atomic.Int32
}
func (l *dispatchWorkerListener) Name() string { return l.name }
func (l *dispatchWorkerListener) OnEvent(ctx context.Context, event *channel.ChannelEvent) error {
l.count.Add(1)
return nil
}
func newDispatchWorkerDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:dispatch-worker?mode=memory&cache=shared"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
sqlDB, err := db.DB()
if err != nil {
t.Fatalf("sqlite db handle: %v", err)
}
sqlDB.SetMaxOpenConns(1)
if err := db.AutoMigrate(&model.BackgroundJob{}); err != nil {
t.Fatalf("migrate background jobs: %v", err)
}
t.Cleanup(func() {
db.Exec("DELETE FROM background_jobs")
sqlDB.Close()
})
return db
}
func TestEventDispatcherQueuesAsyncListenersDurably(t *testing.T) {
db := newDispatchWorkerDB(t)
wp := worker.NewWorkerPoolWithOptions(db, worker.WithNow(func() time.Time { return time.Date(2026, 6, 5, 11, 30, 0, 0, time.UTC) }))
ed := NewEventDispatcher(channel.NewDispatcher())
ed.SetWorkerPool(wp)
syncListener := &dispatchWorkerListener{name: "sync-listener"}
asyncListener := &dispatchWorkerListener{name: "async-listener"}
ed.RegisterSync(syncListener, string(channel.EventConversationCreated))
ed.RegisterAsync(asyncListener, string(channel.EventConversationCreated))
event := channel.NewChannelEvent(channel.EventConversationCreated, channel.ChannelWebWidget, 1, 2)
if err := ed.Dispatch(context.Background(), event); err != nil {
t.Fatalf("dispatch: %v", err)
}
if syncListener.count.Load() != 1 {
t.Fatalf("sync listener should run immediately")
}
if asyncListener.count.Load() != 0 {
t.Fatalf("async listener should wait for durable worker")
}
var count int64
if err := db.Model(&model.BackgroundJob{}).Where("job_type = ? AND status = ?", TaskTypeEventListenerDispatch, model.BackgroundJobStatusQueued).Count(&count).Error; err != nil {
t.Fatalf("count jobs: %v", err)
}
if count != 1 {
t.Fatalf("expected one queued listener dispatch job, got %d", count)
}
processed, err := wp.ProcessOne(context.Background())
if err != nil || !processed {
t.Fatalf("process listener job: processed=%v err=%v", processed, err)
}
if asyncListener.count.Load() != 1 {
t.Fatalf("async listener was not called by worker")
}
}
func TestEventDispatcherDispatchAsyncQueuesAllMatchingListeners(t *testing.T) {
db := newDispatchWorkerDB(t)
wp := worker.NewWorkerPoolWithOptions(db, worker.WithNow(func() time.Time { return time.Date(2026, 6, 5, 11, 45, 0, 0, time.UTC) }))
ed := NewEventDispatcher(channel.NewDispatcher())
ed.SetWorkerPool(wp)
one := &dispatchWorkerListener{name: "one"}
two := &dispatchWorkerListener{name: "two"}
ed.RegisterSync(one, string(channel.EventMessageCreated))
ed.RegisterAsync(two, string(channel.EventMessageCreated))
ed.DispatchAsync(context.Background(), channel.NewChannelEvent(channel.EventMessageCreated, channel.ChannelAPI, 1, 2))
for i := 0; i < 2; i++ {
processed, err := wp.ProcessOne(context.Background())
if err != nil || !processed {
t.Fatalf("process listener job %d: processed=%v err=%v", i, processed, err)
}
}
if one.count.Load() != 1 || two.count.Load() != 1 {
t.Fatalf("expected both listeners via durable jobs, got one=%d two=%d", one.count.Load(), two.count.Load())
}
}