Skip to content

Commit 04f4f09

Browse files
committed
Read redis hostname from REDIS_HOST env variable
1 parent fe28dc7 commit 04f4f09

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

Sources/App/Core/Dependencies/EnvironmentClient.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct EnvironmentClient {
5151
case repositorySaveFailed
5252
case repositorySaveUniqueViolation
5353
}
54+
var redisHostname: @Sendable () -> String = { "redis" }
5455
var shouldFail: @Sendable (_ failureMode: FailureMode) -> Bool = { _ in false }
5556
}
5657

@@ -107,6 +108,12 @@ extension EnvironmentClient: DependencyKey {
107108
.map(Mastodon.Credentials.init(accessToken:))
108109
},
109110
random: { range in Double.random(in: range) },
111+
redisHostname: {
112+
// Defaulting this to `redis`, which is the service name in `app.yml`.
113+
// This is also why `REDIS_HOST` is not set as an env variable in `app.yml`,
114+
// it's a known value that needs no configuration outside of local use for testing.
115+
Environment.get("REDIS_HOST") ?? "redis"
116+
},
110117
shouldFail: { failureMode in
111118
let shouldFail = Environment.get("FAILURE_MODE")
112119
.map { Data($0.utf8) }

Sources/App/Core/Dependencies/RedisClient.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ private actor Redis {
109109
}
110110

111111
private init() async throws {
112+
@Dependency(\.environment) var environment
112113
let connection = RedisConnection.make(
113-
configuration: try .init(hostname: Redis.hostname),
114+
configuration: try .init(hostname: environment.redisHostname()),
114115
boundEventLoop: NIOSingletons.posixEventLoopGroup.any()
115116
)
116117
self.client = try await connection.get()
117118
}
118119

119-
// This hostname has to match the redir service name in app.yml.
120-
static let hostname = "redis"
121120
static let maxConnectionAttempts = 3
122121

123122
func set(key: String, value: String?, expiresIn: Duration?) async {

0 commit comments

Comments
 (0)