Rapid file download using concurrent connections
Flock is a Swift package which provides methods for downloading a file from multiple connections, by taking advantage of the Range
HTTP header and structured concurrency.
The objective is to speed up downloads by maximizing core usage and download bandwidth.
Flock can be included in your project via Swift Package Manager. Add the following line to the dependencies in your Package.swift
file:
.package(url: "https://github.com/qasim/Flock", exact: "0.2.0"),
Then, include Flock
as a dependency for the target which requires it:
.target(
name: "<target>",
dependencies: [
"Flock",
]
),
Finally, add import Flock
to your source code.
Flock extends URLSession
for your convenience, with reasonable defaults.
For example, to download a file using up to as many connections as there are active processors:
try await URLSession.shared.flock(from: URL(string: "http://212.183.159.230/1GB.zip")!)
To track progress of your download as it transfers, pass a FlockProgressDelegate
:
class ExampleProgressDelegate: FlockProgressDelegate {
func request(_ request: URLRequest, didReceiveBytes bytesReceived: Int, totalBytesReceived: Int, totalBytesExpected: Int?) {
print("\(totalBytesReceived) bytes downloaded")
}
}
try await URLSession.shared.flock(
from: URL(string: "http://212.183.159.230/1GB.zip")!,
progressDelegate: ExampleProgressDelegate()
)
For more details, have a look at the Flock API reference.
Flock also provides a command-line interface for downloading files inside a shell.
brew install qasim/tools/flock
flock --help
Flock comes with no warranty. You must use it at your own risk.
Flock works fastest when a file being downloaded has multiple file servers backing its URL. Alternatively, support for manually providing mirrors is planned as well.