Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
improvement attempt number 1
Browse files Browse the repository at this point in the history
  • Loading branch information
whyrusleeping committed Aug 30, 2018
1 parent 4769613 commit 287e22f
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bitswap
import (
"context"
"fmt"
"math/rand"
"time"

notifications "github.com/ipfs/go-bitswap/notifications"
Expand Down Expand Up @@ -283,7 +284,22 @@ func (s *Session) wantBlocks(ctx context.Context, ks []*cid.Cid) {
for _, c := range ks {
s.liveWants[c.KeyString()] = now
}
s.bs.wm.WantBlocks(ctx, ks, s.activePeersArr, s.id)
if len(s.activePeers) == 0 {
s.bs.wm.WantBlocks(ctx, ks, s.activePeersArr, s.id)
} else {
spl := divvy(ks, len(s.activePeersArr))
for ki, pi := range rand.Perm(len(s.activePeersArr)) {
s.bs.wm.WantBlocks(ctx, spl[ki], []peer.ID{s.activePeersArr[pi]}, s.id)
}
}
}

func divvy(ks []*cid.Cid, n int) [][]*cid.Cid {
out := make([][]*cid.Cid, n)
for i, c := range ks {
out[i%n] = append(out[i%n], c)
}
return out
}

func (s *Session) cancel(keys []*cid.Cid) {
Expand Down

0 comments on commit 287e22f

Please sign in to comment.