mirror of
https://github.com/apple/pkl.git
synced 2026-03-28 03:51:14 +01:00
Use java.net.http.HttpClient instead of java.net.Http(s)URLConnection (#217)
Moving to java.net.http.HttpClient brings many benefits, including HTTP/2 support and the ability to make asynchronous requests. Major additions and changes: - Introduce a lightweight org.pkl.core.http.HttpClient API. This keeps some flexibility and allows to enforce behavior such as setting the User-Agent header. - Provide an implementation that delegates to java.net.http.HttpClient. - Use HttpClient for all HTTP(s) requests across the codebase. This required adding an HttpClient parameter to constructors and factory methods of multiple classes, some of which are public APIs. - Manage CA certificates per HTTP client instead of per JVM. This makes it unnecessary to set JVM-wide system/security properties and default SSLSocketFactory's. - Add executor v2 options to the executor SPI - Add pkl-certs as a new artifact, and remove certs from pkl-commons-cli artifact Each HTTP client maintains its own connection pool and SSLContext. For efficiency reasons, It's best to reuse clients whenever feasible. To avoid memory leaks, clients are not stored in static fields. HTTP clients are expensive to create. For this reason, EvaluatorBuilder defaults to a "lazy" client that creates the underlying java.net.http.HttpClient on the first send (which may never happen).
This commit is contained in:
@@ -31,7 +31,7 @@ class CliPackageDownloader(
|
||||
if (moduleCacheDir == null) {
|
||||
throw CliException("Cannot download packages because no cache directory is specified.")
|
||||
}
|
||||
val packageResolver = PackageResolver.getInstance(securityManager, moduleCacheDir)
|
||||
val packageResolver = PackageResolver.getInstance(securityManager, httpClient, moduleCacheDir)
|
||||
val errors = mutableMapOf<PackageUri, Throwable>()
|
||||
for (pkg in packageUris) {
|
||||
try {
|
||||
|
||||
@@ -82,6 +82,7 @@ class CliProjectPackager(
|
||||
outputPath,
|
||||
stackFrameTransformer,
|
||||
securityManager,
|
||||
httpClient,
|
||||
skipPublishCheck,
|
||||
consoleWriter
|
||||
)
|
||||
|
||||
@@ -40,6 +40,7 @@ class CliProjectResolver(
|
||||
SecurityManagers.defaultTrustLevels,
|
||||
rootDir
|
||||
),
|
||||
httpClient,
|
||||
moduleCacheDir
|
||||
)
|
||||
val dependencies = ProjectDependenciesResolver(project, packageResolver, errWriter).resolve()
|
||||
|
||||
@@ -36,6 +36,7 @@ internal class CliRepl(private val options: CliEvaluatorOptions) : CliCommand(op
|
||||
SecurityManagers.defaultTrustLevels,
|
||||
rootDir
|
||||
),
|
||||
httpClient,
|
||||
Loggers.stdErr(),
|
||||
listOf(
|
||||
ModuleKeyFactories.standardLibrary,
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.pkl.server.Server
|
||||
class CliServer(options: CliBaseOptions) : CliCommand(options) {
|
||||
override fun doRun() =
|
||||
try {
|
||||
val server = Server(MessageTransports.stream(System.`in`, System.out))
|
||||
val server = Server(MessageTransports.stream(System.`in`, System.out), httpClient)
|
||||
server.use { it.start() }
|
||||
} catch (e: ProtocolException) {
|
||||
throw CliException(e.message!!)
|
||||
|
||||
Reference in New Issue
Block a user