From 94169ded94ca58a0eabbcca95ecf9e1e1cc794b9 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Fri, 9 Oct 2020 14:09:01 +0200 Subject: [PATCH] Always launch the ping uploader on a background thread The uploader is always started after pings are already submitted (and assembled). The ping uploader can potentially block as it uploads several pings and even goes to sleep if told to wait (soon). By putting it on a background thread we ensure this doesn't stop any other work. This was already manually done in one place, but not in the others. This should be equivalent to Android's way of pushing this work to a workmanager job. Unfortunately this is near impossible to add a test for, as we would somehow need to instruct the uploader to block so that we can encounter the problems. Which we can't. So we have to live with the existing tests (they pass) & integration tests (on CI). --- glean-core/ios/Glean/Glean.swift | 8 +++----- glean-core/ios/Glean/Net/HttpPingUploader.swift | 9 +++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/glean-core/ios/Glean/Glean.swift b/glean-core/ios/Glean/Glean.swift index e826d94886..e2a957f0b5 100644 --- a/glean-core/ios/Glean/Glean.swift +++ b/glean-core/ios/Glean/Glean.swift @@ -158,7 +158,7 @@ public class Glean { // 1. Pings were submitted through Glean and it is ready to upload those pings; // 2. Upload is disabled, to upload a possible deletion-request ping. if pingSubmitted || !uploadEnabled { - HttpPingUploader(configuration: configuration).process() + HttpPingUploader.launch(configuration: configuration) } // Check for overdue metrics pings @@ -270,9 +270,7 @@ public class Glean { if originalEnabled && !enabled { // If uploading is disabled, we need to send the deletion-request ping - Dispatchers.shared.launchConcurrent { - HttpPingUploader(configuration: self.configuration!).process() - } + HttpPingUploader.launch(configuration: self.configuration!) } } } @@ -456,7 +454,7 @@ public class Glean { if submittedPing != 0 { if let config = self.configuration { - HttpPingUploader(configuration: config).process() + HttpPingUploader.launch(configuration: config) } } } diff --git a/glean-core/ios/Glean/Net/HttpPingUploader.swift b/glean-core/ios/Glean/Net/HttpPingUploader.swift index 8213472c50..9d8a8e7580 100644 --- a/glean-core/ios/Glean/Net/HttpPingUploader.swift +++ b/glean-core/ios/Glean/Net/HttpPingUploader.swift @@ -33,6 +33,15 @@ public class HttpPingUploader { self.config = configuration } + /// Launch a new ping uploader on the background thread. + /// + /// This function doesn't block. + static func launch(configuration: Configuration) { + Dispatchers.shared.launchConcurrent { + HttpPingUploader(configuration: configuration).process() + } + } + /// Synchronously upload a ping to Mozilla servers. /// /// - parameters: