Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the crash for FlushRequest.sendRequest #642

Merged
merged 1 commit into from Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 23 additions & 6 deletions Sources/Flush.swift
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
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
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
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