Skip to content

Commit

Permalink
added retry on sequence error for update
Browse files Browse the repository at this point in the history
  • Loading branch information
ivcosla committed May 15, 2019
1 parent 1c1c6ca commit 2bdd5a6
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions pkg/messaging-discovery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"

"github.com/skycoin/skywire/pkg/cipher"
Expand All @@ -25,8 +26,9 @@ type APIClient interface {
// HTTPClient represents a client that communicates with a messaging-discovery service through http, it
// implements APIClient
type httpClient struct {
client http.Client
address string
client http.Client
address string
updateMux sync.Mutex // for thread-safe sequence incrementing
}

// NewHTTP constructs a new APIClient that communicates with discovery via http.
Expand Down Expand Up @@ -116,19 +118,41 @@ func (c *httpClient) SetEntry(ctx context.Context, e *Entry) error {

// UpdateEntry updates Entry in messaging discovery.
func (c *httpClient) UpdateEntry(ctx context.Context, sk cipher.SecKey, e *Entry) error {
c.updateMux.Lock()
defer c.updateMux.Unlock()

e.Sequence++
e.Timestamp = time.Now().UnixNano()
err := e.Sign(sk)
if err != nil {
return err
}

err = c.SetEntry(ctx, e)
if err != nil {
e.Sequence--
for {
err = c.SetEntry(ctx, e)
if err != nil && err != ErrValidationWrongSequence {
e.Sequence--
return err
}
if err == ErrValidationWrongSequence {
rE, entryErr := c.Entry(ctx, e.Static)
if entryErr != nil {
return err
}
if rE.Timestamp > e.Timestamp { // If there is a more up to date entry drop update
e.Sequence = rE.Sequence
return nil
}
e.Sequence = rE.Sequence + 1
err := e.Sign(sk)
if err != nil {
return err
}
}
if err == nil {
return nil
}
}

return err
}

// AvailableServers returns list of available servers.
Expand Down

0 comments on commit 2bdd5a6

Please sign in to comment.