bridgev2: add clean shutdown

This commit is contained in:
Tulir Asokan 2024-06-17 16:00:07 +03:00
commit 833995832b
6 changed files with 47 additions and 2 deletions

View file

@ -120,3 +120,21 @@ func (br *Bridge) Start() error {
br.Log.Info().Msg("Bridge started")
return nil
}
func (br *Bridge) Stop() {
br.Log.Info().Msg("Shutting down bridge")
br.Matrix.Stop()
br.cacheLock.Lock()
var wg sync.WaitGroup
wg.Add(len(br.userLoginsByID))
for _, login := range br.userLoginsByID {
go login.Disconnect(wg.Done)
}
wg.Wait()
br.cacheLock.Unlock()
err := br.DB.Close()
if err != nil {
br.Log.Warn().Err(err).Msg("Failed to close database")
}
br.Log.Info().Msg("Shutdown complete")
}

View file

@ -141,6 +141,14 @@ func (br *Connector) Start(ctx context.Context) error {
return nil
}
func (br *Connector) Stop() {
br.AS.Stop()
br.EventProcessor.Stop()
if br.Crypto != nil {
br.Crypto.Stop()
}
}
var MinSpecVersion = mautrix.SpecV14
func (br *Connector) ensureConnection(ctx context.Context) {

View file

@ -354,8 +354,7 @@ func (br *BridgeMain) WaitForInterrupt() {
// Stop cleanly stops the bridge. This is called by [Run] and does not need to be called manually.
func (br *BridgeMain) Stop() {
br.Log.Info().Msg("Shutting down bridge")
// TODO actually stop cleanly
br.Bridge.Stop()
}
// InitVersion formats the bridge version and build time nicely for things like

View file

@ -20,6 +20,7 @@ import (
type MatrixConnector interface {
Init(*Bridge)
Start(ctx context.Context) error
Stop()
ParseGhostMXID(userID id.UserID) (networkid.UserID, bool)
FormatGhostMXID(userID networkid.UserID) id.UserID

View file

@ -141,6 +141,7 @@ type MaxFileSizeingNetwork interface {
// NetworkAPI is an interface representing a remote network client for a single user login.
type NetworkAPI interface {
Connect(ctx context.Context) error
Disconnect()
IsLoggedIn() bool
LogoutRemote(ctx context.Context)

View file

@ -9,6 +9,7 @@ package bridgev2
import (
"context"
"fmt"
"time"
"github.com/rs/zerolog"
@ -161,3 +162,20 @@ func (ul *UserLogin) GetRemoteName() string {
name, _ := ul.Metadata["remote_name"].(string)
return name
}
func (ul *UserLogin) Disconnect(done func()) {
defer done()
if ul.Client != nil {
disconnected := make(chan struct{})
go func() {
ul.Client.Disconnect()
ul.Client = nil
close(disconnected)
}()
select {
case <-disconnected:
case <-time.After(5 * time.Second):
ul.Log.Warn().Msg("Client disconnection timed out")
}
}
}