Skip to content

Commit

Permalink
Updated flushQueue entry scheduling.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsneed committed Nov 17, 2023
1 parent 2f61ef7 commit 4af5a8b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
11 changes: 8 additions & 3 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,23 @@ extension Analytics {
flushGroup.enter()

apply { plugin in
// we want to enter as soon as possible. waiting to do it from
// another queue just takes too long.
flushGroup.enter()
operatingMode.run(queue: configuration.values.flushQueue) {
if let p = plugin as? FlushCompletion {
// this is async
// flush(group:completion:) handles the enter/leave.
p.flush(group: flushGroup) { plugin in
p.flush { plugin in
// we don't really care about the plugin value .. yet.
flushGroup.leave()
}
} else if let p = plugin as? EventPlugin {
// we have no idea if this will be async or not, assume it's sync.
flushGroup.enter()
p.flush()
flushGroup.leave()
} else {
// this is for plugins that don't implement flush.
flushGroup.leave()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Segment/Plugins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public protocol VersionedPlugin {
}

public protocol FlushCompletion {
func flush(group: DispatchGroup, completion: @escaping (DestinationPlugin) -> Void)
func flush(completion: @escaping (DestinationPlugin) -> Void)
}

// For internal platform-specific bits
Expand Down
6 changes: 1 addition & 5 deletions Sources/Segment/Plugins/SegmentDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public class SegmentDestination: DestinationPlugin, Subscriber, FlushCompletion
// unused .. see flush(group:completion:)
}

public func flush(group: DispatchGroup, completion: @escaping (DestinationPlugin) -> Void) {
public func flush(completion: @escaping (DestinationPlugin) -> Void) {
guard let storage = self.storage else { return }
guard let analytics = self.analytics else { return }
guard let httpClient = self.httpClient else { return }
Expand All @@ -135,8 +135,6 @@ public class SegmentDestination: DestinationPlugin, Subscriber, FlushCompletion
if pendingUploads == 0 {
for url in data {
analytics.log(message: "Processing Batch:\n\(url.lastPathComponent)")
// enter the dispatch group
group.enter()
// set up the task
let uploadTask = httpClient.startBatchUpload(writeKey: analytics.configuration.values.writeKey, batch: url) { (result) in
switch result {
Expand All @@ -154,8 +152,6 @@ public class SegmentDestination: DestinationPlugin, Subscriber, FlushCompletion
self.cleanupUploads()
// call the completion
completion(self)
// leave the dispatch group
group.leave()
}
// we have a legit upload in progress now, so add it to our list.
if let upload = uploadTask {
Expand Down

0 comments on commit 4af5a8b

Please sign in to comment.