Skip to content

Commit 74839c7

Browse files
bsneedBrandon Sneed
andauthored
Reduce file descriptor consumption (#59)
* Reduce file descriptor consumption * Fix for tvOS prior to 13 * Another tvOS 13 fix Co-authored-by: Brandon Sneed <[email protected]>
1 parent 54c9943 commit 74839c7

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

Sources/Segment/Utilities/Storage.swift

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ internal class Storage: Subscriber {
1515
let userDefaults: UserDefaults?
1616
static let MAXFILESIZE = 475000 // Server accepts max 500k per batch
1717

18+
private var fileHandle: FileHandle? = nil
19+
1820
init(store: Store, writeKey: String) {
1921
self.store = store
2022
self.writeKey = writeKey
@@ -241,23 +243,20 @@ extension Storage {
241243
}
242244

243245
syncQueue.sync {
244-
do {
245-
let jsonString = event.toString()
246-
if let jsonData = jsonString.data(using: .utf8) {
247-
let handle = try FileHandle(forWritingTo: storeFile)
248-
handle.seekToEndOfFile()
249-
// prepare for the next entry
250-
if newFile == false {
251-
handle.write(",".data(using: .utf8)!)
252-
}
253-
// write the data
254-
handle.write(jsonData)
255-
handle.closeFile()
256-
} else {
257-
assert(false, "Storage: Unable to convert event to json!")
246+
let jsonString = event.toString()
247+
if let jsonData = jsonString.data(using: .utf8) {
248+
fileHandle?.seekToEndOfFile()
249+
// prepare for the next entry
250+
if newFile == false {
251+
fileHandle?.write(",".data(using: .utf8)!)
258252
}
259-
} catch {
260-
assert(false, "Storage: failed to write event to \(storeFile), error: \(error)")
253+
// write the data
254+
fileHandle?.write(jsonData)
255+
if #available(tvOS 13, *) {
256+
try? fileHandle?.synchronize()
257+
}
258+
} else {
259+
assert(false, "Storage: Unable to convert event to json!")
261260
}
262261
}
263262
}
@@ -266,7 +265,8 @@ extension Storage {
266265
syncQueue.sync {
267266
let contents = "{ \"batch\": ["
268267
do {
269-
try contents.write(toFile: file.path, atomically: true, encoding: .utf8)
268+
FileManager.default.createFile(atPath: file.path, contents: contents.data(using: .utf8))
269+
fileHandle = try FileHandle(forWritingTo: file)
270270
} catch {
271271
assert(false, "Storage: failed to write \(file), error: \(error)")
272272
}
@@ -275,23 +275,27 @@ extension Storage {
275275

276276
func finish(file: URL) {
277277
syncQueue.sync {
278-
let tempFile = file.appendingPathExtension(Storage.tempExtension)
279-
try? FileManager.default.copyItem(at: file, to: tempFile)
280-
281278
let sentAt = Date().iso8601()
282279

283280
// write it to the existing file
284281
let fileEnding = "],\"sentAt\":\"\(sentAt)\"}"
285282
let endData = fileEnding.data(using: .utf8)
286-
if let endData = endData, let handle = try? FileHandle(forWritingTo: tempFile) {
287-
handle.seekToEndOfFile()
288-
handle.write(endData)
289-
handle.closeFile()
283+
if let endData = endData {
284+
fileHandle?.seekToEndOfFile()
285+
fileHandle?.write(endData)
286+
if #available(tvOS 13, *) {
287+
try? fileHandle?.synchronize()
288+
}
289+
fileHandle?.closeFile()
290+
fileHandle = nil
290291
} else {
291292
// something is wrong with this file, maybe delete it?
292293
//assert(false, "Storage: event storage \(file) is messed up!")
293294
}
294-
295+
296+
let tempFile = file.appendingPathExtension(Storage.tempExtension)
297+
try? FileManager.default.copyItem(at: file, to: tempFile)
298+
295299
let currentFile: Int = (userDefaults?.integer(forKey: Constants.events.rawValue) ?? 0) + 1
296300
userDefaults?.set(currentFile, forKey: Constants.events.rawValue)
297301
}

0 commit comments

Comments
 (0)