Skip to content

Commit

Permalink
Try using IHttpClientFactory instead of just HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Lanayx committed Sep 19, 2023
1 parent 1fd942c commit 5c90dd4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ nuget FSharp.Core
nuget Microsoft.Extensions.Caching.Memory
nuget Microsoft.Extensions.Logging
nuget Microsoft.Extensions.Logging.Abstractions
nuget Microsoft.Extensions.Http
nuget Microsoft.IO.RecyclableMemoryStream
nuget Microsoft.Bcl.AsyncInterfaces
nuget System.Text.Json
Expand Down
45 changes: 25 additions & 20 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ NUGET
FSharp.Core (7.0.400)
FSharp.UMX (1.1)
FSharp.Core (>= 4.3.4)
K4os.Compression.LZ4 (1.3.5)
K4os.Compression.LZ4 (1.3.6)
System.Memory (>= 4.5.5)
System.Runtime.CompilerServices.Unsafe (>= 5.0)
libsodium (1.0.18.4)
Expand All @@ -33,6 +33,11 @@ NUGET
Microsoft.Extensions.DependencyInjection.Abstractions (7.0)
Microsoft.Bcl.AsyncInterfaces (>= 7.0)
System.Threading.Tasks.Extensions (>= 4.5.4)
Microsoft.Extensions.Http (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Logging (>= 7.0)
Microsoft.Extensions.Logging.Abstractions (>= 7.0)
Microsoft.Extensions.Options (>= 7.0)
Microsoft.Extensions.Logging (7.0)
Microsoft.Bcl.AsyncInterfaces (>= 7.0)
Microsoft.Extensions.DependencyInjection (>= 7.0)
Expand Down Expand Up @@ -134,7 +139,7 @@ NUGET
FSharp.Core (7.0.400)
FSharp.UMX (1.1)
FSharp.Core (>= 4.3.4)
K4os.Compression.LZ4 (1.3.5)
K4os.Compression.LZ4 (1.3.6)
libsodium (1.0.18.4)
Microsoft.Bcl.AsyncInterfaces (7.0)
Microsoft.Extensions.Caching.Abstractions (7.0)
Expand Down Expand Up @@ -195,16 +200,16 @@ NUGET
NSec.Cryptography (20.2)
libsodium (>= 1.0.18 < 1.0.19)
System.Runtime.CompilerServices.Unsafe (>= 4.7)
OpenTelemetry (1.5.1)
OpenTelemetry (1.6)
Microsoft.Extensions.Logging.Configuration (>= 3.1)
OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.5.1)
OpenTelemetry.Api (1.5.1)
System.Diagnostics.DiagnosticSource (>= 7.0)
OpenTelemetry.Api.ProviderBuilderExtensions (1.5.1)
OpenTelemetry.Api.ProviderBuilderExtensions (>= 1.6)
OpenTelemetry.Api (1.6)
System.Diagnostics.DiagnosticSource (>= 7.0.2)
OpenTelemetry.Api.ProviderBuilderExtensions (1.6)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 3.1)
OpenTelemetry.Api (>= 1.5.1)
OpenTelemetry.Exporter.Console (1.5.1)
OpenTelemetry (>= 1.5.1)
OpenTelemetry.Api (>= 1.6)
OpenTelemetry.Exporter.Console (1.6)
OpenTelemetry (>= 1.6)
System.Text.Encodings.Web (>= 4.7.2)
System.Text.Json (>= 4.7.2)
PemUtils (3.0.0.82)
Expand Down Expand Up @@ -275,7 +280,7 @@ NUGET
FSharp.Core (7.0.400)
FSharp.UMX (1.1)
FSharp.Core (>= 4.3.4)
K4os.Compression.LZ4 (1.3.5)
K4os.Compression.LZ4 (1.3.6)
System.Memory (>= 4.5.5)
System.Runtime.CompilerServices.Unsafe (>= 5.0)
libsodium (1.0.18.4)
Expand Down Expand Up @@ -354,8 +359,8 @@ NUGET
libsodium (>= 1.0.18 < 1.0.19)
System.Memory (>= 4.5.3)
System.Runtime.CompilerServices.Unsafe (>= 4.7)
OpenTelemetry.Api (1.5.1)
System.Diagnostics.DiagnosticSource (>= 7.0)
OpenTelemetry.Api (1.6)
System.Diagnostics.DiagnosticSource (>= 7.0.2)
PemUtils (3.0.0.82)
DerConverter (>= 3.0.0.82)
Pipelines.Sockets.Unofficial (2.2.8)
Expand Down Expand Up @@ -454,7 +459,7 @@ NUGET
FsCheck (2.16.6)
FSharp.Core (>= 4.2.3)
FSharp.Core (7.0.400)
Microsoft.CodeCoverage (17.7)
Microsoft.CodeCoverage (17.7.2)
Microsoft.Extensions.DependencyInjection (7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.DependencyInjection.Abstractions (7.0)
Expand All @@ -468,14 +473,14 @@ NUGET
Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0)
Microsoft.Extensions.Primitives (>= 7.0)
Microsoft.Extensions.Primitives (7.0)
Microsoft.NET.Test.Sdk (17.7)
Microsoft.CodeCoverage (>= 17.7)
Microsoft.TestPlatform.TestHost (>= 17.7)
Microsoft.TestPlatform.ObjectModel (17.7)
Microsoft.NET.Test.Sdk (17.7.2)
Microsoft.CodeCoverage (>= 17.7.2)
Microsoft.TestPlatform.TestHost (>= 17.7.2)
Microsoft.TestPlatform.ObjectModel (17.7.2)
NuGet.Frameworks (>= 6.5)
System.Reflection.Metadata (>= 1.6)
Microsoft.TestPlatform.TestHost (17.7)
Microsoft.TestPlatform.ObjectModel (>= 17.7)
Microsoft.TestPlatform.TestHost (17.7.2)
Microsoft.TestPlatform.ObjectModel (>= 17.7.2)
Newtonsoft.Json (>= 13.0.1)
Mono.Cecil (0.11.5)
Newtonsoft.Json (13.0.3)
Expand Down
21 changes: 12 additions & 9 deletions src/Pulsar.Client/Auth/Oauth2/AuthenticationOauth2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ open System.IO
open System.Text.Json
open System.Net.Http
open System.Text.Json.Serialization
open Microsoft.Extensions.DependencyInjection
open Pulsar.Client.Api
open Microsoft.Extensions.Http

