Files
pipelines/internal/server/attention_acks.go
T

91 lines
3.0 KiB
Go

package server
import (
"context"
"sync"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// attentionAckStore persists the shared in-app acknowledgements.
// *pipeline.Store implements it.
type attentionAckStore interface {
AttentionAcks(context.Context) (protocol.AttentionAcks, error)
AckAttention(context.Context, *protocol.AttentionAcks, time.Time) (delta, all protocol.AttentionAcks, err error)
}
// attentionAckHub fans an accepted acknowledgement out to every other open
// connection, so a dismissal on one device disappears on the others live.
// It holds each connection's serialized write function, never the socket.
type attentionAckHub struct {
mu sync.Mutex
clients map[string]func(protocol.Message) error
}
func newAttentionAckHub() *attentionAckHub {
return &attentionAckHub{clients: map[string]func(protocol.Message) error{}}
}
func (h *attentionAckHub) attach(clientID string, write func(protocol.Message) error) {
h.mu.Lock()
defer h.mu.Unlock()
h.clients[clientID] = write
}
func (h *attentionAckHub) detach(clientID string) {
h.mu.Lock()
defer h.mu.Unlock()
delete(h.clients, clientID)
}
// broadcast pushes delta to every connection except the sender. Each write
// runs on its own goroutine (bounded by the connection's own write timeout), so
// one slow client cannot delay the sender's reply or the other clients.
func (h *attentionAckHub) broadcast(sender string, delta protocol.AttentionAcks) {
h.mu.Lock()
targets := make([]func(protocol.Message) error, 0, len(h.clients))
for id, write := range h.clients {
if id != sender {
targets = append(targets, write)
}
}
h.mu.Unlock()
for _, write := range targets {
frame := delta
go func() {
_ = write(protocol.Message{Type: protocol.AttentionAcksChanged, AttentionAcks: &frame})
}()
}
}
// handleAttentionAcks answers attention_acks and ack_attention. It reports
// whether request was one of them. An ack that changes nothing (already
// dismissed, or a watermark that does not advance) replies with the whole set
// but pushes nothing.
func handleAttentionAcks(ctx context.Context, request protocol.Message, store attentionAckStore, hub *attentionAckHub, clientID string, write func(protocol.Message) error) bool {
if request.Type != protocol.GetAttentionAcks && request.Type != protocol.AckAttention {
return false
}
if store == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "attention acknowledgements are unavailable: no store"})
return true
}
var all, delta protocol.AttentionAcks
var err error
if request.Type == protocol.GetAttentionAcks {
all, err = store.AttentionAcks(ctx)
} else {
delta, all, err = store.AckAttention(ctx, request.AttentionAcks, time.Now())
}
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return true
}
_ = write(protocol.Message{Type: protocol.AttentionAcksReply, ID: request.ID, AttentionAcks: &all})
if hub != nil && !delta.Empty() {
hub.broadcast(clientID, delta)
}
return true
}