Skip to content

Remove unnecessary logs from streams #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nanpa/remove-stream-logs.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="removed" "Removed unnecessary logs from stream handlers"
22 changes: 9 additions & 13 deletions Sources/LiveKit/DataStream/Incoming/IncomingStreamManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

/// Mapping between stream ID and descriptor for open streams.
private var openStreams: [String: Descriptor] = [:]
/// Stream topics without a registered handler.
private var failedToOpenStreams: Set<String> = []

private var byteStreamHandlers: [String: ByteStreamHandler] = [:]
private var textStreamHandlers: [String: TextStreamHandler] = [:]
Expand Down Expand Up @@ -63,27 +65,28 @@
let identity = Participant.Identity(from: identityString)

guard let streamInfo = Self.streamInfo(from: header) else {
log("Invalid header for stream '\(header.streamID)'", .error)
return
}
openStream(with: streamInfo, from: identity)
}

private func openStream(with info: StreamInfo, from identity: Participant.Identity) {
guard openStreams[info.id] == nil else {
log("Stream '\(info.id)' already open", .error)
return
}
guard let handler = handler(for: info) else {
log("Unable to find handler for stream '\(info.id)'", .warning)
let topic = info.topic
if !failedToOpenStreams.contains(topic) {
logger.warning("Unable to find handler for incoming stream: \(info.id), topic: \(topic), opened by: \(identity)")
failedToOpenStreams.insert(topic)
}
return
}

var continuation: StreamReaderSource.Continuation!
let source = StreamReaderSource {
$0.onTermination = { @Sendable [weak self] termination in
$0.onTermination = { @Sendable [weak self] _ in
guard let self else { return }
self.log("Continuation terminated: \(termination)", .debug)
Task { await self.closeStream(with: info.id) }
}
continuation = $0
Expand All @@ -94,23 +97,18 @@
openTime: Date.timeIntervalSinceReferenceDate,
continuation: continuation
)
log("Opened stream '\(info.id)'", .debug)
openStreams[info.id] = descriptor

Task.detached {
do { try await handler(source, identity) }
catch { logger.error("Unhandled error in stream handler") }
try await handler(source, identity)
}
}

/// Close the stream with the given id.
private func closeStream(with id: String) {
guard let descriptor = openStreams[id] else {

Check warning on line 109 in Sources/LiveKit/DataStream/Incoming/IncomingStreamManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.2, macOS, true)

value 'descriptor' was defined but never used; consider replacing with boolean test

Check warning on line 109 in Sources/LiveKit/DataStream/Incoming/IncomingStreamManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS)

value 'descriptor' was defined but never used; consider replacing with boolean test

Check warning on line 109 in Sources/LiveKit/DataStream/Incoming/IncomingStreamManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-14, 15.4, macOS,variant=Mac Catalyst)

value 'descriptor' was defined but never used; consider replacing with boolean test

Check warning on line 109 in Sources/LiveKit/DataStream/Incoming/IncomingStreamManager.swift

View workflow job for this annotation

GitHub Actions / Build & Test (macos-15, 16.2, iOS Simulator,OS=18.1,name=iPhone 16 Pro, true)

value 'descriptor' was defined but never used; consider replacing with boolean test
log("No descriptor for stream '\(id)'", .debug)
return
}
let openDuration = Date.timeIntervalSinceReferenceDate - descriptor.openTime
log("Closed stream '\(id)' (open for \(openDuration))", .debug)
openStreams[id] = nil
}

Expand All @@ -133,7 +131,6 @@
/// Handles a data stream trailer.
func handle(trailer: Livekit_DataStream.Trailer) {
guard let descriptor = openStreams[trailer.streamID] else {
log("Received trailer for unknown stream '\(trailer.streamID)'", .warning)
return
}
if let totalLength = descriptor.info.totalLength {
Expand Down Expand Up @@ -175,7 +172,6 @@

deinit {
guard !openStreams.isEmpty else { return }
log("Terminating \(openStreams.count) open stream(s)", .debug)
for descriptor in openStreams.values {
descriptor.continuation.finish(throwing: StreamError.terminated)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ actor OutgoingStreamManager: Loggable {

let descriptor = Descriptor(info: info)
openStreams[info.id] = descriptor

log("Opened stream '\(info.id)'", .debug)
}

private func send(_ data: some StreamData, to id: String) async throws {
Expand Down Expand Up @@ -210,8 +208,6 @@ actor OutgoingStreamManager: Loggable {

try await packetHandler(packet)
openStreams[id] = nil

log("Closed stream '\(id)'", .debug)
}

// MARK: - Destination
Expand Down
Loading