all the refactor
This commit is contained in:
+30
-43
@@ -23,7 +23,7 @@ import (
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/checkout"
|
||||
"git.k6n.net/mats/go-cart-actor/pkg/profile"
|
||||
"git.k6n.net/mats/go-redis-inventory/pkg/inventory"
|
||||
"git.k6n.net/mats/slask-finder/pkg/messaging"
|
||||
"git.k6n.net/mats/platform/rabbit"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
@@ -159,13 +159,12 @@ func (a *App) updateInventory(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Start runs the WebSocket hub loop and, when conn is non-nil, the RabbitMQ
|
||||
// mutation consumer that broadcasts cart mutations to connected clients. The
|
||||
// background goroutines stop when ctx is cancelled.
|
||||
func (a *App) Start(ctx context.Context, conn *amqp.Connection) error {
|
||||
// consumer re-subscribes automatically on reconnect (conn is a managed
|
||||
// reconnecting connection). Background goroutines stop when ctx is cancelled.
|
||||
func (a *App) Start(ctx context.Context, conn *rabbit.Conn) error {
|
||||
go a.hub.Run()
|
||||
if conn != nil {
|
||||
if err := startMutationConsumer(ctx, conn, a.hub); err != nil {
|
||||
return err
|
||||
}
|
||||
startMutationConsumer(ctx, conn, a.hub)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -177,44 +176,32 @@ func (a *App) Shutdown(_ context.Context) error {
|
||||
}
|
||||
|
||||
// startMutationConsumer subscribes to the cart "mutation" topic and forwards
|
||||
// each message to the hub for broadcast over WebSocket. Best-effort: a full hub
|
||||
// queue drops the message rather than blocking.
|
||||
func startMutationConsumer(ctx context.Context, conn *amqp.Connection, hub *Hub) error {
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
return err
|
||||
}
|
||||
msgs, err := messaging.DeclareBindAndConsume(ch, "cart", "mutation")
|
||||
if err != nil {
|
||||
_ = ch.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer ch.Close()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case m, ok := <-msgs:
|
||||
if !ok {
|
||||
log.Printf("mutation consumer: channel closed")
|
||||
return
|
||||
}
|
||||
log.Printf("mutation event: %s", string(m.Body))
|
||||
if hub != nil {
|
||||
select {
|
||||
case hub.broadcast <- m.Body:
|
||||
default:
|
||||
// hub queue full: drop to avoid blocking
|
||||
}
|
||||
}
|
||||
if err := m.Ack(false); err != nil {
|
||||
log.Printf("error acknowledging message: %v", err)
|
||||
// each message to the hub for broadcast over WebSocket. It (re)subscribes on the
|
||||
// initial connect and on every reconnect — via conn.NotifyOnReconnect — so a
|
||||
// broker blip doesn't silently kill the feed. Best-effort: a full hub queue
|
||||
// drops the message rather than blocking.
|
||||
func startMutationConsumer(ctx context.Context, conn *rabbit.Conn, hub *Hub) {
|
||||
conn.NotifyOnReconnect(func() {
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
log.Printf("mutation consumer: open channel: %v", err)
|
||||
return
|
||||
}
|
||||
// Subscribe to every grain's mutation stream (mutation.#) on the shared
|
||||
// "mutations" exchange. Bodies are platform/event.Event envelopes
|
||||
// (Source=grain, Subject=id, Payload=mutation summary) — forwarded raw to
|
||||
// WebSocket clients, which is exactly what a debug feed wants.
|
||||
if err := rabbit.ListenToPattern(ch, actor.MutationExchange, "mutation.#", func(d amqp.Delivery) error {
|
||||
if hub != nil {
|
||||
select {
|
||||
case hub.broadcast <- d.Body:
|
||||
default:
|
||||
// hub queue full: drop to avoid blocking
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Printf("mutation consumer: subscribe: %v", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user