Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/shutdown race #897

Merged
merged 4 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions pkg/app/appdisc/discovery_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package appdisc

import (
"context"
"sync"

"github.com/skycoin/skywire/pkg/servicedisc"
)
Expand All @@ -24,7 +25,8 @@ func (emptyUpdater) Stop() {}

// serviceUpdater updates service-discovery entry of locally running App.
type serviceUpdater struct {
client *servicedisc.HTTPClient
client *servicedisc.HTTPClient
stopOnce sync.Once
}

func (u *serviceUpdater) Start() {
Expand All @@ -35,8 +37,10 @@ func (u *serviceUpdater) Start() {
}

func (u *serviceUpdater) Stop() {
ctx := context.Background()
if err := u.client.DeleteEntry(ctx); err != nil {
return
}
u.stopOnce.Do(func() {
ctx := context.Background()
if err := u.client.DeleteEntry(ctx); err != nil {
return
}
})
}
7 changes: 6 additions & 1 deletion pkg/servicedisc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type HTTPClient struct {
log logrus.FieldLogger
conf Config
entry Service
entryMx sync.Mutex // only used if UpdateLoop && UpdateStats functions are used.
entryMx sync.Mutex // only used if RegisterEntry && DeleteEntry functions are used.
client http.Client
}

Expand Down Expand Up @@ -166,6 +166,7 @@ func (c *HTTPClient) RegisterEntry(ctx context.Context) error {
return err
}
c.entry = entry
c.log.WithField("entry", c.entry).Debug("Entry registered successfully")
return nil
}

Expand Down Expand Up @@ -226,6 +227,9 @@ func (c *HTTPClient) postEntry(ctx context.Context) (Service, error) {

// DeleteEntry calls 'DELETE /api/services/{entry_addr}'.
func (c *HTTPClient) DeleteEntry(ctx context.Context) (err error) {
c.entryMx.Lock()
defer c.entryMx.Unlock()

auth, err := c.Auth(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -260,6 +264,7 @@ func (c *HTTPClient) DeleteEntry(ctx context.Context) (err error) {
}
return &hErr
}
c.log.WithField("entry", c.entry).Debug("Entry deleted successfully")
return nil
}

Expand Down