Skip to content

Instantly share code, notes, and snippets.

@kingst
Created November 9, 2018 23:38
Show Gist options
  • Save kingst/8f959e5dde6f7c1b8e5b6ae1f79271d8 to your computer and use it in GitHub Desktop.
Save kingst/8f959e5dde6f7c1b8e5b6ae1f79271d8 to your computer and use it in GitHub Desktop.
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