Skip to content

Commit

Permalink
Merge pull request #897 from ersonp/fix/shutdown-race
Browse files Browse the repository at this point in the history
Fix/shutdown race
  • Loading branch information
jdknives authored Sep 28, 2021
2 parents e0d3bd2 + 66257ee commit 42a9d6f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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

0 comments on commit 42a9d6f

Please sign in to comment.