Attempt socket bugfix

This commit is contained in:
Christoph Hagen 2023-11-10 15:01:37 +01:00
parent 9f20563877
commit 1369a621ad
2 changed files with 16 additions and 22 deletions

View File

@ -35,8 +35,6 @@ final class DeviceManager {
private let messagesToDeviceMetric: Metric<Int> private let messagesToDeviceMetric: Metric<Int>
private let scheduler: AsyncScheduler
var deviceState: DeviceState { var deviceState: DeviceState {
guard let connection, !connection.isClosed else { guard let connection, !connection.isClosed else {
return .disconnected return .disconnected
@ -55,22 +53,21 @@ final class DeviceManager {
/// A promise to finish the request once the device responds or times out /// A promise to finish the request once the device responds or times out
private var requestInProgress: CheckedContinuation<Data, Error>? private var requestInProgress: CheckedContinuation<Data, Error>?
init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64, scheduler: AsyncScheduler) { init(deviceKey: Data, remoteKey: Data, deviceTimeout: Int64) {
self.deviceKey = deviceKey self.deviceKey = deviceKey
self.remoteKey = remoteKey self.remoteKey = remoteKey
self.deviceTimeout = deviceTimeout self.deviceTimeout = deviceTimeout
self.deviceStateMetric = .init( self.deviceStateMetric = .init(
"sesame.device", "sesame.device",
name: "Device connected", name: "Device status",
description: "Shows if the device is connected via WebSocket") description: "Shows if the device is connected and authenticated via WebSocket")
self.messagesToDeviceMetric = .init( self.messagesToDeviceMetric = .init(
"sesame.messages", "sesame.messages",
name: "Forwarded Messages", name: "Forwarded Messages",
description: "The number of messages transmitted to the device") description: "The number of messages transmitted to the device")
self.scheduler = scheduler
} }
private func updateDeviceConnectionMetric() async { func updateDeviceConnectionMetric() async {
_ = try? await deviceStateMetric.update(deviceState) _ = try? await deviceStateMetric.update(deviceState)
} }
@ -166,7 +163,7 @@ final class DeviceManager {
func createNewDeviceConnection(_ socket: WebSocket) async { func createNewDeviceConnection(_ socket: WebSocket) async {
await removeDeviceConnection() await removeDeviceConnection()
socket.eventLoop.execute {
socket.onBinary { [weak self] _, data in socket.onBinary { [weak self] _, data in
self?.processDeviceResponse(data) self?.processDeviceResponse(data)
} }
@ -177,7 +174,7 @@ final class DeviceManager {
_ = socket.onClose.always { [weak self] _ in _ = socket.onClose.always { [weak self] _ in
self?.didCloseDeviceSocket() self?.didCloseDeviceSocket()
} }
}
connection = socket connection = socket
await updateDeviceConnectionMetric() await updateDeviceConnectionMetric()
} }

View File

@ -35,11 +35,7 @@ public func configure(_ app: Application) throws {
let keyFile = storageFolder.appendingPathComponent(config.keyFileName) let keyFile = storageFolder.appendingPathComponent(config.keyFileName)
let (deviceKey, remoteKey) = try loadKeys(at: keyFile) let (deviceKey, remoteKey) = try loadKeys(at: keyFile)
deviceManager = DeviceManager( deviceManager = DeviceManager(deviceKey: deviceKey, remoteKey: remoteKey, deviceTimeout: config.deviceTimeout)
deviceKey: deviceKey,
remoteKey: remoteKey,
deviceTimeout: config.deviceTimeout,
scheduler: asyncScheduler)
try routes(app) try routes(app)
@ -49,6 +45,7 @@ public func configure(_ app: Application) throws {
asyncScheduler.schedule { asyncScheduler.schedule {
_ = try await status.update(.nominal) _ = try await status.update(.nominal)
await deviceManager.updateDeviceConnectionMetric()
} }
let df = DateFormatter() let df = DateFormatter()