1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 14:07:02 +09:00

Change periodic sync init

This commit is contained in:
mcrakhman 2023-08-08 11:13:15 +02:00
parent 10687d2dc1
commit cdacae232e
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 20 additions and 8 deletions

View file

@ -111,6 +111,11 @@ func (s *syncStatusService) Init(a *app.App) (err error) {
s.spaceId = sharedState.SpaceId s.spaceId = sharedState.SpaceId
s.configuration = a.MustComponent(nodeconf.CName).(nodeconf.NodeConf) s.configuration = a.MustComponent(nodeconf.CName).(nodeconf.NodeConf)
s.storage = a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorage) s.storage = a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorage)
s.periodicSync = periodicsync.NewPeriodicSync(
s.updateIntervalSecs,
s.updateTimeout,
s.update,
log)
return return
} }
@ -126,11 +131,6 @@ func (s *syncStatusService) SetUpdateReceiver(updater UpdateReceiver) {
} }
func (s *syncStatusService) Run(ctx context.Context) error { func (s *syncStatusService) Run(ctx context.Context) error {
s.periodicSync = periodicsync.NewPeriodicSync(
s.updateIntervalSecs,
s.updateTimeout,
s.update,
log)
s.periodicSync.Run() s.periodicSync.Run()
return nil return nil
} }
@ -290,9 +290,6 @@ func (s *syncStatusService) RemoveAllExcept(senderId string, differentRemoteIds
} }
func (s *syncStatusService) Close(ctx context.Context) error { func (s *syncStatusService) Close(ctx context.Context) error {
if s.periodicSync == nil {
return nil
}
s.periodicSync.Close() s.periodicSync.Close()
return nil return nil
} }

View file

@ -5,6 +5,7 @@ import (
"context" "context"
"github.com/anyproto/any-sync/app/logger" "github.com/anyproto/any-sync/app/logger"
"go.uber.org/zap" "go.uber.org/zap"
"sync/atomic"
"time" "time"
) )
@ -39,9 +40,11 @@ type periodicCall struct {
loopDone chan struct{} loopDone chan struct{}
periodSeconds int periodSeconds int
timeout time.Duration timeout time.Duration
isRunning atomic.Bool
} }
func (p *periodicCall) Run() { func (p *periodicCall) Run() {
p.isRunning.Store(true)
go p.loop(p.periodSeconds) go p.loop(p.periodSeconds)
} }
@ -75,6 +78,9 @@ func (p *periodicCall) loop(periodSeconds int) {
} }
func (p *periodicCall) Close() { func (p *periodicCall) Close() {
if !p.isRunning.Load() {
return
}
p.loopCancel() p.loopCancel()
<-p.loopDone <-p.loopDone
} }

View file

@ -45,4 +45,13 @@ func TestPeriodicSync_Run(t *testing.T) {
pSync.Close() pSync.Close()
require.Equal(t, 2, times) require.Equal(t, 2, times)
}) })
t.Run("loop close not running", func(t *testing.T) {
secs := 0
diffSyncer := func(ctx context.Context) (err error) {
return nil
}
pSync := NewPeriodicSync(secs, 0, diffSyncer, l)
pSync.Close()
})
} }