mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-15 03:30:39 +03:00
Better broker logging
This commit is contained in:
parent
84080a0e44
commit
08dbf44529
@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/deluan/navidrome/log"
|
"github.com/deluan/navidrome/log"
|
||||||
|
"github.com/deluan/navidrome/model/request"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Broker interface {
|
type Broker interface {
|
||||||
@ -52,15 +53,19 @@ func (broker *broker) SendMessage(event Event) {
|
|||||||
pkg.Name = event.EventName()
|
pkg.Name = event.EventName()
|
||||||
pkg.Event = event
|
pkg.Event = event
|
||||||
data, _ := json.Marshal(pkg)
|
data, _ := json.Marshal(pkg)
|
||||||
|
|
||||||
|
log.Trace("Broker received new event", "name", pkg.Name, "payload", data)
|
||||||
broker.notifier <- data
|
broker.notifier <- data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (broker *broker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
func (broker *broker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
// Make sure that the writer supports flushing.
|
// Make sure that the writer supports flushing.
|
||||||
//
|
|
||||||
flusher, ok := rw.(http.Flusher)
|
flusher, ok := rw.(http.Flusher)
|
||||||
|
|
||||||
|
username, _ := request.UsernameFrom(req.Context())
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Error(rw, "Streaming unsupported! Events cannot be sent to this client", "address", req.RemoteAddr,
|
||||||
|
"userAgent", req.UserAgent(), "user", username)
|
||||||
http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
|
http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -83,9 +88,7 @@ func (broker *broker) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// Listen to connection close and un-register messageChan
|
// Listen to connection close and un-register messageChan
|
||||||
// notify := rw.(http.CloseNotifier).CloseNotify()
|
|
||||||
notify := req.Context().Done()
|
notify := req.Context().Done()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-notify
|
<-notify
|
||||||
broker.closingClients <- messageChan
|
broker.closingClients <- messageChan
|
||||||
@ -112,13 +115,13 @@ func (broker *broker) listen() {
|
|||||||
// A new client has connected.
|
// A new client has connected.
|
||||||
// Register their message channel
|
// Register their message channel
|
||||||
broker.clients[s] = true
|
broker.clients[s] = true
|
||||||
log.Debug("Client added", "numClients", len(broker.clients))
|
log.Debug("Client added to event broker", "numClients", len(broker.clients))
|
||||||
case s := <-broker.closingClients:
|
case s := <-broker.closingClients:
|
||||||
|
|
||||||
// A client has dettached and we want to
|
// A client has dettached and we want to
|
||||||
// stop sending them messages.
|
// stop sending them messages.
|
||||||
delete(broker.clients, s)
|
delete(broker.clients, s)
|
||||||
log.Debug("Removed client", "numClients", len(broker.clients))
|
log.Debug("Removed client from event broker", "numClients", len(broker.clients))
|
||||||
case event := <-broker.notifier:
|
case event := <-broker.notifier:
|
||||||
|
|
||||||
// We got a new event from the outside!
|
// We got a new event from the outside!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user