From fe708a12596a496a6073ab2bc51c19461a24eb92 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Thu, 9 Sep 2021 10:04:33 -0700 Subject: [PATCH 1/3] Reduce file descriptor consumption --- Sources/Segment/Utilities/Storage.swift | 50 ++++++++++++------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Sources/Segment/Utilities/Storage.swift b/Sources/Segment/Utilities/Storage.swift index 97f1a45b..e0cd58cc 100644 --- a/Sources/Segment/Utilities/Storage.swift +++ b/Sources/Segment/Utilities/Storage.swift @@ -15,6 +15,8 @@ internal class Storage: Subscriber { let userDefaults: UserDefaults? static let MAXFILESIZE = 475000 // Server accepts max 500k per batch + private var fileHandle: FileHandle? = nil + init(store: Store, writeKey: String) { self.store = store self.writeKey = writeKey @@ -241,23 +243,18 @@ extension Storage { } syncQueue.sync { - do { - let jsonString = event.toString() - if let jsonData = jsonString.data(using: .utf8) { - let handle = try FileHandle(forWritingTo: storeFile) - handle.seekToEndOfFile() - // prepare for the next entry - if newFile == false { - handle.write(",".data(using: .utf8)!) - } - // write the data - handle.write(jsonData) - handle.closeFile() - } else { - assert(false, "Storage: Unable to convert event to json!") + let jsonString = event.toString() + if let jsonData = jsonString.data(using: .utf8) { + fileHandle?.seekToEndOfFile() + // prepare for the next entry + if newFile == false { + fileHandle?.write(",".data(using: .utf8)!) } - } catch { - assert(false, "Storage: failed to write event to \(storeFile), error: \(error)") + // write the data + fileHandle?.write(jsonData) + try? fileHandle?.synchronize() + } else { + assert(false, "Storage: Unable to convert event to json!") } } } @@ -266,7 +263,8 @@ extension Storage { syncQueue.sync { let contents = "{ \"batch\": [" do { - try contents.write(toFile: file.path, atomically: true, encoding: .utf8) + FileManager.default.createFile(atPath: file.path, contents: contents.data(using: .utf8)) + fileHandle = try FileHandle(forWritingTo: file) } catch { assert(false, "Storage: failed to write \(file), error: \(error)") } @@ -275,23 +273,25 @@ extension Storage { func finish(file: URL) { syncQueue.sync { - let tempFile = file.appendingPathExtension(Storage.tempExtension) - try? FileManager.default.copyItem(at: file, to: tempFile) - let sentAt = Date().iso8601() // write it to the existing file let fileEnding = "],\"sentAt\":\"\(sentAt)\"}" let endData = fileEnding.data(using: .utf8) - if let endData = endData, let handle = try? FileHandle(forWritingTo: tempFile) { - handle.seekToEndOfFile() - handle.write(endData) - handle.closeFile() + if let endData = endData { + fileHandle?.seekToEndOfFile() + fileHandle?.write(endData) + try? fileHandle?.synchronize() + fileHandle?.closeFile() + fileHandle = nil } else { // something is wrong with this file, maybe delete it? //assert(false, "Storage: event storage \(file) is messed up!") } - + + let tempFile = file.appendingPathExtension(Storage.tempExtension) + try? FileManager.default.copyItem(at: file, to: tempFile) + let currentFile: Int = (userDefaults?.integer(forKey: Constants.events.rawValue) ?? 0) + 1 userDefaults?.set(currentFile, forKey: Constants.events.rawValue) } From d7cc7a5f8c262596fa3e096e987e81c697ccfcc4 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Thu, 9 Sep 2021 10:08:13 -0700 Subject: [PATCH 2/3] Fix for tvOS prior to 13 --- Sources/Segment/Utilities/Storage.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Segment/Utilities/Storage.swift b/Sources/Segment/Utilities/Storage.swift index e0cd58cc..7a85f63b 100644 --- a/Sources/Segment/Utilities/Storage.swift +++ b/Sources/Segment/Utilities/Storage.swift @@ -252,7 +252,9 @@ extension Storage { } // write the data fileHandle?.write(jsonData) - try? fileHandle?.synchronize() + if #available(tvOS 13, *) { + try? fileHandle?.synchronize() + } } else { assert(false, "Storage: Unable to convert event to json!") } From 686ac20944bb3d9a279e0dea500a63b0bdd68527 Mon Sep 17 00:00:00 2001 From: Brandon Sneed Date: Thu, 9 Sep 2021 10:10:50 -0700 Subject: [PATCH 3/3] Another tvOS 13 fix --- Sources/Segment/Utilities/Storage.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/Segment/Utilities/Storage.swift b/Sources/Segment/Utilities/Storage.swift index 7a85f63b..73e1c387 100644 --- a/Sources/Segment/Utilities/Storage.swift +++ b/Sources/Segment/Utilities/Storage.swift @@ -283,7 +283,9 @@ extension Storage { if let endData = endData { fileHandle?.seekToEndOfFile() fileHandle?.write(endData) - try? fileHandle?.synchronize() + if #available(tvOS 13, *) { + try? fileHandle?.synchronize() + } fileHandle?.closeFile() fileHandle = nil } else {