open Pulsar.Client.Auth
type Metadata =
Expand Down Expand Up @@ -50,6 +52,11 @@ type internal AuthenticationOauth2(issuerUrl: Uri, audience: string, privateKey:
inherit Authentication()

let mutable token : Option<TokenResult * DateTime> = None
let httpClientFactory =
ServiceCollection()
.AddHttpClient()
.BuildServiceProvider()
.GetService<IHttpClientFactory>()

//Gets a well-known metadata URL for the given OAuth issuer URL.
//https://tools.ietf.org/id/draft-ietf-oauth-discovery-08.html#ASConfig
Expand All @@ -62,14 +69,15 @@ type internal AuthenticationOauth2(issuerUrl: Uri, audience: string, privateKey:
let! response = httpClient.GetStreamAsync metadataDataUrl
return! JsonSerializer.DeserializeAsync<Metadata> response
}
let createClient httpClient issuerUrl =
let getClient() =
backgroundTask {
let httpClient = httpClientFactory.CreateClient()
let! metadata = getMetadata httpClient issuerUrl
return TokenClient(Uri(metadata.TokenEndpoint), httpClient)
}

let openAndDeserializeCreds uri =
task{
backgroundTask {
use fs = new FileStream(uri, FileMode.Open, FileAccess.Read)
let! temp = JsonSerializer.DeserializeAsync<Credentials>(fs)
return temp
Expand All @@ -87,8 +95,6 @@ type internal AuthenticationOauth2(issuerUrl: Uri, audience: string, privateKey:
None
)

let httpClient = new HttpClient()
let tokenClientTask = createClient httpClient issuerUrl

override this.GetAuthMethodName() =
"token"
Expand All @@ -99,7 +105,7 @@ type internal AuthenticationOauth2(issuerUrl: Uri, audience: string, privateKey:
let newToken =
(backgroundTask {
let! credentials = openAndDeserializeCreds(privateKey.LocalPath)
let! tokenTaskResult = tokenClientTask
let! tokenTaskResult = getClient()
return!
tokenTaskResult.ExchangeClientCredentials(
credentials.ClientId,
Expand All @@ -118,7 +124,4 @@ type internal AuthenticationOauth2(issuerUrl: Uri, audience: string, privateKey:
raise <| Exception e

| Some tokenResult ->
upcast AuthenticationDataToken(fun () -> tokenResult.AccessToken)

override this.Dispose() =
httpClient.Dispose()
upcast AuthenticationDataToken(fun () -> tokenResult.AccessToken)
1 change: 1 addition & 0 deletions src/Pulsar.Client/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FSharp.Core
FSharp.UMX
Pipelines.Sockets.Unofficial
Microsoft.IO.RecyclableMemoryStream
Microsoft.Extensions.Http
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Abstractions
Microsoft.Bcl.AsyncInterfaces
Expand Down

0 comments on commit 5c90dd4

Please sign in to comment.