Use nonce in data messages to de-dupe SendData API. (#3366)
* Use nonce in data messages to de-dupe SendData API. With API retries, it is possible that some data messages are sent multiple times. Check the nonce (if present) to de-dupe data messages before forwarding it to the clients. * add nonce to UserPacket * defer unlock * 128-bit UUID as nonce * - UUID nonce - make a UserPacketDeduper module for easier re-use. * deps * feedback
This commit is contained in:
@@ -142,6 +142,8 @@ type Room struct {
|
||||
simulationLock sync.Mutex
|
||||
disconnectSignalOnResumeParticipants map[livekit.ParticipantIdentity]time.Time
|
||||
disconnectSignalOnResumeNoMessagesParticipants map[livekit.ParticipantIdentity]*disconnectSignalOnResumeNoMessages
|
||||
|
||||
userPacketDeduper *UserPacketDeduper
|
||||
}
|
||||
|
||||
type ParticipantOptions struct {
|
||||
@@ -270,6 +272,7 @@ func NewRoom(
|
||||
trailer: []byte(utils.RandomSecret()),
|
||||
disconnectSignalOnResumeParticipants: make(map[livekit.ParticipantIdentity]time.Time),
|
||||
disconnectSignalOnResumeNoMessagesParticipants: make(map[livekit.ParticipantIdentity]*disconnectSignalOnResumeNoMessages),
|
||||
userPacketDeduper: NewUserPacketDeduper(),
|
||||
}
|
||||
|
||||
if r.protoRoom.EmptyTimeout == 0 {
|
||||
@@ -1741,12 +1744,20 @@ func (r *Room) createAgentDispatchesFromRoomAgent() {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Room) IsDataMessageUserPacketDuplicate(up *livekit.UserPacket) bool {
|
||||
return r.userPacketDeduper.IsDuplicate(up)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
|
||||
func BroadcastDataPacketForRoom(r types.Room, source types.LocalParticipant, kind livekit.DataPacket_Kind, dp *livekit.DataPacket, logger logger.Logger) {
|
||||
dp.Kind = kind // backward compatibility
|
||||
dest := dp.GetUser().GetDestinationSids()
|
||||
if u := dp.GetUser(); u != nil {
|
||||
if r.IsDataMessageUserPacketDuplicate(u) {
|
||||
logger.Infow("dropping duplicate data message", "nonce", u.Nonce)
|
||||
return
|
||||
}
|
||||
if len(dp.DestinationIdentities) == 0 {
|
||||
dp.DestinationIdentities = u.DestinationIdentities
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user