Exclude session start time from publishing time (#3089)

This commit is contained in:
cnderrauber
2024-10-12 10:19:33 +08:00
committed by GitHub
parent 6a721efa7c
commit 9147120915
+13 -4
View File
@@ -181,7 +181,7 @@ type ParticipantImpl struct {
isPublisher atomic.Bool isPublisher atomic.Bool
sessionStartRecorded atomic.Bool sessionStartRecorded atomic.Bool
lastActiveAt time.Time lastActiveAt atomic.Pointer[time.Time]
// when first connected // when first connected
connectedAt time.Time connectedAt time.Time
// timer that's set when disconnect is detected on primary PC // timer that's set when disconnect is detected on primary PC
@@ -1518,15 +1518,17 @@ func (p *ParticipantImpl) setupSubscriptionManager() {
} }
func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) { func (p *ParticipantImpl) updateState(state livekit.ParticipantInfo_State) {
if state == livekit.ParticipantInfo_ACTIVE {
t := time.Now()
p.lastActiveAt.CompareAndSwap(nil, &t)
}
oldState := p.state.Swap(state).(livekit.ParticipantInfo_State) oldState := p.state.Swap(state).(livekit.ParticipantInfo_State)
if oldState == state { if oldState == state {
return return
} }
if state == livekit.ParticipantInfo_DISCONNECTED && oldState == livekit.ParticipantInfo_ACTIVE { if state == livekit.ParticipantInfo_DISCONNECTED && oldState == livekit.ParticipantInfo_ACTIVE {
prometheus.RecordSessionDuration(int(p.ProtocolVersion()), time.Since(p.lastActiveAt)) prometheus.RecordSessionDuration(int(p.ProtocolVersion()), time.Since(*p.lastActiveAt.Load()))
} else if state == livekit.ParticipantInfo_ACTIVE {
p.lastActiveAt = time.Now()
} }
p.params.Logger.Debugw("updating participant state", "state", state.String()) p.params.Logger.Debugw("updating participant state", "state", state.String())
p.dirty.Store(true) p.dirty.Store(true)
@@ -2221,6 +2223,13 @@ func (p *ParticipantImpl) mediaTrackReceived(track *webrtc.TrackRemote, rtpRecei
} }
mt = p.addMediaTrack(signalCid, track.ID(), ti) mt = p.addMediaTrack(signalCid, track.ID(), ti)
newTrack = true newTrack = true
// if the addTrackRequest is sent before participant active then it means the client tries to publish
// before fully connected, in this case we only record the time when the participant is active since
// we want this metric to represent the time cost by pubilshing.
if activeAt := p.lastActiveAt.Load(); activeAt != nil && createdAt.Before(*activeAt) {
createdAt = *activeAt
}
pubTime = time.Since(createdAt) pubTime = time.Since(createdAt)
p.dirty.Store(true) p.dirty.Store(true)
} }