Don't use goroutine for queuing bridge states

This commit is contained in:
Tulir Asokan 2023-02-17 15:51:01 +02:00
parent da1aa50757
commit fc743f1ad2

24
user.go
View file

@ -712,7 +712,7 @@ func (user *User) phoneSeen(ts time.Time) {
} else if !user.PhoneRecentlySeen(false) {
if user.BridgeState.GetPrev().Error == WAPhoneOffline && user.IsConnected() {
user.log.Debugfln("Saw phone after current bridge state said it has been offline, switching state back to connected")
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
} else {
user.log.Debugfln("Saw phone after current bridge state said it has been offline, not sending new bridge state (prev: %s, connected: %t)", user.BridgeState.GetPrev().Error, user.IsConnected())
}
@ -766,19 +766,19 @@ func (user *User) HandleEvent(event interface{}) {
}
case *events.OfflineSyncPreview:
user.log.Infofln("Server says it's going to send %d messages and %d receipts that were missed during downtime", v.Messages, v.Receipts)
go user.BridgeState.Send(status.BridgeState{
user.BridgeState.Send(status.BridgeState{
StateEvent: status.StateBackfilling,
Message: fmt.Sprintf("backfilling %d messages and %d receipts", v.Messages, v.Receipts),
})
case *events.OfflineSyncCompleted:
if !user.PhoneRecentlySeen(true) {
user.log.Infofln("Offline sync completed, but phone last seen date is still %s - sending phone offline bridge status", user.PhoneLastSeen)
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAPhoneOffline})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAPhoneOffline})
} else {
if user.BridgeState.GetPrev().StateEvent == status.StateBackfilling {
user.log.Infoln("Offline sync completed")
}
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
case *events.AppStateSyncComplete:
if len(user.Client.Store.PushName) > 0 && v.Name == appstate.WAPatchCriticalBlock {
@ -816,32 +816,32 @@ func (user *User) HandleEvent(event interface{}) {
} else {
message = "Unknown stream error"
}
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: message})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: message})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
case *events.StreamReplaced:
if user.bridge.Config.Bridge.CrashOnStreamReplaced {
user.log.Infofln("Stopping bridge due to StreamReplaced event")
user.bridge.ManualStop(60)
} else {
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: "Stream replaced"})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: "Stream replaced"})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
user.sendMarkdownBridgeAlert("The bridge was started in another location. Use `reconnect` to reconnect this one.")
}
case *events.ConnectFailure:
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: fmt.Sprintf("Unknown connection failure: %s", v.Reason)})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: fmt.Sprintf("Unknown connection failure: %s", v.Reason)})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
case *events.ClientOutdated:
user.log.Errorfln("Got a client outdated connect failure. The bridge is likely out of date, please update immediately.")
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: "Connect failure: 405 client outdated"})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: "Connect failure: 405 client outdated"})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
case *events.TemporaryBan:
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: v.String()})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Message: v.String()})
user.bridge.Metrics.TrackConnectionState(user.JID, false)
case *events.Disconnected:
// Don't send the normal transient disconnect state if we're already in a different transient disconnect state.
// TODO remove this if/when the phone offline state is moved to a sub-state of CONNECTED
if user.BridgeState.GetPrev().Error != WAPhoneOffline && user.PhoneRecentlySeen(false) {
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WADisconnected})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WADisconnected})
}
user.bridge.Metrics.TrackConnectionState(user.JID, false)
case *events.Contact:
@ -926,10 +926,10 @@ func (user *User) HandleEvent(event interface{}) {
case *events.AppState:
// Ignore
case *events.KeepAliveTimeout:
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAKeepaliveTimeout})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateTransientDisconnect, Error: WAKeepaliveTimeout})
case *events.KeepAliveRestored:
user.log.Infof("Keepalive restored after timeouts, sending connected event")
go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
case *events.MarkChatAsRead:
if user.bridge.Config.Bridge.SyncManualMarkedUnread {
user.markUnread(user.GetPortalByJID(v.JID), !v.Action.GetRead())