Skip to content

Commit

Permalink
fix: metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
hongcha98 committed Dec 23, 2023
1 parent b353686 commit 3052cf3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/forward/forward_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use webrtc::track::track_local::{TrackLocal, TrackLocalWriter};
use webrtc::track::track_remote::TrackRemote;

use crate::forward::info::Layer;
use crate::media;
use crate::AppError;
use crate::{media, metrics};

use super::rtcp::RtcpMessage;
use super::track_match;
Expand Down Expand Up @@ -160,6 +160,7 @@ impl PeerForwardInternal {
}
info!("[{}] [anchor] set {}", self.id, peer.get_stats_id());
*anchor = Some(peer);
metrics::PUBLISH.inc();
Ok(())
}

Expand All @@ -180,14 +181,15 @@ impl PeerForwardInternal {
subscribe_group.clear();
*anchor = None;
info!("[{}] [anchor] set none", self.id);
metrics::PUBLISH.dec();
Ok(())
}

pub async fn add_subscribe(&self, peer: Arc<RTCPeerConnection>) -> Result<()> {
let mut subscribe_peers = self.subscribe_group.write().await;
subscribe_peers.push(PeerWrap(peer.clone()));
drop(subscribe_peers);
info!("[{}] [subscribe] [{}] up", self.id, peer.get_stats_id());
metrics::SUBSCRIBE.inc();
Ok(())
}

Expand Down Expand Up @@ -298,9 +300,12 @@ impl PeerForwardInternal {
subscription_group.remove(&peer_wrap);
}
let mut subscribe_peers = self.subscribe_group.write().await;
let size = subscribe_peers.len();
subscribe_peers.retain(|x| x != &peer_wrap);
drop(subscribe_peers);
info!("[{}] [subscribe] [{}] down", self.id, peer.get_stats_id());
if size != subscribe_peers.len() {
info!("[{}] [subscribe] [{}] down", self.id, peer.get_stats_id());
metrics::SUBSCRIBE.dec();
}
Ok(())
}

Expand Down
10 changes: 2 additions & 8 deletions src/forward/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use webrtc::sdp::{MediaDescription, SessionDescription};

use crate::forward::forward_internal::{get_peer_key, PeerForwardInternal};
use crate::forward::info::Layer;
use crate::media;
use crate::AppError;
use crate::{media, metrics};

mod forward_internal;
pub mod info;
Expand Down Expand Up @@ -72,11 +72,7 @@ impl PeerForward {
RTCPeerConnectionState::Failed | RTCPeerConnectionState::Disconnected => {
let _ = pc.close().await;
}
RTCPeerConnectionState::Connected => {
metrics::PUBLISH.inc();
}
RTCPeerConnectionState::Closed => {
metrics::PUBLISH.dec();
let _ = internal.remove_anchor(pc).await;
}
_ => {}
Expand Down Expand Up @@ -127,7 +123,6 @@ impl PeerForward {
let _ = pc.close().await;
}
RTCPeerConnectionState::Closed => {
metrics::SUBSCRIBE.dec();
let _ = internal.remove_subscribe(pc).await;
}
_ => {}
Expand All @@ -140,8 +135,7 @@ impl PeerForward {
peer_complete(offer, peer.clone()).await?,
get_peer_key(peer.clone()),
);
metrics::SUBSCRIBE.inc();
let _ = self.internal.add_subscribe(peer).await?;
self.internal.add_subscribe(peer).await?;
Ok((sdp, key))
}

Expand Down

0 comments on commit 3052cf3

Please sign in to comment.