Skip to content

Commit a354ebe

Browse files
committed
review
1 parent cbd8ce4 commit a354ebe

File tree

5 files changed

+48
-26
lines changed

5 files changed

+48
-26
lines changed

Diff for: Coder-Desktop/Coder-Desktop/State.swift

+4-9
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,11 @@ class AppState: ObservableObject {
157157

158158
public func refreshDeploymentConfig() async {
159159
if hasSession {
160-
do {
161-
let config = try await client!.sshConfiguration()
162-
hostnameSuffix = config.hostname_suffix ?? Self.defaultHostnameSuffix
163-
} catch {
164-
// If fetching the config fails, there's likely a bigger issue.
165-
// We'll show an error in the UI if they try and do something
166-
logger.error("failed to refresh deployment config: \(error)")
167-
return
160+
let res = try? await retry(floor: .milliseconds(100), ceil: .seconds(10)) {
161+
let config = try await client!.agentConnectionInfoGeneric()
162+
return config.hostname_suffix
168163
}
164+
hostnameSuffix = res ?? Self.defaultHostnameSuffix
169165
}
170166
}
171167

@@ -194,7 +190,6 @@ class AppState: ObservableObject {
194190
static let hasSession = "hasSession"
195191
static let baseAccessURL = "baseAccessURL"
196192
static let sessionToken = "sessionToken"
197-
static let hostnameSuffix = "hostnameSuffix"
198193

199194
static let useLiteralHeaders = "UseLiteralHeaders"
200195
static let literalHeaders = "LiteralHeaders"

Diff for: Coder-Desktop/Coder-Desktop/Views/VPN/VPNMenuItem.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ struct MenuItemView: View {
5858
var formattedName = AttributedString(name)
5959
formattedName.foregroundColor = .primary
6060

61-
if let lastDot = formattedName.range(of: ".", options: .backwards) {
62-
formattedName[lastDot.lowerBound ..< formattedName.endIndex].foregroundColor = .secondary
61+
if let range = formattedName.range(of: ".\(state.hostnameSuffix)", options: .backwards) {
62+
formattedName[range].foregroundColor = .secondary
6363
}
6464
return formattedName
6565
}

Diff for: Coder-Desktop/CoderSDK/Deployment.swift

+1-15
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ public extension Client {
88
}
99
return try decode(BuildInfoResponse.self, from: res.data)
1010
}
11-
12-
func sshConfiguration() async throws(SDKError) -> SSHConfigResponse {
13-
let res = try await request("/api/v2/deployment/ssh", method: .get)
14-
guard res.resp.statusCode == 200 else {
15-
throw responseAsError(res)
16-
}
17-
return try decode(SSHConfigResponse.self, from: res.data)
18-
}
1911
}
2012

2113
public struct BuildInfoResponse: Codable, Equatable, Sendable {
@@ -27,10 +19,4 @@ public struct BuildInfoResponse: Codable, Equatable, Sendable {
2719
.firstMatch(in: version, range: NSRange(version.startIndex ..< version.endIndex, in: version))
2820
.flatMap { Range($0.range(at: 1), in: version).map { String(version[$0]) } }
2921
}
30-
}
31-
32-
public struct SSHConfigResponse: Codable, Equatable, Sendable {
33-
public let hostname_prefix: String?
34-
public let hostname_suffix: String?
35-
public let ssh_config_options: [String: String]
36-
}
22+
}

Diff for: Coder-Desktop/CoderSDK/Util.swift

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
public func retry<T>(
4+
floor: Duration,
5+
ceil: Duration,
6+
rate: Double = 1.618,
7+
operation: @Sendable () async throws -> T
8+
) async throws -> T {
9+
var delay = floor
10+
11+
while !Task.isCancelled {
12+
do {
13+
return try await operation()
14+
} catch let error as CancellationError {
15+
throw error
16+
} catch {
17+
try Task.checkCancellation()
18+
19+
delay = min(ceil, delay * rate)
20+
try await Task.sleep(for: delay)
21+
}
22+
}
23+
24+
throw CancellationError()
25+
}

Diff for: Coder-Desktop/CoderSDK/WorkspaceAgents.swift

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
3+
4+
public extension Client {
5+
func agentConnectionInfoGeneric() async throws(SDKError) -> AgentConnectionInfo {
6+
let res = try await request("/api/v2/workspaceagents/connection", method: .get)
7+
guard res.resp.statusCode == 200 else {
8+
throw responseAsError(res)
9+
}
10+
return try decode(AgentConnectionInfo.self, from: res.data)
11+
}
12+
}
13+
14+
public struct AgentConnectionInfo: Codable, Sendable {
15+
public let hostname_suffix: String?
16+
}

0 commit comments

Comments
 (0)