no message

This commit is contained in:
overtake 2020-07-13 17:34:50 +03:00
parent 3dcc32c3c3
commit 1fcd271df1
4 changed files with 69 additions and 21 deletions

View File

@ -188,8 +188,8 @@ public final class MediaBox {
let _ = self.ensureDirectoryCreated let _ = self.ensureDirectoryCreated
} }
public func setMaxStoreTimes(general: Int32, shortLived: Int32) { public func setMaxStoreTimes(general: Int32, shortLived: Int32, gigabytesLimit: Int32) {
self.timeBasedCleanup.setMaxStoreTimes(general: general, shortLived: shortLived) self.timeBasedCleanup.setMaxStoreTimes(general: general, shortLived: shortLived, gigabytesLimit: gigabytesLimit)
} }
private func fileNameForId(_ id: MediaResourceId) -> String { private func fileNameForId(_ id: MediaResourceId) -> String {

View File

@ -3,15 +3,15 @@ import SwiftSignalKit
private typealias SignalKitTimer = SwiftSignalKit.Timer private typealias SignalKitTimer = SwiftSignalKit.Timer
private func scanFiles(at path: String, olderThan minTimestamp: Int32, _ f: (String) -> Void) { private func scanFiles(at path: String, olderThan minTimestamp: Int32, anyway: ((String, Int, Int32)) -> Void, unlink f: (String) -> Void) {
guard let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: [.contentModificationDateKey, .isDirectoryKey], options: [.skipsSubdirectoryDescendants], errorHandler: nil) else { guard let enumerator = FileManager.default.enumerator(at: URL(fileURLWithPath: path), includingPropertiesForKeys: [.contentModificationDateKey, .isDirectoryKey, .fileSizeKey], options: [.skipsSubdirectoryDescendants], errorHandler: nil) else {
return return
} }
while let item = enumerator.nextObject() { while let item = enumerator.nextObject() {
guard let url = item as? NSURL else { guard let url = item as? NSURL else {
continue continue
} }
guard let resourceValues = try? url.resourceValues(forKeys: [.contentModificationDateKey, .isDirectoryKey]) else { guard let resourceValues = try? url.resourceValues(forKeys: [.contentModificationDateKey, .isDirectoryKey, .fileSizeKey]) else {
continue continue
} }
if let value = resourceValues[.isDirectoryKey] as? Bool, value { if let value = resourceValues[.isDirectoryKey] as? Bool, value {
@ -23,6 +23,11 @@ private func scanFiles(at path: String, olderThan minTimestamp: Int32, _ f: (Str
f(file) f(file)
} }
} }
if let file = url.path {
if let size = (resourceValues[.fileSizeKey] as? NSNumber)?.intValue {
anyway((file, size, Int32(value.timeIntervalSince1970)))
}
}
} }
} }
} }
@ -37,8 +42,22 @@ private final class TimeBasedCleanupImpl {
private var generalMaxStoreTime: Int32? private var generalMaxStoreTime: Int32?
private var shortLivedMaxStoreTime: Int32? private var shortLivedMaxStoreTime: Int32?
private var gigabytesLimit: Int32?
private let scheduledScanDisposable = MetaDisposable() private let scheduledScanDisposable = MetaDisposable()
private struct GeneralFile : Comparable, Equatable {
let file: String
let size: Int
let timestamp:Int32
static func == (lhs: GeneralFile, rhs: GeneralFile) -> Bool {
return lhs.timestamp == rhs.timestamp && lhs.size == rhs.size && lhs.file == rhs.file
}
static func < (lhs: GeneralFile, rhs: GeneralFile) -> Bool {
return lhs.timestamp < rhs.timestamp
}
}
init(queue: Queue, generalPaths: [String], shortLivedPaths: [String]) { init(queue: Queue, generalPaths: [String], shortLivedPaths: [String]) {
self.queue = queue self.queue = queue
self.generalPaths = generalPaths self.generalPaths = generalPaths
@ -51,38 +70,60 @@ private final class TimeBasedCleanupImpl {
self.scheduledScanDisposable.dispose() self.scheduledScanDisposable.dispose()
} }
func setMaxStoreTimes(general: Int32, shortLived: Int32) { func setMaxStoreTimes(general: Int32, shortLived: Int32, gigabytesLimit: Int32) {
if self.generalMaxStoreTime != general || self.shortLivedMaxStoreTime != shortLived { if self.generalMaxStoreTime != general || self.shortLivedMaxStoreTime != shortLived || self.gigabytesLimit != gigabytesLimit {
self.generalMaxStoreTime = general self.generalMaxStoreTime = general
self.gigabytesLimit = gigabytesLimit
self.shortLivedMaxStoreTime = shortLived self.shortLivedMaxStoreTime = shortLived
self.resetScan(general: general, shortLived: shortLived) self.resetScan(general: general, shortLived: shortLived, gigabytesLimit: gigabytesLimit)
} }
} }
private func resetScan(general: Int32, shortLived: Int32) { private func resetScan(general: Int32, shortLived: Int32, gigabytesLimit: Int32) {
let generalPaths = self.generalPaths let generalPaths = self.generalPaths
let shortLivedPaths = self.shortLivedPaths let shortLivedPaths = self.shortLivedPaths
let scanOnce = Signal<Never, NoError> { subscriber in let scanOnce = Signal<Never, NoError> { subscriber in
DispatchQueue.global(qos: .utility).async { DispatchQueue.global(qos: .utility).async {
var removedShortLivedCount: Int = 0 var removedShortLivedCount: Int = 0
var removedGeneralCount: Int = 0 var removedGeneralCount: Int = 0
var removedGeneralLimitCount: Int = 0
let timestamp = Int32(Date().timeIntervalSince1970) let timestamp = Int32(Date().timeIntervalSince1970)
let bytesLimit = UInt64(gigabytesLimit) * 1024 * 1024 * 1024
let oldestShortLivedTimestamp = timestamp - shortLived let oldestShortLivedTimestamp = timestamp - shortLived
let oldestGeneralTimestamp = timestamp - general let oldestGeneralTimestamp = timestamp - general
for path in shortLivedPaths { for path in shortLivedPaths {
scanFiles(at: path, olderThan: oldestShortLivedTimestamp, { file in scanFiles(at: path, olderThan: oldestShortLivedTimestamp, anyway: { _, _, _ in
}, unlink: { file in
removedShortLivedCount += 1 removedShortLivedCount += 1
unlink(file) unlink(file)
}) })
} }
var checkFiles:[GeneralFile] = []
var totalLimitSize: Int = 0
for path in generalPaths { for path in generalPaths {
scanFiles(at: path, olderThan: oldestGeneralTimestamp, { file in scanFiles(at: path, olderThan: oldestGeneralTimestamp, anyway: { file, size, timestamp in
checkFiles.append(GeneralFile(file: file, size: size, timestamp: timestamp))
totalLimitSize += size
}, unlink: { file in
removedGeneralCount += 1 removedGeneralCount += 1
unlink(file) unlink(file)
}) })
} }
if removedShortLivedCount != 0 || removedGeneralCount != 0 {
print("[TimeBasedCleanup] removed \(removedShortLivedCount) short-lived files, \(removedGeneralCount) general files") for item in checkFiles.sorted(by: <) {
if totalLimitSize > bytesLimit {
unlink(item.file)
removedGeneralLimitCount += 1
totalLimitSize -= item.size
}
}
if removedShortLivedCount != 0 || removedGeneralCount != 0 || removedGeneralLimitCount != 0 {
print("[TimeBasedCleanup] removed \(removedShortLivedCount) short-lived files, \(removedGeneralCount) general files, \(removedGeneralLimitCount) limit files")
} }
subscriber.putCompletion() subscriber.putCompletion()
} }
@ -148,9 +189,9 @@ final class TimeBasedCleanup {
} }
} }
func setMaxStoreTimes(general: Int32, shortLived: Int32) { func setMaxStoreTimes(general: Int32, shortLived: Int32, gigabytesLimit: Int32) {
self.impl.with { impl in self.impl.with { impl in
impl.setMaxStoreTimes(general: general, shortLived: shortLived) impl.setMaxStoreTimes(general: general, shortLived: shortLived, gigabytesLimit: gigabytesLimit)
} }
} }
} }

View File

@ -2,21 +2,25 @@ import Postbox
public struct CacheStorageSettings: PreferencesEntry, Equatable { public struct CacheStorageSettings: PreferencesEntry, Equatable {
public let defaultCacheStorageTimeout: Int32 public let defaultCacheStorageTimeout: Int32
public let defaultCacheStorageLimitGigabytes: Int32
public static var defaultSettings: CacheStorageSettings { public static var defaultSettings: CacheStorageSettings {
return CacheStorageSettings(defaultCacheStorageTimeout: Int32.max) return CacheStorageSettings(defaultCacheStorageTimeout: Int32.max, defaultCacheStorageLimitGigabytes: Int32.max)
} }
public init(defaultCacheStorageTimeout: Int32) { public init(defaultCacheStorageTimeout: Int32, defaultCacheStorageLimitGigabytes: Int32) {
self.defaultCacheStorageTimeout = defaultCacheStorageTimeout self.defaultCacheStorageTimeout = defaultCacheStorageTimeout
self.defaultCacheStorageLimitGigabytes = defaultCacheStorageLimitGigabytes
} }
public init(decoder: PostboxDecoder) { public init(decoder: PostboxDecoder) {
self.defaultCacheStorageTimeout = decoder.decodeInt32ForKey("dt", orElse: Int32.max) self.defaultCacheStorageTimeout = decoder.decodeInt32ForKey("dt", orElse: Int32.max)
self.defaultCacheStorageLimitGigabytes = decoder.decodeInt32ForKey("dl", orElse: Int32.max)
} }
public func encode(_ encoder: PostboxEncoder) { public func encode(_ encoder: PostboxEncoder) {
encoder.encodeInt32(self.defaultCacheStorageTimeout, forKey: "dt") encoder.encodeInt32(self.defaultCacheStorageTimeout, forKey: "dt")
encoder.encodeInt32(self.defaultCacheStorageLimitGigabytes, forKey: "dl")
} }
public func isEqual(to: PreferencesEntry) -> Bool { public func isEqual(to: PreferencesEntry) -> Bool {
@ -28,10 +32,13 @@ public struct CacheStorageSettings: PreferencesEntry, Equatable {
} }
public static func ==(lhs: CacheStorageSettings, rhs: CacheStorageSettings) -> Bool { public static func ==(lhs: CacheStorageSettings, rhs: CacheStorageSettings) -> Bool {
return lhs.defaultCacheStorageTimeout == rhs.defaultCacheStorageTimeout return lhs.defaultCacheStorageTimeout == rhs.defaultCacheStorageTimeout && lhs.defaultCacheStorageLimitGigabytes == rhs.defaultCacheStorageLimitGigabytes
} }
public func withUpdatedDefaultCacheStorageTimeout(_ defaultCacheStorageTimeout: Int32) -> CacheStorageSettings { public func withUpdatedDefaultCacheStorageTimeout(_ defaultCacheStorageTimeout: Int32) -> CacheStorageSettings {
return CacheStorageSettings(defaultCacheStorageTimeout: defaultCacheStorageTimeout) return CacheStorageSettings(defaultCacheStorageTimeout: defaultCacheStorageTimeout, defaultCacheStorageLimitGigabytes: self.defaultCacheStorageLimitGigabytes)
}
public func withUpdatedDefaultCacheStorageLimitGigabytes(_ defaultCacheStorageLimitGigabytes: Int32) -> CacheStorageSettings {
return CacheStorageSettings(defaultCacheStorageTimeout: self.defaultCacheStorageTimeout, defaultCacheStorageLimitGigabytes: defaultCacheStorageLimitGigabytes)
} }
} }

View File

@ -1121,7 +1121,7 @@ public class Account {
return return
} }
let settings: CacheStorageSettings = sharedData.entries[SharedDataKeys.cacheStorageSettings] as? CacheStorageSettings ?? CacheStorageSettings.defaultSettings let settings: CacheStorageSettings = sharedData.entries[SharedDataKeys.cacheStorageSettings] as? CacheStorageSettings ?? CacheStorageSettings.defaultSettings
mediaBox.setMaxStoreTimes(general: settings.defaultCacheStorageTimeout, shortLived: 60 * 60) mediaBox.setMaxStoreTimes(general: settings.defaultCacheStorageTimeout, shortLived: 60 * 60, gigabytesLimit: settings.defaultCacheStorageLimitGigabytes)
}) })
let _ = masterNotificationsKey(masterNotificationKeyValue: self.masterNotificationKey, postbox: self.postbox, ignoreDisabled: false).start(next: { key in let _ = masterNotificationsKey(masterNotificationKeyValue: self.masterNotificationKey, postbox: self.postbox, ignoreDisabled: false).start(next: { key in