provides cancelation when a component stop responding it closes all resources

This commit is contained in:
Leandro Moreira
2024-01-31 00:21:15 -03:00
parent 1e29f3c4ab
commit a903aa17d7
6 changed files with 97 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
package controllers
import (
"context"
"net"
"github.com/flavioribeiro/donut/internal/entities"
@@ -27,7 +28,7 @@ func NewWebRTCController(
}
}
func (c *WebRTCController) CreatePeerConnection() (*webrtc.PeerConnection, error) {
func (c *WebRTCController) CreatePeerConnection(cancel context.CancelFunc) (*webrtc.PeerConnection, error) {
c.l.Sugar().Infow("trying to set up web rtc conn")
peerConnectionConfiguration := webrtc.Configuration{}
@@ -48,6 +49,18 @@ func (c *WebRTCController) CreatePeerConnection() (*webrtc.PeerConnection, error
}
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
finished := connectionState == webrtc.ICEConnectionStateClosed ||
connectionState == webrtc.ICEConnectionStateDisconnected ||
connectionState == webrtc.ICEConnectionStateCompleted ||
connectionState == webrtc.ICEConnectionStateFailed
if finished {
c.l.Sugar().Infow("Canceling webrtc",
"status", connectionState.String(),
)
cancel()
}
c.l.Sugar().Infow("OnICEConnectionStateChange",
"status", connectionState.String(),
)