Allow NSURLSessions to be reused on Apple platforms#1006
Open
lawrencewuskyboxlabs wants to merge 1 commit into
Open
Allow NSURLSessions to be reused on Apple platforms#1006lawrencewuskyboxlabs wants to merge 1 commit into
lawrencewuskyboxlabs wants to merge 1 commit into
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current implementation of
AppleHttpProviderwill spin up a newhttp_task_appleand thus a newNSURLSessionwith each call that is performed. This somewhat goes against Apple's recommended practice of reusing the sessions when possible and is contributing 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.Key changes:
AppleHttpSessionManagerhas been introduced which largely replaces the functionality provided by the now-deletedhttp_task_apple. Rather than individualhttp_tasks being created, there is only oneManagerwhich is responsible for creating sessions, creating and runningNSURLSessionTaskson the appropriate sessions, and closing sessions when all their tasks are completed. Sessions are represented byAppleHttpSessions and are identified by their timeout period, and each session also has a corresponding map ofAppleHttpTaskContextskeyed byNSUIntegertask identifiers. This is to keep track of theHCCallHandleandXAsyncBlockthat should be used for each task, since originally eachhttp_taskwould have this info.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 in the session, theDelegatealso maintains its own map ofTaskContextskeyed by task identifiers. On a per-task basis, it needs to track the call handle and, for reporting purposes, the expected download size. 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 initial reply 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_task, 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 libhttpclient that have the context set so this also brings Apple more in line with the majority.I'm open to any feedback on this, thanks!