perf: 优化后端性能与直播延迟,包含数据库WAL模式、流媒体写缓冲、转码 Preset 优化、聊天锁和指标采集优化,以及前端自动追帧功能
This commit is contained in:
@@ -66,20 +66,25 @@ func (h *Hub) Run() {
|
||||
}
|
||||
h.rooms[client.RoomID][client] = true
|
||||
|
||||
// Send existing history to the newly joined client
|
||||
// Copy existing history to send outside the lock
|
||||
var historyCopy []Message
|
||||
if history, ok := h.roomsHistory[client.RoomID]; ok {
|
||||
for _, msg := range history {
|
||||
msg.IsHistory = true
|
||||
msgBytes, _ := json.Marshal(msg)
|
||||
// Use select to avoid blocking if client's send channel is full
|
||||
historyCopy = make([]Message, len(history))
|
||||
copy(historyCopy, history)
|
||||
}
|
||||
h.mutex.Unlock()
|
||||
|
||||
// Send history outside the lock
|
||||
for _, msg := range historyCopy {
|
||||
msg.IsHistory = true
|
||||
msgBytes, err := json.Marshal(msg)
|
||||
if err == nil {
|
||||
select {
|
||||
case client.Send <- msgBytes:
|
||||
default:
|
||||
// If send fails, we could potentially log or ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
h.mutex.Unlock()
|
||||
|
||||
case client := <-h.unregister:
|
||||
h.mutex.Lock()
|
||||
@@ -99,6 +104,12 @@ func (h *Hub) Run() {
|
||||
h.mutex.Unlock()
|
||||
|
||||
case message := <-h.broadcast:
|
||||
// Marshal message outside the lock to optimize performance
|
||||
msgBytes, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
h.mutex.Lock()
|
||||
// Only store "chat" and "danmaku" messages in history
|
||||
if message.Type == "chat" || message.Type == "danmaku" {
|
||||
@@ -111,7 +122,6 @@ func (h *Hub) Run() {
|
||||
|
||||
clients := h.rooms[message.RoomID]
|
||||
if clients != nil {
|
||||
msgBytes, _ := json.Marshal(message)
|
||||
for client := range clients {
|
||||
select {
|
||||
case client.Send <- msgBytes:
|
||||
|
||||
Reference in New Issue
Block a user