Skip to content

Commit

Permalink
now removing transport from discovery on transport deregister
Browse files Browse the repository at this point in the history
# Conflicts:
#	go.mod
  • Loading branch information
ivcosla authored and nkryuchkov committed Nov 7, 2019
1 parent f6f0681 commit 82c7047
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
30 changes: 30 additions & 0 deletions pkg/transport-discovery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func (c *apiClient) Get(ctx context.Context, path string) (*http.Response, error
return c.client.Do(req.WithContext(ctx))
}

// Delete performs a new DELETE request.
func (c *apiClient) Delete(ctx context.Context, path string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodDelete, c.client.Addr()+path, new(bytes.Buffer))
if err != nil {
return nil, err
}

return c.client.Do(req.WithContext(ctx))
}

// RegisterTransports registers new Transports.
func (c *apiClient) RegisterTransports(ctx context.Context, entries ...*transport.SignedEntry) error {
if len(entries) == 0 {
Expand Down Expand Up @@ -150,6 +160,26 @@ func (c *apiClient) GetTransportsByEdge(ctx context.Context, pk cipher.PubKey) (
return entries, nil
}

// DeleteTransport deletes given transpot by it's ID. A visor can only delete transports if he is one of it's edges.
func (c *apiClient) DeleteTransport(ctx context.Context, id uuid.UUID) error {
resp, err := c.Delete(ctx, fmt.Sprintf("/transports/id:%s", id.String()))
if resp != nil {
defer func() {
if err := resp.Body.Close(); err != nil {
log.WithError(err).Warn("Failed to close HTTP response body")
}
}()
}
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status: %d, error: %v", resp.StatusCode, extractError(resp.Body))
}

return nil
}

// UpdateStatuses updates statuses of transports in discovery.
func (c *apiClient) UpdateStatuses(ctx context.Context, statuses ...*transport.Status) ([]*transport.EntryWithStatus, error) {
if len(statuses) == 0 {
Expand Down
16 changes: 16 additions & 0 deletions pkg/transport/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type DiscoveryClient interface {
RegisterTransports(ctx context.Context, entries ...*SignedEntry) error
GetTransportByID(ctx context.Context, id uuid.UUID) (*EntryWithStatus, error)
GetTransportsByEdge(ctx context.Context, pk cipher.PubKey) ([]*EntryWithStatus, error)
DeleteTransport(ctx context.Context, id uuid.UUID) error
UpdateStatuses(ctx context.Context, statuses ...*Status) ([]*EntryWithStatus, error)
}

Expand Down Expand Up @@ -81,6 +82,21 @@ func (td *mockDiscoveryClient) GetTransportsByEdge(ctx context.Context, pk ciphe
return res, nil
}

// NOTE that mock implementation doesn't checks wheter the transport to be deleted is valid or not, this is, that
// it can be deleted by the visor who called DeleteTransport
func (td *mockDiscoveryClient) DeleteTransport(ctx context.Context, id uuid.UUID) error {
td.Lock()
defer td.Unlock()

_, ok := td.entries[id]
if !ok {
return errors.New("transport not found")
}

delete(td.entries, id)
return nil
}

func (td *mockDiscoveryClient) UpdateStatuses(ctx context.Context, statuses ...*Status) ([]*EntryWithStatus, error) {
res := make([]*EntryWithStatus, 0)
for _, status := range statuses {
Expand Down
11 changes: 10 additions & 1 deletion pkg/transport/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"strings"
"sync"
"time"

"github.com/SkycoinProject/skywire-mainnet/internal/skyenv"

Expand Down Expand Up @@ -224,7 +225,15 @@ func (tm *Manager) DeleteTransport(id uuid.UUID) {
if tp, ok := tm.tps[id]; ok {
tp.Close()
delete(tm.tps, id)
tm.Logger.Infof("Unregistered transport %s", id)
tm.Logger.Infof("Unregistered transport %s from manager", id)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := tm.conf.DiscoveryClient.DeleteTransport(ctx, id)
if err != nil {
tm.Logger.Errorf("Unregister transport %s from discovery failed with error: %s", id, err)
}
tm.Logger.Infof("Unregistered transport %s from discovery", id)
}
}

Expand Down

0 comments on commit 82c7047

Please sign in to comment.