Take control over your live stream video by running it yourself. Streaming + chat out of the box.
HTML activitypubbroadcastingchatdecentralizedfederationfediversegolanghacktoberfesthlslivelivestreamowncastrtmpself-hostedstreaming-videovideo
fix(chat): synchronize parted message timers (#5142)
Gabe Kangasa9d579eee500
diff --git a/.github/workflows/browser-testing.yml b/.github/workflows/browser-testing.ymlindex 4f61cf55d5..97e87a995e 100644--- a/.github/workflows/browser-testing.yml+++ b/.github/workflows/browser-testing.yml@@ -247,7 +247,7 @@ jobs:aws s3 cp "$f" "s3://$S3_BUCKET/$key" --endpoint-url "$S3_ENDPOINT" \--acl public-read --content-type video/mp4 --no-progress >&2poster_url=""- if [ -n "$FFMPEG" ] && "$FFMPEG" -v error -ss 1 -i "$f" -frames:v 1 "${f%.mp4}.png"; then+ if [ -n "$FFMPEG" ] && "$FFMPEG" -v error -ss 3 -i "$f" -frames:v 1 "${f%.mp4}.png"; thenposter="${f%.mp4}.png"poster_url="$S3_ENDPOINT/$S3_BUCKET/${key%.mp4}.png"aws s3 cp "$poster" "s3://$S3_BUCKET/${key%.mp4}.png" --endpoint-url "$S3_ENDPOINT" \diff --git a/services/chat/server.go b/services/chat/server.goindex d897dab594..8c1770f285 100644--- a/services/chat/server.go+++ b/services/chat/server.go@@ -81,8 +81,9 @@ type Service struct {// each accepted inbound message.chatMessagesSentCounter prometheus.Gauge- // a map of user IDs and timers that fire for chat part messages.- userPartedTimers map[string]*time.Ticker+ // a map of user IDs and pending chat part messages.+ userPartedTimers map[string]*pendingUserPart+ userPartedTimersMu sync.Mutexseq uintmaxSocketConnectionLimit uint64@@ -102,7 +103,7 @@ func newServer(webhooksSvc *webhooks.Service) *Service {maxSocketConnectionLimit: maximumConcurrentConnectionLimit,geoipClient: geoip.NewClient(),webhooks: webhooksSvc,- userPartedTimers: map[string]*time.Ticker{},+ userPartedTimers: map[string]*pendingUserPart{},}return server@@ -113,11 +114,11 @@ func (s *Service) Run() {for {select {case clientID := <-s.unregister:- if client, ok := s.clients[clientID]; ok {+ s.mu.RLock()+ client, ok := s.clients[clientID]+ s.mu.RUnlock()+ if ok {s.handleClientDisconnected(client)- s.mu.Lock()- delete(s.clients, clientID)- s.mu.Unlock()}case message := <-s.inbound:@@ -159,9 +160,7 @@ func (s *Service) Addclient(conn *websocket.Conn, user *models.User, accessToken// If there is a pending disconnect timer then clear it. A quick// reconnect before the part was sent is not a new join, so neither the// join broadcast nor the webhook should fire for it.- if ticker, ok := s.userPartedTimers[user.ID]; ok {- ticker.Stop()- delete(s.userPartedTimers, user.ID)+ if s.cancelUserPartedMessage(user.ID) {isNewJoin = false}@@ -211,29 +210,68 @@ func (s *Service) sendUserJoinedMessage(c *Client) {s.webhooks.SendChatEventUserJoined(userJoinedEvent)}+type pendingUserPart struct {+ timer *time.Timer+}+func (s *Service) handleClientDisconnected(c *Client) {- if _, ok := s.clients[c.Id]; ok {- log.Debugln("Deleting", c.Id)- delete(s.clients, c.Id)- }+ s.mu.Lock()+ defer s.mu.Unlock()- additionalClientCheck, _ := s.GetClientsForUser(c.User.ID)- if len(additionalClientCheck) > 0 {- // This user is still connected to chat with another client.+ if _, ok := s.clients[c.Id]; !ok {return}- s.userPartedTimers[c.User.ID] = time.NewTicker(10 * time.Second)+ log.Debugln("Deleting", c.Id)+ delete(s.clients, c.Id)++ for _, client := range s.clients {+ if client.User.ID == c.User.ID {+ // This user is still connected to chat with another client.+ return+ }+ }++ s.scheduleUserPartedMessage(c)+}++func (s *Service) scheduleUserPartedMessage(c *Client) {+ pending := &pendingUserPart{}++ s.userPartedTimersMu.Lock()+ if existing, ok := s.userPartedTimers[c.User.ID]; ok {+ existing.timer.Stop()+ }+ s.userPartedTimers[c.User.ID] = pending+ pending.timer = time.AfterFunc(10*time.Second, func() {+ s.sendUserPartedMessage(c, pending)+ })+ s.userPartedTimersMu.Unlock()+}- go func() {- <-s.userPartedTimers[c.User.ID].C- s.sendUserPartedMessage(c)- }()+func (s *Service) cancelUserPartedMessage(userID string) bool {+ s.userPartedTimersMu.Lock()+ defer s.userPartedTimersMu.Unlock()++ pending, ok := s.userPartedTimers[userID]+ if !ok {+ return false+ }++ delete(s.userPartedTimers, userID)+ pending.timer.Stop()+ return true}-func (s *Service) sendUserPartedMessage(c *Client) {- s.userPartedTimers[c.User.ID].Stop()+func (s *Service) sendUserPartedMessage(c *Client, pending *pendingUserPart) {+ s.userPartedTimersMu.Lock()+ current, ok := s.userPartedTimers[c.User.ID]+ if !ok || current != pending {+ s.userPartedTimersMu.Unlock()+ return+ }delete(s.userPartedTimers, c.User.ID)+ s.userPartedTimersMu.Unlock()userPartEvent := events.UserPartEvent{}userPartEvent.SetDefaults()diff --git a/services/chat/server_test.go b/services/chat/server_test.goindex 2d1fcfca12..3fa8109fbc 100644--- a/services/chat/server_test.go+++ b/services/chat/server_test.go@@ -1,9 +1,11 @@package chatimport (+ "fmt""net/http""net/http/httptest""os"+ "sync""testing""time"@@ -107,3 +109,55 @@ func TestSendUserJoinedMessage_WebhookFiresRegardlessOfJoinPartSetting(t *testin})}}++func TestUserPartedTimersHandleConcurrentDisconnectsAndReconnects(t *testing.T) {+ chatSvc := &Service{+ clients: map[uint]*Client{},+ userPartedTimers: map[string]*pendingUserPart{},+ }++ const (+ workers = 8+ iterations = 100+ )++ start := make(chan struct{})+ var wg sync.WaitGroup+ for worker := range workers {+ wg.Add(1)+ go func() {+ defer wg.Done()+ <-start++ for iteration := range iterations {+ id := uint(worker*iterations + iteration)+ client := &Client{+ Id: id,+ User: &models.User{ID: fmt.Sprintf("user-%d", id)},+ }++ chatSvc.mu.Lock()+ chatSvc.clients[id] = client+ chatSvc.mu.Unlock()++ chatSvc.handleClientDisconnected(client)+ chatSvc.cancelUserPartedMessage(client.User.ID)+ }+ }()+ }++ close(start)+ wg.Wait()++ chatSvc.mu.RLock()+ defer chatSvc.mu.RUnlock()+ if len(chatSvc.clients) != 0 {+ t.Fatalf("connected clients = %d, want 0", len(chatSvc.clients))+ }++ chatSvc.userPartedTimersMu.Lock()+ defer chatSvc.userPartedTimersMu.Unlock()+ if len(chatSvc.userPartedTimers) != 0 {+ t.Fatalf("pending part timers = %d, want 0", len(chatSvc.userPartedTimers))+ }+}