From 1c69a9eeed240024d43baf66649498cfc7217fc3 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 19 Feb 2025 22:57:40 -0800 Subject: [PATCH] Dependent participants should not trigger count towards FirstJoinedAt (#3448) * Dependent participants should not trigger count towards FirstJoinedAt According to the API, empty timeout should be honored as long as no independent participant joins the room. If we counted Agents and Egress as part of FirstJoinedAt, it would have the side effect of using departureTimeout instead of emptyTimeout for idle calculations. * use Room logger --- pkg/rtc/room.go | 14 ++++++++++---- pkg/service/roommanager.go | 5 ++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index a2fa0381d..5f7a2ea4a 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -440,7 +440,7 @@ func (r *Room) Join(participant types.LocalParticipant, requestSource routing.Me } } - if r.FirstJoinedAt() == 0 { + if r.FirstJoinedAt() == 0 && !participant.IsDependent() { r.joinedAt.Store(time.Now().Unix()) } @@ -871,36 +871,42 @@ func (r *Room) IsClosed() bool { } // CloseIfEmpty closes the room if all participants had left, or it's still empty past timeout -func (r *Room) CloseIfEmpty() { +func (r *Room) CloseIfEmpty() string { r.lock.Lock() if r.IsClosed() || r.holds.Load() > 0 { r.lock.Unlock() - return + return "" } for _, p := range r.participants { if !p.IsDependent() { r.lock.Unlock() - return + return "" } } var timeout uint32 var elapsed int64 + reason := "" if r.FirstJoinedAt() > 0 && r.LastLeftAt() > 0 { elapsed = time.Now().Unix() - r.LastLeftAt() // need to give time in case participant is reconnecting timeout = r.protoRoom.DepartureTimeout + reason = "departure timeout" } else { elapsed = time.Now().Unix() - r.protoRoom.CreationTime timeout = r.protoRoom.EmptyTimeout + reason = "empty timeout" } r.lock.Unlock() if elapsed >= int64(timeout) { r.Close(types.ParticipantCloseReasonRoomClosed) + return reason } + + return "" } func (r *Room) Close(reason types.ParticipantCloseReason) { diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 2417f1f66..18835af28 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -197,7 +197,10 @@ func (r *RoomManager) CloseIdleRooms() { r.lock.RUnlock() for _, room := range rooms { - room.CloseIfEmpty() + reason := room.CloseIfEmpty() + if reason != "" { + room.Logger.Infow("closing idle room", "reason", reason) + } } }