Skip to content
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

Make Segment.io flush asynchronous #259

Closed
wants to merge 5 commits into from
Closed

Make Segment.io flush asynchronous #259

wants to merge 5 commits into from

Conversation

bsneed
Copy link
Contributor

@bsneed bsneed commented Oct 16, 2023

It's my suspicion that file IO is the slow-down when calling flush() from the main thread with a significant number of events present. As such, in this fix async operation was done in the segment destination instead of the upper level analytics method to prevent unknowns from happening with device mode destinations. Those device mode destinations (now or in the future) may not be as thread-safe, or have expectations about running on the main thread, etc.

A future-todo would be to make asynchronous file iO standard and remove usage of FileManager and the like.

@bsneed
Copy link
Contributor Author

bsneed commented Oct 16, 2023

Some tests need to be fixed due to the new async nature of flush and tests depending on the previous sync.

@@ -113,47 +114,50 @@ public class SegmentDestination: DestinationPlugin, Subscriber {
}

public func flush() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this could be simplified to:

public func flush() {
    flushQueue.async { [weak self] in
        self?._flush()
    }
}

// Existing flush (no need for this comment-- just making the point)
private func _flush() {
  // existing code...
}

@bsneed
Copy link
Contributor Author

bsneed commented Oct 18, 2023

@cprince-foreflight can you give the branch a try and let me know the results?

@cprince-foreflight
Copy link
Contributor

@cprince-foreflight can you give the branch a try and let me know the results?

Just tried a smoke test of this branch with our app and am still seeing the expected Segment events in the Segment test debugger. So, no issues apparent. (Though it takes longer-- with builds to our beta testers-- to get real tests of the issue that brought this about. I.e., tests with the app in flight).

Any ideas of when (a) this will be merged and (b) when a tagged release with this will happen?

Thanks!

@bsneed
Copy link
Contributor Author

bsneed commented Oct 19, 2023

@cprince-foreflight Some version of this likely sometime next week. We're having an internal dialogue about whether we want to make sync vs. async a choice at the API level for flush. There's scenarios that demand one or the other and the lib wasn't structured to take this into account.

@bsneed
Copy link
Contributor Author

bsneed commented Nov 16, 2023

Closing this in favor of #269

@bsneed bsneed closed this Nov 16, 2023
@bsneed bsneed deleted the bsneed/async-flush branch March 8, 2024 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flush running on main thread and may take a long time
2 participants