Skip to content

Commit 38799be

Browse files
committed
bridgev2/networkinterface: let matrix connector reset remote network connections
1 parent d77cb62 commit 38799be

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

bridgev2/bridge.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,34 @@ func (br *Bridge) StartLogins(ctx context.Context) error {
373373
return nil
374374
}
375375

376+
func (br *Bridge) ResetNetworkConnections() {
377+
nrn, ok := br.Network.(NetworkResettingNetwork)
378+
if ok {
379+
br.Log.Info().Msg("Resetting network connections with NetworkConnector.ResetNetworkConnections")
380+
nrn.ResetNetworkConnections()
381+
return
382+
}
383+
384+
br.Log.Info().Msg("Network connector doesn't support ResetNetworkConnections, recreating clients manually")
385+
for _, login := range br.GetAllCachedUserLogins() {
386+
login.Log.Debug().Msg("Disconnecting and recreating client for network reset")
387+
ctx := login.Log.WithContext(br.BackgroundCtx)
388+
login.Client.Disconnect()
389+
err := login.recreateClient(ctx)
390+
if err != nil {
391+
login.Log.Err(err).Msg("Failed to recreate client during network reset")
392+
login.BridgeState.Send(status.BridgeState{
393+
StateEvent: status.StateUnknownError,
394+
Error: "bridgev2-network-reset-fail",
395+
Info: map[string]any{"go_error": err.Error()},
396+
})
397+
} else {
398+
login.Client.Connect(ctx)
399+
}
400+
}
401+
br.Log.Info().Msg("Finished resetting all user logins")
402+
}
403+
376404
func (br *Bridge) IsStopping() bool {
377405
return br.stopping.Load()
378406
}

bridgev2/networkinterface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,16 @@ type MaxFileSizeingNetwork interface {
318318
SetMaxFileSize(maxSize int64)
319319
}
320320

321+
type NetworkResettingNetwork interface {
322+
NetworkConnector
323+
// ResetHTTPTransport should recreate the HTTP client used by the bridge.
324+
// It should refetch settings from the Matrix connector using GetHTTPClientSettings if applicable.
325+
ResetHTTPTransport()
326+
// ResetNetworkConnections should forcefully disconnect and restart any persistent network connections.
327+
// ResetHTTPTransport will usually be called before this, so resetting the transport is not necessary here.
328+
ResetNetworkConnections()
329+
}
330+
321331
type RemoteEchoHandler func(RemoteMessage, *database.Message) (bool, error)
322332

323333
type MatrixMessageResponse struct {

bridgev2/userlogin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cmp"
1111
"context"
1212
"fmt"
13+
"maps"
1314
"slices"
1415
"sync"
1516
"time"
@@ -140,6 +141,12 @@ func (br *Bridge) GetCachedUserLoginByID(id networkid.UserLoginID) *UserLogin {
140141
return br.userLoginsByID[id]
141142
}
142143

144+
func (br *Bridge) GetAllCachedUserLogins() (logins []*UserLogin) {
145+
br.cacheLock.Lock()
146+
defer br.cacheLock.Unlock()
147+
return slices.Collect(maps.Values(br.userLoginsByID))
148+
}
149+
143150
func (br *Bridge) GetCurrentBridgeStates() (states []status.BridgeState) {
144151
br.cacheLock.Lock()
145152
defer br.cacheLock.Unlock()

0 commit comments

Comments
 (0)