diff --git a/commonspace/headsync/headsync.go b/commonspace/headsync/headsync.go index 21f8acb1..4d7d0395 100644 --- a/commonspace/headsync/headsync.go +++ b/commonspace/headsync/headsync.go @@ -29,6 +29,7 @@ type TreeHeads struct { type HeadSync interface { Init(objectIds []string, deletionState settingsstate.ObjectDeletionState) + Run() UpdateHeads(id string, heads []string) HandleRangeRequest(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error) @@ -48,6 +49,7 @@ type headSync struct { syncer DiffSyncer configuration nodeconf.NodeConf spaceIsDeleted *atomic.Bool + isRunning bool syncPeriod int } @@ -93,7 +95,11 @@ func NewHeadSync( func (d *headSync) Init(objectIds []string, deletionState settingsstate.ObjectDeletionState) { d.fillDiff(objectIds) d.syncer.Init(deletionState) +} + +func (d *headSync) Run() { d.periodicSync.Run() + d.isRunning = true } func (d *headSync) HandleRangeRequest(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error) { @@ -135,8 +141,10 @@ func (d *headSync) RemoveObjects(ids []string) { } func (d *headSync) Close() (err error) { - d.periodicSync.Close() - return nil + if d.isRunning { + d.periodicSync.Close() + } + return } func (d *headSync) fillDiff(objectIds []string) { diff --git a/commonspace/headsync/headsync_test.go b/commonspace/headsync/headsync_test.go index c803edc4..3f83cdab 100644 --- a/commonspace/headsync/headsync_test.go +++ b/commonspace/headsync/headsync_test.go @@ -51,6 +51,7 @@ func TestDiffService(t *testing.T) { storageMock.EXPECT().WriteSpaceHash(hash) pSyncMock.EXPECT().Run() service.Init([]string{initId}, delState) + service.Run() }) t.Run("update heads", func(t *testing.T) { @@ -64,7 +65,9 @@ func TestDiffService(t *testing.T) { }) t.Run("close", func(t *testing.T) { + pSyncMock.EXPECT().Run() pSyncMock.EXPECT().Close() + service.Run() service.Close() }) } diff --git a/commonspace/space.go b/commonspace/space.go index 9a22f453..332a0c0e 100644 --- a/commonspace/space.go +++ b/commonspace/space.go @@ -96,6 +96,7 @@ func NewSpaceId(id string, repKey uint64) string { type Space interface { Id() string Init(ctx context.Context) error + StartHeadSync() StoredIds() []string DebugAllHeads() []headsync.TreeHeads @@ -231,6 +232,10 @@ func (s *space) Init(ctx context.Context) (err error) { return nil } +func (s *space) StartHeadSync() { + s.headSync.Run() +} + func (s *space) ObjectSync() objectsync.ObjectSync { return s.objectSync }