1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-08 05:57:03 +09:00

nodeconf: stop update loop on close

This commit is contained in:
Sergey Cherepanov 2023-04-17 15:59:43 +02:00 committed by Mikhail Iudin
parent b7eb185463
commit d819f7773e
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0

View file

@ -30,15 +30,18 @@ type Service interface {
}
type service struct {
accountId string
config Configuration
source Source
store Store
last NodeConf
mu sync.RWMutex
accountId string
config Configuration
source Source
store Store
last NodeConf
mu sync.RWMutex
updateCtx context.Context
updateCtxCancel context.CancelFunc
}
func (s *service) Init(a *app.App) (err error) {
s.updateCtx, s.updateCtxCancel = context.WithCancel(context.Background())
s.config = a.MustComponent("config").(ConfigGetter).GetNodeConf()
s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId
s.source = a.MustComponent(CNameSource).(Source)
@ -61,7 +64,14 @@ func (s *service) Run(_ context.Context) (err error) {
}
func (s *service) updateLoop(ctx context.Context) {
for _ = range time.NewTicker(time.Minute * 10).C {
ticker := time.NewTicker(time.Minute * 10)
defer ticker.Stop()
for {
select {
case <-s.updateCtx.Done():
return
case <-ticker.C:
}
err := s.updateConfiguration(ctx)
if err != nil {
if err == ErrConfigurationNotChanged {
@ -201,5 +211,8 @@ func (s *service) NodeTypes(nodeId string) []NodeType {
}
func (s *service) Close(ctx context.Context) (err error) {
if s.updateCtxCancel != nil {
s.updateCtxCancel()
}
return
}