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")
+3 -1
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
@@ -1118,6 +1119,7 @@ func (p *ParticipantImpl) setupTransportManager() error {
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(),
DataChannelMaxBufferedAmount: p.params.DataChannelMaxBufferedAmount,
Logger: p.params.Logger.WithComponent(sutils.ComponentTransport), 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() {
@@ -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)
} }
}) })
+5
View File
@@ -256,6 +256,7 @@ type TransportParams struct {
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)
} }
+2
View File
@@ -66,6 +66,7 @@ type TransportManagerParams struct {
AllowUDPUnstableFallback bool AllowUDPUnstableFallback bool
TURNSEnabled bool TURNSEnabled bool
AllowPlayoutDelay bool AllowPlayoutDelay bool
DataChannelMaxBufferedAmount uint64
Logger logger.Logger Logger logger.Logger
} }
@@ -186,6 +187,7 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
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,