Skip to content

Commit

Permalink
Revert usage of HTTPClient.shared (#193)
Browse files Browse the repository at this point in the history
* Revert "Temporarily don't use `HTTPClient.shared` in `GitHubAPI` (#188)"

This reverts commit bf86a01.

* Revert using `HTTPClient.shared`

* use a new shared configuration for Penny's HTTPClient's

* remove syncShutdown() s

* no need to try!
  • Loading branch information
MahdiBM committed May 24, 2024
1 parent b4ab513 commit 54c7c3c
Show file tree
Hide file tree
Showing 29 changed files with 220 additions and 81 deletions.
10 changes: 8 additions & 2 deletions Lambdas/AutoFaqs/AutoFaqsHandler.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AWSLambdaRuntime
import AWSLambdaEvents
import AsyncHTTPClient
import Foundation
import SotoCore
import Models
import Shared
import LambdasShared

@main
Expand All @@ -14,8 +16,12 @@ struct AutoFaqsHandler: LambdaHandler {
let autoFaqsRepo: S3AutoFaqsRepository

init(context: LambdaInitializationContext) async {
self.awsClient = AWSClient()
self.autoFaqsRepo = S3AutoFaqsRepository(awsClient: awsClient, logger: context.logger)
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
self.awsClient = AWSClient(httpClient: httpClient)
self.autoFaqsRepo = S3AutoFaqsRepository(awsClient: self.awsClient, logger: context.logger)
}

func handle(
Expand Down
10 changes: 8 additions & 2 deletions Lambdas/AutoPings/AutoPingsHandler.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AWSLambdaRuntime
import AWSLambdaEvents
import AsyncHTTPClient
import Foundation
import SotoCore
import Models
import Shared
import LambdasShared

@main
Expand All @@ -14,8 +16,12 @@ struct AutoPingsHandler: LambdaHandler {
let pingsRepo: S3AutoPingsRepository

init(context: LambdaInitializationContext) async {
self.awsClient = AWSClient()
self.pingsRepo = S3AutoPingsRepository(awsClient: awsClient, logger: context.logger)
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
self.awsClient = AWSClient(httpClient: httpClient)
self.pingsRepo = S3AutoPingsRepository(awsClient: self.awsClient, logger: context.logger)
}

func handle(
Expand Down
8 changes: 7 additions & 1 deletion Lambdas/Faqs/FaqsHandler.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AWSLambdaRuntime
import AWSLambdaEvents
import AsyncHTTPClient
import Foundation
import SotoCore
import Models
Expand All @@ -14,7 +15,12 @@ struct FaqsHandler: LambdaHandler {
let faqsRepo: S3FaqsRepository

init(context: LambdaInitializationContext) async {
self.awsClient = AWSClient()
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
let awsClient = AWSClient(httpClient: httpClient)
self.awsClient = awsClient
self.faqsRepo = S3FaqsRepository(awsClient: awsClient, logger: context.logger)
}

Expand Down
13 changes: 9 additions & 4 deletions Lambdas/GHHooks/+Rendering/+LeafRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import Logging
import Foundation

extension LeafRenderer {
static func forGHHooks(logger: Logger) throws -> LeafRenderer {
static func forGHHooks(httpClient: HTTPClient, logger: Logger) throws -> LeafRenderer {
try LeafRenderer(
subDirectory: "GHHooksLambda",
extraSources: [DocsLeafSource(logger: logger)],
logger: logger
httpClient: httpClient,
extraSources: [DocsLeafSource(
httpClient: httpClient,
logger: logger
)],
logger: logger,
on: httpClient.eventLoopGroup.next()
)
}
}
Expand Down Expand Up @@ -39,7 +44,7 @@ private struct DocsLeafSource: LeafSource {
}
}

let httpClient: HTTPClient = .shared
let httpClient: HTTPClient
let logger: Logger

func file(
Expand Down
6 changes: 4 additions & 2 deletions Lambdas/GHHooks/Authenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import Shared

actor Authenticator {
private let secretsRetriever: SecretsRetriever
private let httpClient: HTTPClient = .shared
private let httpClient: HTTPClient
let logger: Logger

/// The cached access token.
private var cachedAccessToken: InstallationToken?

private let queue = SerialProcessor()

init(secretsRetriever: SecretsRetriever, logger: Logger) {
init(secretsRetriever: SecretsRetriever, httpClient: HTTPClient, logger: Logger) {
self.secretsRetriever = secretsRetriever
self.httpClient = httpClient
self.logger = logger
}

Expand Down Expand Up @@ -60,6 +61,7 @@ actor Authenticator {

private func makeClient(token: String) throws -> Client {
try .makeForGitHub(
httpClient: self.httpClient,
authorization: .bearer(token),
logger: self.logger
)
Expand Down
5 changes: 4 additions & 1 deletion Lambdas/GHHooks/EventHandler/HandlerContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Logging
struct HandlerContext: Sendable {
let eventName: GHEvent.Kind
let event: GHEvent
let httpClient: HTTPClient = .shared
let httpClient: HTTPClient
let discordClient: any DiscordClient
let githubClient: Client
let renderClient: RenderClient
Expand All @@ -21,6 +21,7 @@ struct HandlerContext: Sendable {
init(
eventName: GHEvent.Kind,
event: GHEvent,
httpClient: HTTPClient,
discordClient: any DiscordClient,
githubClient: Client,
renderClient: RenderClient,
Expand All @@ -30,6 +31,7 @@ struct HandlerContext: Sendable {
) {
self.eventName = eventName
self.event = event
self.httpClient = httpClient
self.discordClient = discordClient
self.githubClient = githubClient
self.renderClient = renderClient
Expand All @@ -38,6 +40,7 @@ struct HandlerContext: Sendable {
self.requester = .init(
eventName: eventName,
event: event,
httpClient: httpClient,
discordClient: discordClient,
githubClient: githubClient,
usersService: usersService,
Expand Down
2 changes: 1 addition & 1 deletion Lambdas/GHHooks/EventHandler/Requester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Logging
struct Requester: Sendable {
let eventName: GHEvent.Kind
let event: GHEvent
let httpClient: HTTPClient = .shared
let httpClient: HTTPClient
let discordClient: any DiscordClient
let githubClient: Client
let usersService: any UsersService
Expand Down
19 changes: 16 additions & 3 deletions Lambdas/GHHooks/GHHooksHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct GHHooksHandler: LambdaHandler {
typealias Event = APIGatewayV2Request
typealias Output = APIGatewayV2Response

let httpClient: HTTPClient
let githubClient: Client
let secretsRetriever: SecretsRetriever
let messageLookupRepo: any MessageLookupRepo
Expand All @@ -26,7 +27,7 @@ struct GHHooksHandler: LambdaHandler {
var discordClient: any DiscordClient {
get async throws {
let botToken = try await secretsRetriever.getSecret(arnEnvVarKey: "BOT_TOKEN_ARN")
return await DefaultDiscordClient(token: botToken)
return await DefaultDiscordClient(httpClient: httpClient, token: botToken)
}
}

Expand All @@ -36,15 +37,22 @@ struct GHHooksHandler: LambdaHandler {
/// bootstrapping the logging system which it appears to not have.
DiscordGlobalConfiguration.makeLogger = { _ in context.logger }

let awsClient = AWSClient()
self.httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)

let awsClient = AWSClient(httpClient: self.httpClient)
self.secretsRetriever = SecretsRetriever(awsClient: awsClient, logger: logger)

let authenticator = Authenticator(
secretsRetriever: secretsRetriever,
httpClient: httpClient,
logger: logger
)

self.githubClient = try .makeForGitHub(
httpClient: httpClient,
authorization: .computedBearer { isRetry in
try await authenticator.generateAccessToken(
forceRefreshToken: isRetry
Expand Down Expand Up @@ -130,15 +138,20 @@ struct GHHooksHandler: LambdaHandler {
context: .init(
eventName: eventName,
event: event,
httpClient: httpClient,
discordClient: discordClient,
githubClient: githubClient,
renderClient: RenderClient(
renderer: try .forGHHooks(
httpClient: httpClient,
logger: logger
)
),
messageLookupRepo: self.messageLookupRepo,
usersService: ServiceFactory.makeUsersService(apiBaseURL: apiBaseURL),
usersService: ServiceFactory.makeUsersService(
httpClient: httpClient,
apiBaseURL: apiBaseURL
),
logger: logger
)
).handle()
Expand Down
13 changes: 10 additions & 3 deletions Lambdas/GHOAuth/OAuthLambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct GHOAuthHandler: LambdaHandler {
typealias Event = APIGatewayV2Request
typealias Output = APIGatewayV2Response

let client: HTTPClient = .shared
let client: HTTPClient
let secretsRetriever: SecretsRetriever
let userService: any UsersService
let signers: JWTSigners
Expand All @@ -38,13 +38,20 @@ struct GHOAuthHandler: LambdaHandler {
}

init(context: LambdaInitializationContext) async throws {
self.client = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
self.logger = context.logger

let awsClient = AWSClient()
let awsClient = AWSClient(httpClient: client)
self.secretsRetriever = SecretsRetriever(awsClient: awsClient, logger: logger)

let apiBaseURL = try requireEnvVar("API_BASE_URL")
self.userService = ServiceFactory.makeUsersService(apiBaseURL: apiBaseURL)
self.userService = ServiceFactory.makeUsersService(
httpClient: client,
apiBaseURL: apiBaseURL
)

signers = JWTSigners()
signers.use(.es256(key: try getJWTSignersPublicKey()))
Expand Down
7 changes: 2 additions & 5 deletions Lambdas/GitHubAPI/+Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import struct NIOCore.TimeAmount

extension Client {
package static func makeForGitHub(
httpClient: HTTPClient,
authorization: AuthorizationHeader,
timeout: TimeAmount = .seconds(5),
logger: Logger
Expand All @@ -14,11 +15,7 @@ extension Client {
)
let transport = AsyncHTTPClientTransport(
configuration: .init(
client: HTTPClient(
configuration: .init(
decompression: .enabled(limit: .ratio(20))
)
),
client: httpClient,
timeout: timeout
)
)
Expand Down
21 changes: 13 additions & 8 deletions Lambdas/Sponsors/SponsorsLambda.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ struct SponsorsHandler: LambdaHandler {
typealias Event = APIGatewayV2Request
typealias Output = APIGatewayV2Response

let httpClient: HTTPClient = .shared
let awsClient: AWSClient = AWSClient()
let httpClient: HTTPClient
let awsClient: AWSClient
let secretsRetriever: SecretsRetriever
let logger: Logger

Expand All @@ -23,7 +23,7 @@ struct SponsorsHandler: LambdaHandler {
var discordClient: any DiscordClient {
get async throws {
let botToken = try await secretsRetriever.getSecret(arnEnvVarKey: "BOT_TOKEN_ARN")
return await DefaultDiscordClient(token: botToken)
return await DefaultDiscordClient(httpClient: httpClient, token: botToken)
}
}

Expand All @@ -32,6 +32,11 @@ struct SponsorsHandler: LambdaHandler {
}

init(context: LambdaInitializationContext) async throws {
self.httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
self.awsClient = AWSClient(httpClient: self.httpClient)
self.secretsRetriever = SecretsRetriever(awsClient: awsClient, logger: context.logger)
self.logger = context.logger
}
Expand Down Expand Up @@ -61,7 +66,10 @@ struct SponsorsHandler: LambdaHandler {
context.logger.debug("Looking for user in the DB")
let newSponsorID = payload.sender.id
let apiBaseURL = try requireEnvVar("API_BASE_URL")
let userService = ServiceFactory.makeUsersService(apiBaseURL: apiBaseURL)
let userService = ServiceFactory.makeUsersService(
httpClient: httpClient,
apiBaseURL: apiBaseURL
)
guard let user = try await userService.getUser(githubID: "\(newSponsorID)") else {
context.logger.error("No user found with GitHub ID \(newSponsorID)")
return APIGatewayV2Response(
Expand Down Expand Up @@ -226,10 +234,7 @@ struct SponsorsHandler: LambdaHandler {
triggerActionRequest.body = .bytes(ByteBuffer(string: #"{"ref":"main"}"#))

// Send request to trigger workflow and read response
let githubResponse = try await httpClient.execute(
triggerActionRequest,
timeout: .seconds(10)
)
let githubResponse = try await httpClient.execute(triggerActionRequest, timeout: .seconds(10))

guard 200..<300 ~= githubResponse.status.code else {
let body = try await githubResponse.body.collect(upTo: 1024 * 1024)
Expand Down
10 changes: 7 additions & 3 deletions Lambdas/Users/UsersHandler.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AWSLambdaRuntime
import AWSLambdaEvents
import AsyncHTTPClient
import Foundation
import SotoCore
import Models
import Shared
import LambdasShared

@main
Expand All @@ -14,10 +16,12 @@ struct UsersHandler: LambdaHandler {
let logger: Logger

init(context: LambdaInitializationContext) async {
self.internalService = InternalUsersService(
awsClient: AWSClient(),
logger: context.logger
let httpClient = HTTPClient(
eventLoopGroupProvider: .shared(context.eventLoop),
configuration: .forPenny
)
let awsClient = AWSClient(httpClient: httpClient)
self.internalService = InternalUsersService(awsClient: awsClient, logger: context.logger)
self.logger = context.logger
}

Expand Down
10 changes: 8 additions & 2 deletions Sources/Penny/+Rendering/+LeafRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import Logging
import Foundation

extension LeafRenderer {
static func forPenny(logger: Logger) throws -> LeafRenderer {
static func forPenny(
httpClient: HTTPClient,
logger: Logger,
on eventLoop: any EventLoop
) throws -> LeafRenderer {
try LeafRenderer(
subDirectory: "Penny",
logger: logger
httpClient: httpClient,
logger: logger,
on: eventLoop
)
}
}
Loading

0 comments on commit 54c7c3c

Please sign in to comment.