Created
November 9, 2018 23:38
-
-
Save kingst/8f959e5dde6f7c1b8e5b6ae1f79271d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let dictLock = DispatchSemaphore(value: 1) | |
var originSemaphores: [String: DispatchSemaphore] = [:] | |
// make a non blocking HTTP request | |
func makeHttpRequest(url: String, origin: String, complete: @escaping (() -> Void)) { | |
dictLock.wait() | |
if originSemaphores[origin] == nil { | |
originSemaphores[origin] = DispatchSemaphore(value: 2) | |
} | |
let semaphore = originSemaphores[origin]! | |
dictLock.signal() | |
DispatchQueue.global(qos: .default).async { | |
semaphore.wait() | |
blockingNetworkRequest(url: url) | |
complete() | |
semaphore.signal() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment