diff --git a/pkg/app/appdisc/discovery_manager.go b/pkg/app/appdisc/discovery_manager.go index 0a0d807817..2259f95f7c 100644 --- a/pkg/app/appdisc/discovery_manager.go +++ b/pkg/app/appdisc/discovery_manager.go @@ -2,6 +2,7 @@ package appdisc import ( "context" + "sync" "github.com/skycoin/skywire/pkg/servicedisc" ) @@ -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() { @@ -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 + } + }) } diff --git a/pkg/servicedisc/client.go b/pkg/servicedisc/client.go index 21b88ed52f..c602ade2bc 100644 --- a/pkg/servicedisc/client.go +++ b/pkg/servicedisc/client.go @@ -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 } @@ -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 } @@ -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 @@ -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 }