navidrome/server/events/sse.go

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

217 lines
5.2 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"
"errors"
"fmt"
"io"
"net/http"
"sync/atomic"
"time"
2020-12-21 17:39:38 +01:00
"github.com/deluan/navidrome/consts"
"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
writeTimeOut = 5 * time.Second
)
2020-12-21 16:01:37 +01:00
var (
errWriteTimeOut = errors.New("write timeout")
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
}
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),
}
// 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
}
// writeEvent Write to the ResponseWriter, Server Sent Events compatible
func writeEvent(w io.Writer, event message, timeout time.Duration) (n int, err error) {
flusher, _ := w.(http.Flusher)
complete := make(chan struct{}, 1)
go func() {
n, err = 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()
complete <- struct{}{}
}()
select {
case <-complete:
return
case <-time.After(timeout):
return 0, errWriteTimeOut
}
}
2020-12-12 19:46:36 +01:00
func (b *broker) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
user, _ := request.UserFrom(ctx)
// Make sure that the writer supports flushing.
_, ok := w.(http.Flusher)
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:
log.Trace(ctx, "Sending event to client", "event", event, "client", c.String())
_, err := writeEvent(w, event, writeTimeOut)
if err == errWriteTimeOut {
return
}
case <-ctx.Done():
log.Trace(ctx, "Client closed the 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())
2020-12-12 19:46:36 +01:00
c := client{
id: uuid.NewString(),
2020-12-12 19:35:49 +01:00
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()
clients := map[client]struct{}{}
for {
select {
2020-12-12 19:46:36 +01:00
case c := <-b.subscribing:
// A new client has connected.
// Register their message channel
clients[c] = struct{}{}
log.Debug("Client added to event broker", "numClients", len(clients), "newClient", c.String())
2020-11-14 00:40:33 +01:00
// Send a serverStart event to new client
2020-12-21 17:39:38 +01:00
c.channel <- b.preparePackage(&ServerStart{consts.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(clients, c)
log.Debug("Removed client from event broker", "numClients", len(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
for c := range clients {
log.Trace("Putting event on client's queue", "client", c.String(), "event", event)
2020-12-21 17:39:38 +01:00
// Use non-blocking send. If cannot send, ignore the message
2020-12-12 19:35:49 +01:00
select {
case c.channel <- event:
2020-12-12 19:35:49 +01:00
default:
log.Warn("Could not send event to client", "client", c.String(), "event", event)
2020-12-12 19:35:49 +01:00
}
}
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()})
}
}
}