diff --git a/bridgev2/bridge.go b/bridgev2/bridge.go index 2ad6a614..c84c2fd5 100644 --- a/bridgev2/bridge.go +++ b/bridgev2/bridge.go @@ -11,6 +11,7 @@ import ( "fmt" "os" "sync" + "sync/atomic" "time" "github.com/rs/zerolog" @@ -52,6 +53,7 @@ type Bridge struct { Background bool ExternallyManagedDB bool + stopping atomic.Bool wakeupBackfillQueue chan struct{} stopBackfillQueue *exsync.Event @@ -127,6 +129,7 @@ func (br *Bridge) Start(ctx context.Context) error { func (br *Bridge) RunOnce(ctx context.Context, loginID networkid.UserLoginID, params *ConnectBackgroundParams) error { br.Background = true + br.stopping.Store(false) err := br.StartConnectors(ctx) if err != nil { return err @@ -162,6 +165,7 @@ func (br *Bridge) RunOnce(ctx context.Context, loginID networkid.UserLoginID, pa case <-time.After(20 * time.Second): case <-ctx.Done(): } + br.stopping.Store(true) return nil } else { br.Log.Info().Str("user_login_id", string(login.ID)).Msg("Starting individual user login in background mode") @@ -171,6 +175,7 @@ func (br *Bridge) RunOnce(ctx context.Context, loginID networkid.UserLoginID, pa func (br *Bridge) StartConnectors(ctx context.Context) error { br.Log.Info().Msg("Starting bridge") + br.stopping.Store(false) if br.BackgroundCtx == nil || br.BackgroundCtx.Err() != nil { br.BackgroundCtx, br.cancelBackgroundCtx = context.WithCancel(context.Background()) br.BackgroundCtx = br.Log.WithContext(br.BackgroundCtx) @@ -368,6 +373,10 @@ func (br *Bridge) StartLogins(ctx context.Context) error { return nil } +func (br *Bridge) IsStopping() bool { + return br.stopping.Load() +} + func (br *Bridge) Stop() { br.stop(false, 0) } @@ -378,6 +387,7 @@ func (br *Bridge) StopWithTimeout(timeout time.Duration) { func (br *Bridge) stop(isRunOnce bool, timeout time.Duration) { br.Log.Info().Msg("Shutting down bridge") + br.stopping.Store(true) br.DisappearLoop.Stop() br.stopBackfillQueue.Set() br.Matrix.PreStop()