Fix thread safety issues

This commit is contained in:
nielsandriesse 2020-06-12 09:49:49 +10:00
parent 1b61330669
commit d0a3758ff4
4 changed files with 81 additions and 6 deletions

View file

@ -1,13 +1,58 @@
import PromiseKit
public extension LokiAPI {
private static var snodeVersion: [LokiAPITarget:String] = [:]
fileprivate static let seedNodePool: Set<String> = [ "https://storage.seed1.loki.network", "https://storage.seed3.loki.network", "https://public.loki.foundation" ]
internal static var snodeFailureCount: [LokiAPITarget:UInt] = [:]
internal static var snodePool: Set<LokiAPITarget> = []
internal static var swarmCache: [String:[LokiAPITarget]] = [:] // TODO: Make this set based?
internal static var _snodeFailureCount: [LokiAPITarget:UInt] = [:]
internal static var snodeFailureCount: [LokiAPITarget:UInt] {
get {
let (promise, seal) = Promise<[LokiAPITarget:UInt]>.pending()
stateQueue.async {
seal.fulfill(_snodeFailureCount)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_snodeFailureCount = newValue
}
}
}
// TODO: Read/write this directly from/to the database
internal static var _snodePool: Set<LokiAPITarget> = []
internal static var snodePool: Set<LokiAPITarget> {
get {
let (promise, seal) = Promise<Set<LokiAPITarget>>.pending()
stateQueue.async {
seal.fulfill(_snodePool)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_snodePool = newValue
}
}
}
// TODO: Read/write this directly from/to the database
internal static var _swarmCache: [String:[LokiAPITarget]] = [:]
internal static var swarmCache: [String:[LokiAPITarget]] {
get {
let (promise, seal) = Promise<[String:[LokiAPITarget]]>.pending()
stateQueue.async {
seal.fulfill(_swarmCache)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_swarmCache = newValue
}
}
}
// MARK: Settings
private static let minimumSnodePoolCount = 32

View file

@ -2,6 +2,7 @@ import PromiseKit
@objc(LKAPI)
public final class LokiAPI : NSObject {
internal static let stateQueue = DispatchQueue(label: "LokiAPI.stateQueue", qos: .userInitiated)
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }

View file

@ -1,9 +1,24 @@
import PromiseKit
@objc(LKMentionsManager)
public final class MentionsManager : NSObject {
@objc public static var _userPublicKeyCache: [String:Set<String>] = [:]
/// A mapping from thread ID to set of user hex encoded public keys.
@objc public static var userPublicKeyCache: [String:Set<String>] = [:]
@objc public static var userPublicKeyCache: [String:Set<String>] {
get {
let (promise, seal) = Promise<[String:Set<String>]>.pending()
LokiAPI.stateQueue.async {
seal.fulfill(_userPublicKeyCache)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_userPublicKeyCache = newValue
}
}
}
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }

View file

@ -15,8 +15,22 @@ import PromiseKit
@objc(LKMultiDeviceProtocol)
public final class MultiDeviceProtocol : NSObject {
public static var _lastDeviceLinkUpdate: [String:Date] = [:]
/// A mapping from hex encoded public key to date updated.
public static var lastDeviceLinkUpdate: [String:Date] = [:]
public static var lastDeviceLinkUpdate: [String:Date] {
get {
let (promise, seal) = Promise<[String:Date]>.pending()
LokiAPI.stateQueue.async {
seal.fulfill(_lastDeviceLinkUpdate)
}
return try! promise.wait()
}
set {
LokiAPI.stateQueue.async {
_lastDeviceLinkUpdate = newValue
}
}
}
internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() }