Add configuration to limit MaxBufferedAmount for data channel (#2170)

* Add configuration to limit MaxBufferedAmount for data channel

* comment

* Fix generate flags

* fix test

* Don't disconnect slow subscriber
This commit is contained in:
cnderrauber
2023-10-23 15:03:58 +08:00
committed by GitHub
parent 0bc932e57e
commit eca32792b8
8 changed files with 94 additions and 64 deletions
+9
View File
@@ -112,6 +112,15 @@ rtc:
# batch_io: # batch_io:
# batch_size: 128 # batch_size: 128
# max_flush_interval: 2ms # max_flush_interval: 2ms
# # force a reconnect on a publication error
# reconnect_on_publication_error: true
# # force a reconnect on a subscription error
# reconnect_on_subscription_error: true
# # force a reconnect on a data channel error
# reconnect_on_data_channel_error: true
# # max number of bytes to buffer for data channel. 0 means unlimited.
# # when this limit is breached, data messages will be dropped till the buffered amount drops below this limit.
# data_channel_max_buffered_amount: 0
# when enabled, LiveKit will expose prometheus metrics on :6789/metrics # when enabled, LiveKit will expose prometheus metrics on :6789/metrics
# prometheus_port: 6789 # prometheus_port: 6789
+11 -1
View File
@@ -106,6 +106,9 @@ type RTCConfig struct {
// force a reconnect on a data channel error // force a reconnect on a data channel error
ReconnectOnDataChannelError *bool `yaml:"reconnect_on_data_channel_error,omitempty"` ReconnectOnDataChannelError *bool `yaml:"reconnect_on_data_channel_error,omitempty"`
// max number of bytes to buffer for data channel. 0 means unlimited
DataChannelMaxBufferedAmount uint64 `yaml:"data_channel_max_buffered_amount,omitempty"`
} }
type TURNServer struct { type TURNServer struct {
@@ -723,6 +726,13 @@ func GenerateCLIFlags(existingFlags []cli.Flag, hidden bool) ([]cli.Flag, error)
Usage: generatedCLIFlagUsage, Usage: generatedCLIFlagUsage,
Hidden: hidden, Hidden: hidden,
} }
case reflect.Uint64:
flag = &cli.Uint64Flag{
Name: name,
EnvVars: []string{envVar},
Usage: generatedCLIFlagUsage,
Hidden: hidden,
}
case reflect.Float32: case reflect.Float32:
flag = &cli.Float64Flag{ flag = &cli.Float64Flag{
Name: name, Name: name,
@@ -784,7 +794,7 @@ func (conf *Config) updateFromCLI(c *cli.Context, baseFlags []cli.Flag) error {
configValue.SetString(c.String(flagName)) configValue.SetString(c.String(flagName))
case reflect.Int, reflect.Int32, reflect.Int64: case reflect.Int, reflect.Int32, reflect.Int64:
configValue.SetInt(c.Int64(flagName)) configValue.SetInt(c.Int64(flagName))
case reflect.Uint8, reflect.Uint16, reflect.Uint32: case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
configValue.SetUint(c.Uint64(flagName)) configValue.SetUint(c.Uint64(flagName))
case reflect.Float32: case reflect.Float32:
configValue.SetFloat(c.Float64(flagName)) configValue.SetFloat(c.Float64(flagName))
+1
View File
@@ -23,6 +23,7 @@ var (
ErrLimitExceeded = errors.New("node has exceeded its configured limit") ErrLimitExceeded = errors.New("node has exceeded its configured limit")
ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room") ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room")
ErrDataChannelUnavailable = errors.New("data channel is not available") ErrDataChannelUnavailable = errors.New("data channel is not available")
ErrDataChannelBufferFull = errors.New("data channel buffer is full")
ErrTransportFailure = errors.New("transport failure") ErrTransportFailure = errors.New("transport failure")
ErrEmptyIdentity = errors.New("participant identity cannot be empty") ErrEmptyIdentity = errors.New("participant identity cannot be empty")
ErrEmptyParticipantID = errors.New("participant ID cannot be empty") ErrEmptyParticipantID = errors.New("participant ID cannot be empty")
+19 -17
View File
@@ -118,6 +118,7 @@ type ParticipantParams struct {
ReconnectOnPublicationError bool ReconnectOnPublicationError bool
ReconnectOnSubscriptionError bool ReconnectOnSubscriptionError bool
ReconnectOnDataChannelError bool ReconnectOnDataChannelError bool
DataChannelMaxBufferedAmount uint64
VersionGenerator utils.TimedVersionGenerator VersionGenerator utils.TimedVersionGenerator
TrackResolver types.MediaTrackResolver TrackResolver types.MediaTrackResolver
DisableDynacast bool DisableDynacast bool
@@ -1103,22 +1104,23 @@ func (p *ParticipantImpl) setupTransportManager() error {
SID: p.params.SID, SID: p.params.SID,
// primary connection does not change, canSubscribe can change if permission was updated // primary connection does not change, canSubscribe can change if permission was updated
// after the participant has joined // after the participant has joined
SubscriberAsPrimary: p.ProtocolVersion().SubscriberAsPrimary() && p.CanSubscribe(), SubscriberAsPrimary: p.ProtocolVersion().SubscriberAsPrimary() && p.CanSubscribe(),
Config: p.params.Config, Config: p.params.Config,
ProtocolVersion: p.params.ProtocolVersion, ProtocolVersion: p.params.ProtocolVersion,
Telemetry: p.params.Telemetry, Telemetry: p.params.Telemetry,
CongestionControlConfig: p.params.CongestionControlConfig, CongestionControlConfig: p.params.CongestionControlConfig,
EnabledCodecs: p.params.EnabledCodecs, EnabledCodecs: p.params.EnabledCodecs,
SimTracks: p.params.SimTracks, SimTracks: p.params.SimTracks,
ClientConf: p.params.ClientConf, ClientConf: p.params.ClientConf,
ClientInfo: p.params.ClientInfo, ClientInfo: p.params.ClientInfo,
Migration: p.params.Migration, Migration: p.params.Migration,
AllowTCPFallback: p.params.AllowTCPFallback, AllowTCPFallback: p.params.AllowTCPFallback,
TCPFallbackRTTThreshold: p.params.TCPFallbackRTTThreshold, TCPFallbackRTTThreshold: p.params.TCPFallbackRTTThreshold,
AllowUDPUnstableFallback: p.params.AllowUDPUnstableFallback, AllowUDPUnstableFallback: p.params.AllowUDPUnstableFallback,
TURNSEnabled: p.params.TURNSEnabled, TURNSEnabled: p.params.TURNSEnabled,
AllowPlayoutDelay: p.params.PlayoutDelay.GetEnabled(), AllowPlayoutDelay: p.params.PlayoutDelay.GetEnabled(),
Logger: p.params.Logger.WithComponent(sutils.ComponentTransport), DataChannelMaxBufferedAmount: p.params.DataChannelMaxBufferedAmount,
Logger: p.params.Logger.WithComponent(sutils.ComponentTransport),
} }
if p.params.SyncStreams && p.params.PlayoutDelay.GetEnabled() && p.params.ClientInfo.isFirefox() { if p.params.SyncStreams && p.params.PlayoutDelay.GetEnabled() && p.params.ClientInfo.isFirefox() {
// we will disable playout delay for Firefox if the user is expecting // we will disable playout delay for Firefox if the user is expecting
@@ -2303,7 +2305,7 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket, data []byte) er
err := p.TransportManager.SendDataPacket(dp, data) err := p.TransportManager.SendDataPacket(dp, data)
if err != nil { if err != nil {
if (errors.Is(err, sctp.ErrStreamClosed) || errors.Is(err, io.ErrClosedPipe)) && p.params.ReconnectOnDataChannelError { if (errors.Is(err, sctp.ErrStreamClosed) || errors.Is(err, io.ErrClosedPipe)) && p.params.ReconnectOnDataChannelError {
p.params.Logger.Infow("issuing full reconnect on data channel error") p.params.Logger.Infow("issuing full reconnect on data channel error", "error", err)
p.IssueFullReconnect(types.ParticipantCloseReasonDataChannelError) p.IssueFullReconnect(types.ParticipantCloseReasonDataChannelError)
} }
} else { } else {
+1 -1
View File
@@ -1354,7 +1354,7 @@ func BroadcastDataPacketForRoom(r types.Room, source types.LocalParticipant, dp
utils.ParallelExec(destParticipants, dataForwardLoadBalanceThreshold, 1, func(op types.LocalParticipant) { utils.ParallelExec(destParticipants, dataForwardLoadBalanceThreshold, 1, func(op types.LocalParticipant) {
err := op.SendDataPacket(dp, dpData) err := op.SendDataPacket(dp, dpData)
if err != nil && !errors.Is(err, io.ErrClosedPipe) && !errors.Is(err, sctp.ErrStreamClosed) && if err != nil && !errors.Is(err, io.ErrClosedPipe) && !errors.Is(err, sctp.ErrStreamClosed) &&
!errors.Is(err, ErrTransportFailure) { !errors.Is(err, ErrTransportFailure) && !errors.Is(err, ErrDataChannelBufferFull) {
op.GetLogger().Infow("send data packet error", "error", err) op.GetLogger().Infow("send data packet error", "error", err)
} }
}) })
+19 -14
View File
@@ -242,20 +242,21 @@ type PCTransport struct {
} }
type TransportParams struct { type TransportParams struct {
ParticipantID livekit.ParticipantID ParticipantID livekit.ParticipantID
ParticipantIdentity livekit.ParticipantIdentity ParticipantIdentity livekit.ParticipantIdentity
ProtocolVersion types.ProtocolVersion ProtocolVersion types.ProtocolVersion
Config *WebRTCConfig Config *WebRTCConfig
DirectionConfig DirectionConfig DirectionConfig DirectionConfig
CongestionControlConfig config.CongestionControlConfig CongestionControlConfig config.CongestionControlConfig
Telemetry telemetry.TelemetryService Telemetry telemetry.TelemetryService
EnabledCodecs []*livekit.Codec EnabledCodecs []*livekit.Codec
Logger logger.Logger Logger logger.Logger
SimTracks map[uint32]SimulcastTrackInfo SimTracks map[uint32]SimulcastTrackInfo
ClientInfo ClientInfo ClientInfo ClientInfo
IsOfferer bool IsOfferer bool
IsSendSide bool IsSendSide bool
AllowPlayoutDelay bool AllowPlayoutDelay bool
DataChannelMaxBufferedAmount uint64
} }
func newPeerConnection(params TransportParams, onBandwidthEstimator func(estimator cc.BandwidthEstimator)) (*webrtc.PeerConnection, *webrtc.MediaEngine, error) { func newPeerConnection(params TransportParams, onBandwidthEstimator func(estimator cc.BandwidthEstimator)) (*webrtc.PeerConnection, *webrtc.MediaEngine, error) {
@@ -930,6 +931,10 @@ func (t *PCTransport) SendDataPacket(dp *livekit.DataPacket, data []byte) error
return ErrTransportFailure return ErrTransportFailure
} }
if t.params.DataChannelMaxBufferedAmount > 0 && dc.BufferedAmount() > t.params.DataChannelMaxBufferedAmount {
return ErrDataChannelBufferFull
}
return dc.Send(data) return dc.Send(data)
} }
+33 -31
View File
@@ -49,24 +49,25 @@ const (
) )
type TransportManagerParams struct { type TransportManagerParams struct {
Identity livekit.ParticipantIdentity Identity livekit.ParticipantIdentity
SID livekit.ParticipantID SID livekit.ParticipantID
SubscriberAsPrimary bool SubscriberAsPrimary bool
Config *WebRTCConfig Config *WebRTCConfig
ProtocolVersion types.ProtocolVersion ProtocolVersion types.ProtocolVersion
Telemetry telemetry.TelemetryService Telemetry telemetry.TelemetryService
CongestionControlConfig config.CongestionControlConfig CongestionControlConfig config.CongestionControlConfig
EnabledCodecs []*livekit.Codec EnabledCodecs []*livekit.Codec
SimTracks map[uint32]SimulcastTrackInfo SimTracks map[uint32]SimulcastTrackInfo
ClientConf *livekit.ClientConfiguration ClientConf *livekit.ClientConfiguration
ClientInfo ClientInfo ClientInfo ClientInfo
Migration bool Migration bool
AllowTCPFallback bool AllowTCPFallback bool
TCPFallbackRTTThreshold int TCPFallbackRTTThreshold int
AllowUDPUnstableFallback bool AllowUDPUnstableFallback bool
TURNSEnabled bool TURNSEnabled bool
AllowPlayoutDelay bool AllowPlayoutDelay bool
Logger logger.Logger DataChannelMaxBufferedAmount uint64
Logger logger.Logger
} }
type TransportManager struct { type TransportManager struct {
@@ -173,19 +174,20 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
}) })
subscriber, err := NewPCTransport(TransportParams{ subscriber, err := NewPCTransport(TransportParams{
ParticipantID: params.SID, ParticipantID: params.SID,
ParticipantIdentity: params.Identity, ParticipantIdentity: params.Identity,
ProtocolVersion: params.ProtocolVersion, ProtocolVersion: params.ProtocolVersion,
Config: params.Config, Config: params.Config,
DirectionConfig: params.Config.Subscriber, DirectionConfig: params.Config.Subscriber,
CongestionControlConfig: params.CongestionControlConfig, CongestionControlConfig: params.CongestionControlConfig,
Telemetry: params.Telemetry, Telemetry: params.Telemetry,
EnabledCodecs: subscribeCodecs, EnabledCodecs: subscribeCodecs,
Logger: LoggerWithPCTarget(params.Logger, livekit.SignalTarget_SUBSCRIBER), Logger: LoggerWithPCTarget(params.Logger, livekit.SignalTarget_SUBSCRIBER),
ClientInfo: params.ClientInfo, ClientInfo: params.ClientInfo,
IsOfferer: true, IsOfferer: true,
IsSendSide: true, IsSendSide: true,
AllowPlayoutDelay: params.AllowPlayoutDelay, AllowPlayoutDelay: params.AllowPlayoutDelay,
DataChannelMaxBufferedAmount: params.DataChannelMaxBufferedAmount,
}) })
if err != nil { if err != nil {
return nil, err return nil, err
+1
View File
@@ -409,6 +409,7 @@ func (r *RoomManager) StartSession(
ReconnectOnPublicationError: reconnectOnPublicationError, ReconnectOnPublicationError: reconnectOnPublicationError,
ReconnectOnSubscriptionError: reconnectOnSubscriptionError, ReconnectOnSubscriptionError: reconnectOnSubscriptionError,
ReconnectOnDataChannelError: reconnectOnDataChannelError, ReconnectOnDataChannelError: reconnectOnDataChannelError,
DataChannelMaxBufferedAmount: r.config.RTC.DataChannelMaxBufferedAmount,
VersionGenerator: r.versionGenerator, VersionGenerator: r.versionGenerator,
TrackResolver: room.ResolveMediaTrackForSubscriber, TrackResolver: room.ResolveMediaTrackForSubscriber,
SubscriberAllowPause: subscriberAllowPause, SubscriberAllowPause: subscriberAllowPause,