navidrome/server/events/sse.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

201 lines
4.9 KiB
Go
Raw Normal View History

// Based on https://thoughtbot.com/blog/writing-a-server-sent-events-server-in-go
package events
import (
"encoding/json"
"fmt"
"net/http"
"sync/atomic"
"time"
"github.com/deluan/navidrome/log"
2020-11-10 01:24:04 +01:00
"github.com/deluan/navidrome/model/request"
2020-12-12 19:35:49 +01:00
"github.com/google/uuid"
)
type Broker interface {
http.Handler
SendMessage(event Event)
}
const keepAliveFrequency = 15 * time.Second
var eventId uint32
type (
message struct {
ID uint32
Event string
Data string
}
messageChan chan message
clientsChan chan client
client struct {
2020-12-12 19:35:49 +01:00
id string
address string
username string
userAgent string
channel messageChan
}
)
func (c client) String() string {
2020-12-12 19:35:49 +01:00
return fmt.Sprintf("%s (%s - %s - %s)", c.id, c.username, c.address, c.userAgent)
}
2020-11-14 00:40:33 +01:00
type broker struct {
// Events are pushed to this channel by the main events-gathering routine
2020-12-12 19:46:36 +01:00
publish messageChan
// New client connections
2020-12-12 19:46:36 +01:00
subscribing clientsChan
// Closed client connections
2020-12-12 19:46:36 +01:00
unsubscribing clientsChan
// Client connections registry
2020-12-12 19:35:49 +01:00
clients map[client]struct{}
}
func NewBroker() Broker {
// Instantiate a broker
broker := &broker{
2020-12-12 19:46:36 +01:00
publish: make(messageChan, 100),
subscribing: make(clientsChan, 1),
unsubscribing: make(clientsChan, 1),
clients: make(map[client]struct{}),
}
// Set it running - listening and broadcasting events
go broker.listen()
return broker
}
2020-12-12 19:46:36 +01:00
func (b *broker) SendMessage(evt Event) {
msg := b.preparePackage(evt)
log.Trace("Broker received new event", "event", msg)
2020-12-12 19:46:36 +01:00
b.publish <- msg
2020-11-14 00:40:33 +01:00
}
2020-12-12 19:46:36 +01:00
func (b *broker) newEventID() uint32 {
return atomic.AddUint32(&eventId, 1)
}
func (b *broker) preparePackage(event Event) message {
pkg := message{}
2020-12-12 19:46:36 +01:00
pkg.ID = b.newEventID()
pkg.Event = event.EventName()
data, _ := json.Marshal(event)
pkg.Data = string(data)
return pkg
}
2020-12-12 19:46:36 +01:00
func (b *broker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Make sure that the writer supports flushing.
flusher, ok := w.(http.Flusher)
user, _ := request.UserFrom(ctx)
if !ok {
log.Error(w, "Streaming unsupported! Events cannot be sent to this client", "address", r.RemoteAddr,
"userAgent", r.UserAgent(), "user", user.UserName)
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache, no-transform")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
// Each connection registers its own message channel with the Broker's connections registry
2020-12-12 19:46:36 +01:00
c := b.subscribe(r)
defer b.unsubscribe(c)
log.Debug(ctx, "New broker client", "client", c.String())
2020-11-14 06:44:58 +01:00
for {
select {
2020-12-12 19:46:36 +01:00
case event := <-c.channel:
// Write to the ResponseWriter
// Server Sent Events compatible
2020-12-12 19:46:36 +01:00
log.Trace(ctx, "Sending event to client", "event", event, "client", c.String())
_, _ = fmt.Fprintf(w, "id: %d\nevent: %s\ndata: %s\n\n", event.ID, event.Event, event.Data)
// Flush the data immediately instead of buffering it for later.
flusher.Flush()
case <-ctx.Done():
2020-12-12 19:46:36 +01:00
log.Trace(ctx, "Closing event stream connection", "client", c.String())
return
}
}
}
2020-12-12 19:46:36 +01:00
func (b *broker) subscribe(r *http.Request) client {
2020-12-12 19:35:49 +01:00
user, _ := request.UserFrom(r.Context())
id, _ := uuid.NewRandom()
2020-12-12 19:46:36 +01:00
c := client{
2020-12-12 19:35:49 +01:00
id: id.String(),
username: user.UserName,
address: r.RemoteAddr,
userAgent: r.UserAgent(),
channel: make(messageChan, 5),
}
// Signal the broker that we have a new client
2020-12-12 19:46:36 +01:00
b.subscribing <- c
return c
2020-12-12 19:35:49 +01:00
}
2020-12-12 19:46:36 +01:00
func (b *broker) unsubscribe(c client) {
b.unsubscribing <- c
2020-12-12 19:35:49 +01:00
}
2020-12-12 19:46:36 +01:00
func (b *broker) listen() {
keepAlive := time.NewTicker(keepAliveFrequency)
defer keepAlive.Stop()
for {
select {
2020-12-12 19:46:36 +01:00
case c := <-b.subscribing:
// A new client has connected.
// Register their message channel
2020-12-12 19:46:36 +01:00
b.clients[c] = struct{}{}
log.Debug("Client added to event broker", "numClients", len(b.clients), "newClient", c.String())
2020-11-14 00:40:33 +01:00
// Send a serverStart event to new client
2020-12-12 19:46:36 +01:00
c.channel <- b.preparePackage(&ServerStart{serverStart})
2020-11-14 00:40:33 +01:00
2020-12-12 19:46:36 +01:00
case c := <-b.unsubscribing:
// A client has detached and we want to
// stop sending them messages.
2020-12-12 19:46:36 +01:00
close(c.channel)
delete(b.clients, c)
log.Debug("Removed client from event broker", "numClients", len(b.clients), "client", c.String())
2020-12-12 19:46:36 +01:00
case event := <-b.publish:
// We got a new event from the outside!
// Send event to all connected clients
2020-12-12 19:46:36 +01:00
for client := range b.clients {
log.Trace("Putting event on client's queue", "client", client.String(), "event", event)
2020-12-12 19:35:49 +01:00
// Use non-blocking send
select {
case client.channel <- event:
default:
log.Warn("Could not send message to client", "client", client.String(), "event", event)
}
}
2020-11-14 00:40:33 +01:00
case ts := <-keepAlive.C:
// Send a keep alive message every 15 seconds
2020-12-12 19:46:36 +01:00
b.SendMessage(&KeepAlive{TS: ts.Unix()})
}
}
}
2020-11-14 00:40:33 +01:00
var serverStart time.Time
2020-11-14 00:40:33 +01:00
func init() {
serverStart = time.Now()
}