Skip to content

Commit

Permalink
Merge pull request #2328 from ipfs/feat/parallel-pub
Browse files Browse the repository at this point in the history
put pubkey and ipns entry to dht in parallel
  • Loading branch information
whyrusleeping committed Feb 11, 2016
2 parents 9fbe3d1 + abadc94 commit 7217637
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
23 changes: 21 additions & 2 deletions namesys/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,38 @@ func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqn
entry.Ttl = proto.Uint64(uint64(ttl.Nanoseconds()))
}

err = PublishEntry(ctx, r, ipnskey, entry)
errs := make(chan error)

go func() {
errs <- PublishEntry(ctx, r, ipnskey, entry)
}()

go func() {
errs <- PublishPublicKey(ctx, r, namekey, k.GetPublic())
}()

err = waitOnErrChan(ctx, errs)
if err != nil {
return err
}

err = PublishPublicKey(ctx, r, namekey, k.GetPublic())
err = waitOnErrChan(ctx, errs)
if err != nil {
return err
}

return nil
}

func waitOnErrChan(ctx context.Context, errs chan error) error {
select {
case err := <-errs:
return err
case <-ctx.Done():
return ctx.Err()
}
}

func PublishPublicKey(ctx context.Context, r routing.IpfsRouting, k key.Key, pubk ci.PubKey) error {
log.Debugf("Storing pubkey at: %s", k)
pkbytes, err := pubk.Bytes()
Expand Down
11 changes: 7 additions & 4 deletions namesys/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore/sync"
key "github.com/ipfs/go-ipfs/blocks/key"
path "github.com/ipfs/go-ipfs/path"
mockrouting "github.com/ipfs/go-ipfs/routing/mock"
Expand All @@ -16,8 +17,10 @@ import (
)

func TestRoutingResolve(t *testing.T) {
d := mockrouting.NewServer().Client(testutil.RandIdentityOrFatal(t))
dstore := ds.NewMapDatastore()
dstore := dssync.MutexWrap(ds.NewMapDatastore())
serv := mockrouting.NewServer()
id := testutil.RandIdentityOrFatal(t)
d := serv.ClientWithDatastore(context.Background(), id, dstore)

resolver := NewRoutingResolver(d, 0)
publisher := NewRoutingPublisher(d, dstore)
Expand Down Expand Up @@ -50,7 +53,7 @@ func TestRoutingResolve(t *testing.T) {
}

func TestPrexistingExpiredRecord(t *testing.T) {
dstore := ds.NewMapDatastore()
dstore := dssync.MutexWrap(ds.NewMapDatastore())
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), testutil.RandIdentityOrFatal(t), dstore)

resolver := NewRoutingResolver(d, 0)
Expand Down Expand Up @@ -87,7 +90,7 @@ func TestPrexistingExpiredRecord(t *testing.T) {
}

func TestPrexistingRecord(t *testing.T) {
dstore := ds.NewMapDatastore()
dstore := dssync.MutexWrap(ds.NewMapDatastore())
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), testutil.RandIdentityOrFatal(t), dstore)

resolver := NewRoutingResolver(d, 0)
Expand Down
3 changes: 2 additions & 1 deletion routing/mock/centralized_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore/sync"
key "github.com/ipfs/go-ipfs/blocks/key"
"github.com/ipfs/go-ipfs/util/testutil"
peer "gx/ipfs/QmUBogf4nUefBjmYjn6jfsfPJRkmDGSeMhNj4usRKq69f4/go-libp2p/p2p/peer"
Expand Down Expand Up @@ -74,7 +75,7 @@ func (rs *s) Providers(k key.Key) []peer.PeerInfo {
}

func (rs *s) Client(p testutil.Identity) Client {
return rs.ClientWithDatastore(context.Background(), p, ds.NewMapDatastore())
return rs.ClientWithDatastore(context.Background(), p, dssync.MutexWrap(ds.NewMapDatastore()))
}

func (rs *s) ClientWithDatastore(_ context.Context, p testutil.Identity, datastore ds.Datastore) Client {
Expand Down

0 comments on commit 7217637

Please sign in to comment.