Skip to content

Commit

Permalink
Merge pull request #642 from mixpanel/zihe-fix-flushrequest-crash
Browse files Browse the repository at this point in the history
Fix the crash for FlushRequest.sendRequest
  • Loading branch information
zihejia authored Apr 19, 2024
2 parents 6522fb2 + 460e1a1 commit 0cbd97e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
29 changes: 23 additions & 6 deletions Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,32 @@ class Flush: AppLifecycle {
var flushOnBackground = true
var _flushInterval = 0.0
var _flushBatchSize = APIConstants.maxBatchSize
private let flushIntervalReadWriteLock: DispatchQueue
private var _serverURL = BasePath.DefaultMixpanelAPI
private let flushRequestReadWriteLock: DispatchQueue


var serverURL: String {
get {
flushRequestReadWriteLock.sync {
return _serverURL
}
}
set {
flushRequestReadWriteLock.sync(flags: .barrier, execute: {
_serverURL = newValue
self.flushRequest.serverURL = newValue
})
}
}

var flushInterval: Double {
get {
flushIntervalReadWriteLock.sync {
flushRequestReadWriteLock.sync {
return _flushInterval
}
}
set {
flushIntervalReadWriteLock.sync(flags: .barrier, execute: {
flushRequestReadWriteLock.sync(flags: .barrier, execute: {
_flushInterval = newValue
})

Expand All @@ -52,9 +68,10 @@ class Flush: AppLifecycle {
}
}

required init(basePathIdentifier: String) {
self.flushRequest = FlushRequest(basePathIdentifier: basePathIdentifier)
flushIntervalReadWriteLock = DispatchQueue(label: "com.mixpanel.flush_interval.lock", qos: .utility, attributes: .concurrent, autoreleaseFrequency: .workItem)
required init(serverURL: String) {
self.flushRequest = FlushRequest(serverURL: serverURL)
_serverURL = serverURL
flushRequestReadWriteLock = DispatchQueue(label: "com.mixpanel.flush_interval.lock", qos: .utility, attributes: .concurrent, autoreleaseFrequency: .workItem)
}

func flushQueue(_ queue: Queue, type: FlushType, headers: [String: String], queryItems: [URLQueryItem]) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/FlushRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FlushRequest: Network {
parse: responseParser)
var result = false
let semaphore = DispatchSemaphore(value: 0)
flushRequestHandler(BasePath.getServerURL(identifier: basePathIdentifier),
flushRequestHandler(serverURL,
resource: resource,
completion: { success in
result = success
Expand Down
5 changes: 2 additions & 3 deletions Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
/// https://api.mixpanel.com.
open var serverURL = BasePath.DefaultMixpanelAPI {
didSet {
BasePath.namedBasePaths[name] = serverURL
flushInstance.serverURL = serverURL
}
}

Expand Down Expand Up @@ -323,7 +323,6 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
trackAutomaticEventsEnabled = trackAutomaticEvents
if let serverURL = serverURL {
self.serverURL = serverURL
BasePath.namedBasePaths[name] = serverURL
}
self.proxyServerDelegate = proxyServerDelegate
#if DEBUG
Expand All @@ -347,7 +346,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
self.useUniqueDistinctId = useUniqueDistinctId

readWriteLock = ReadWriteLock(label: "com.mixpanel.globallock")
flushInstance = Flush(basePathIdentifier: name)
flushInstance = Flush(serverURL: self.serverURL)
sessionMetadata = SessionMetadata(trackingQueue: trackingQueue)
trackInstance = Track(apiToken: self.apiToken,
instanceName: self.name,
Expand Down
11 changes: 3 additions & 8 deletions Sources/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation

struct BasePath {
static let DefaultMixpanelAPI = "https://api.mixpanel.com"
static var namedBasePaths = [String: String]()

static func buildURL(base: String, path: String, queryItems: [URLQueryItem]?) -> URL? {
guard let url = URL(string: base) else {
Expand All @@ -25,10 +24,6 @@ struct BasePath {
components.percentEncodedQuery = components.percentEncodedQuery?.replacingOccurrences(of: "+", with: "%2B")
return components.url
}

static func getServerURL(identifier: String) -> String {
return namedBasePaths[identifier] ?? DefaultMixpanelAPI
}
}

enum RequestMethod: String {
Expand Down Expand Up @@ -64,10 +59,10 @@ public struct ServerProxyResource {

class Network {

let basePathIdentifier: String
var serverURL: String

required init(basePathIdentifier: String) {
self.basePathIdentifier = basePathIdentifier
required init(serverURL: String) {
self.serverURL = serverURL
}

class func apiRequest<A>(base: String,
Expand Down

0 comments on commit 0cbd97e

Please sign in to comment.