Allow reuse of Apple's NSURLSessions#1
Conversation
| std::weak_ptr<AppleHttpSessionManager> weak_this = shared_from_this(); | ||
|
|
||
| SessionDelegate* delegate = [SessionDelegate sessionDelegateWithCallHandleRetriever:^HCCallHandle(uint32_t sessionTimeout, NSUInteger taskIdentifier) { | ||
| if (auto me = weak_this.lock()) |
| std::unique_lock<std::shared_mutex> uniqueLock(m_httpSessionsMutex); | ||
|
|
||
| NSURLSession* session = nil; | ||
| auto httpSessionIter = m_httpSessions.find(timeoutInSeconds); |
There was a problem hiding this comment.
I'm confused why the key for this map is timeoutInSeconds, couldn't this collide, like, a lot?
There was a problem hiding this comment.
This is because with the original implementation of each call causing a session to be made, the only differences in those sessions were:
- how the delegate handled events (writing to a particular call's response body, triggering some completion handler, etc.)
- the timeout the session would allow for a task running on it
The delegate's handling is pretty self-contained and the session itself doesn't really interact with its details. However the timeout is something that is defined in the session's config and can't be changed after the session is created. So I figured this would be the best way to identify sessions and choose which ones can be reused among calls; if a session already exists with the same timeout, then the call's corresponding task can run on it and let the delegate handle the per-task specifics.
The current implementation of
AppleHttpProviderwill spin up a newNSURLSessionwith each call that is performed. This somewhat goes against Apple's recommended practice of reusing the sessions when possible and is leading to out of memory issues in cases where a high number of concurrent calls are active, along with possibly another memory leak due to the sessions not being manually invalidated. These changes rework theProviderand theSessionDelegateto allow the same session to be used for calls that share the same timeout period, as this is the only difference in the currently-created session configs.AppleHttpSessionManagerhas been introduced which largely replaces the functionality provided by the oldhttp_task_apple. Rather than individualhttp_tasks being created, there is only oneManagerwhich is responsible for creating sessions, creating and runningNSURLSessionTasks on the appropriate sessions, and closing sessions when all their tasks are completed. Sessions are identified by their timeout period, and each session also has a corresponding map ofAppleHttpTaskContexts keyed byNSUIntegertask identifiers. This is to keep track of theHCCallHandleandXAsyncBlockthat should be used for each task, since originally thehttp_taskhad this info on an individual basis.AppleHttpProviderowns theManagerthrough a shared pointer and is essentially a wrapper around theManager, due to the required opaqueness of theNSObjectstheManagerhas to use. The shared pointer is to allow weak pointers to be safely made.SessionDelegate, but since there can now be multiple tasks running, theDelegatealso maintains its own map ofTaskContexts keyed by task identifiers. It needs to track the call handle and, for reporting purposes, the expected download size on a per-task basis. The call handle can be given by theManagerafter a task is created since theManageris also responsible for starting the task. The download size can be cached upon the first response the task receives.Manager._dataToDownloadin theDelegate, which was only used to report the amount of bytes downloaded so far, is deemed unnecessary because the call handle'sresponseBodyBytesshould match it after it's written to.http_tasks, the call handle no longer has context set by theProvider. This is considered to prevent another possible memory leak in the old implementation, where the release of thehttp_taskin theProvidermeant it was up to the call handle to clean up thehttp_task. Apple and Android are the only platforms in libhttp that have the context set so this also brings Apple more in line with the majority.