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 { type service struct {
accountId string accountId string
config Configuration config Configuration
source Source source Source
store Store store Store
last NodeConf last NodeConf
mu sync.RWMutex mu sync.RWMutex
updateCtx context.Context
updateCtxCancel context.CancelFunc
} }
func (s *service) Init(a *app.App) (err error) { 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.config = a.MustComponent("config").(ConfigGetter).GetNodeConf()
s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId s.accountId = a.MustComponent(commonaccount.CName).(commonaccount.Service).Account().PeerId
s.source = a.MustComponent(CNameSource).(Source) 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) { 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) err := s.updateConfiguration(ctx)
if err != nil { if err != nil {
if err == ErrConfigurationNotChanged { if err == ErrConfigurationNotChanged {
@ -201,5 +211,8 @@ func (s *service) NodeTypes(nodeId string) []NodeType {
} }
func (s *service) Close(ctx context.Context) (err error) { func (s *service) Close(ctx context.Context) (err error) {
if s.updateCtxCancel != nil {
s.updateCtxCancel()
}
return return
} }