[REQ] [swift5] Reuse URLSessions for requests
Created by: fl034
Is your feature request related to a problem? Please describe.
After doing a little research on best practices for URLSession, I stumbled upon this: https://developer.apple.com/videos/play/wwdc2018/714/?time=1646
Now let's see how using fewer URLSession objects can help reduce latency. All the benefits of connections we use that we just discussed in the previous slides are applicable only if you use the same URLSession object to create your tasks. It's also important to know that every URLSession object has a connection pool and when you create multiple of these URLSession objects, you don't get any benefit of connection to use. It's also important to note that the URLSession objects are fairly expensive to create and have a non-trivial memory footprint.
Describe the solution you'd like
- I'd suggest having a dictionary of
[Configuration: URLSession]
that will return you an existing session object for your configuration. - Configuration must be a hash of
URLSessionConfiguration
, and optionalURLSessionDelegate
andOperationQueue
as you can initialize yourURLSession
with those parameters.
Describe alternatives you've considered
- We could leave it as it is now, since you can achieve the same thing by subclassing the request builder.
- Also, possible gains may not be noticeable since today's hardware is extremely fast.
Additional context
- the client with Alamofire option doesn't reuse url sessions either
- here we're creating a new
SessionManager
object for each request, which under the hood of Alamofire 4.x, creates a newURLSession
: https://github.com/OpenAPITools/openapi-generator/blob/150e24dc553a8ea5230ffb938ed3e6020e972faa/modules/openapi-generator/src/main/resources/swift4/AlamofireImplementations.mustache#L64
- here we're creating a new