-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement URLSessionConfiguration.ephemeral #2125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement URLSessionConfiguration.ephemeral #2125
Conversation
@swift-ci test |
@karthikkeyan Could you please review this? |
@swift-ci please test |
if let range = bundleName.range(of: ".", options: .backwards, range: nil, locale: nil) { | ||
bundleName = String(bundleName[..<range.lowerBound]) | ||
} | ||
let cookieFolderPath = _CFXDGCreateDataHomePath()._swiftObject + "/" + bundleName |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am still in the learning phase, ignore this comment if it is wrong,
After looking the code from TestHTTPCookieStorage.test_cookieInXDGSpecPath()
, it seems you are missing,
if let xdg_data_home = getenv("XDG_DATA_HOME") {
// XDG Path
} else {
// Non XDG Path
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karthikkeyan For ephemeral storage, we don't persist the cookies in a file. The XDG spec standardises some common locations on the file system. It's not relevant if we don't want to store cookies to a file. I hope I got your question right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response, the statement that creates the cookie folder's path, is inside if !isEphemeral
if !isEphemeral {
// ...
let cookieFolderPath = _CFXDGCreateDataHomePath()._swiftObject + "/" + bundleName
// ...
}
We can leave that discussion for different PR as the code is not relevant to the intention of this PR.
// TODO: urlCache and urlCredentialStorage should also be ephemeral/in-memory | ||
// URLCache and URLCredentialStorage are still unimplemented | ||
let ephemeralConfiguration = URLSessionConfiguration() | ||
ephemeralConfiguration.httpCookieStorage = HTTPCookieStorage.ephemeralStorage() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just printed all the public property of ephemeral configuration, and noticed some minor discrepancies of values between the existing implementation and yours,
- value for
httpMaximumConnectionsPerHost
is 4, but it is 6 in yours protocolClass
array contains[_NSURLHTTPProtocol, _NSURLDataProtocol, _NSURLFTPProtocol, _NSURLFileProtocol, NSAboutURLProtocol]
, but it is just[SwiftFoundation._HTTPURLProtocol]
in yours.
Hope this helps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right. The protocolClass
array contains all the default native
protocol implementations that come with URLSession
. These are http
, ftp
, file
and data
. We currently have only http
implemented and there is a pull request for ftp
too. This array will be populated as and when the respective implementations are available. Thanks for noticing this, though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@karthikkeyan I checked the value for httpMaximumConnectionsPerHost
on Darwin with the latest dev snapshot. It is 6.
Welcome to Apple Swift version 5.0-dev (LLVM 082dec2e22, Swift 1e5637f841).
Type :help for assistance.
1> import Foundation
2> URLSessionConfiguration.ephemeral.httpMaximumConnectionsPerHost
$R0: Int = 6
@swift-ci test Linux |
@shahmishal The PR build seems to be failing with unrelated issues. Is it possible to do a clean build? I guess |
@swift-ci test |
|
@shahmishal Can you please help me do a clean build here? The failures seem unrelated |
@swift-ci test |
Foundation/HTTPCookieStorage.swift
Outdated
@@ -183,7 +194,7 @@ open class HTTPCookieStorage: NSObject { | |||
} | |||
|
|||
open override var description: String { | |||
return "<NSHTTPCookieStorage cookies count:\(cookies?.count ?? 0)>" | |||
return isEphemeral ? "Ephemeral" : "" + "<NSHTTPCookieStorage cookies count:\(cookies?.count ?? 0)>" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string concatenation is expensive and most of the times lesser performant to string interpolation. you could use string interpolation instead.
"\(isEphemeral ? "Ephemeral" : "")<NSHTTPCookieStorage cookies count:\(cookies?.count ?? 0)>"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manojmahapatra Done. Thanks!
Foundation/HTTPCookieStorage.swift
Outdated
@@ -200,6 +211,9 @@ open class HTTPCookieStorage: NSObject { | |||
} | |||
|
|||
private func updatePersistentStore() { | |||
guard !isEphemeral else { return } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like we could combine these guard statements here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manojmahapatra I'd like to separate concerns. However, I think an if statement
made more sense here. It makes the code more readable and simple. Thanks!
if isEphemeral { return }
The difference between `default` and `ephemeral` `URLSessionConfiguration`s is in the storage aspect. There are three kinds of stores related to a `URLSession` - a `URLCache` - a `URLCredentialStorage` - an `HTTPCookieStorage` While the `default` configuration mandates persistent stores, the `ephemeral` configuration mandates only in-memory, ephemeral stores. As of today, `URLCache` and `URLCredentialStorage` are unimplemented and not configured in the `default` configuration. `HTTPCookieStorage` is implemented and we use the `shared` storage in the `default` configuration. This commit includes: - an ephemeral `HTTPCookieStorage` that does not read from, and write to, a persistent store - an initial `URLSessionConfiguration.ephemeral` implementation which configures an ephmeral `HTTPCookieStorage`, but no `URLCache` and `URLCredentialStorage`
3efe0d5
to
b25ca86
Compare
@swift-ci please test |
@swift-ci please test Linux |
@swift-ci test and merge |
The difference between
default
andephemeral
URLSessionConfiguration
sis in the storage aspect. There are three kinds of stores related to a
URLSession
URLCache
URLCredentialStorage
HTTPCookieStorage
While the
default
configuration mandates persistent stores, theephemeral
configuration mandates only in-memory, ephemeral stores. As of today,
URLCache
and
URLCredentialStorage
are unimplemented and not configured in thedefault
configuration.
HTTPCookieStorage
is implemented and we use theshared
storagein the
default
configuration.This commit includes:
HTTPCookieStorage
that does not read from, and write to, a persistentstore
URLSessionConfiguration.ephemeral
implementation which configures anephmeral
HTTPCookieStorage
, but noURLCache
andURLCredentialStorage