Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit af66214

Browse files
committedMay 12, 2015
more cleanup, and improve test perf
1 parent 006b124 commit af66214

File tree

5 files changed

+14
-52
lines changed

5 files changed

+14
-52
lines changed
 

‎exchange/bitswap/bitswap.go

+7-31
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ func (bs *Bitswap) GetBlocks(ctx context.Context, keys []u.Key) (<-chan *blocks.
223223
// HasBlock announces the existance of a block to this bitswap service. The
224224
// service will potentially notify its peers.
225225
func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
226-
log.Event(ctx, "hasBlock", blk)
227226
select {
228227
case <-bs.process.Closing():
229228
return errors.New("bitswap is closed")
@@ -233,6 +232,7 @@ func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
233232
if err := bs.blockstore.Put(blk); err != nil {
234233
return err
235234
}
235+
236236
bs.wantlist.Remove(blk.Key())
237237
bs.notifications.Publish(blk)
238238
select {
@@ -245,7 +245,6 @@ func (bs *Bitswap) HasBlock(ctx context.Context, blk *blocks.Block) error {
245245

246246
func (bs *Bitswap) sendWantlistMsgToPeers(ctx context.Context, m bsmsg.BitSwapMessage, peers <-chan peer.ID) error {
247247
set := pset.New()
248-
wg := sync.WaitGroup{}
249248

250249
loop:
251250
for {
@@ -259,37 +258,22 @@ loop:
259258
continue
260259
}
261260

262-
wg.Add(1)
263-
go func(p peer.ID) {
264-
defer wg.Done()
265-
if err := bs.send(ctx, p, m); err != nil {
266-
log.Debug(err) // TODO remove if too verbose
267-
}
268-
}(peerToQuery)
261+
bs.pm.Send(peerToQuery, m)
269262
case <-ctx.Done():
270263
return nil
271264
}
272265
}
273-
done := make(chan struct{})
274-
go func() {
275-
wg.Wait()
276-
close(done)
277-
}()
278-
279-
select {
280-
case <-done:
281-
case <-ctx.Done():
282-
// NB: we may be abandoning goroutines here before they complete
283-
// this shouldnt be an issue because they will complete soon anyways
284-
// we just don't want their being slow to impact bitswap transfer speeds
285-
}
286266
return nil
287267
}
288268

289269
func (bs *Bitswap) sendWantlistToPeers(ctx context.Context, peers <-chan peer.ID) error {
270+
entries := bs.wantlist.Entries()
271+
if len(entries) == 0 {
272+
return nil
273+
}
290274
message := bsmsg.New()
291275
message.SetFull(true)
292-
for _, wanted := range bs.wantlist.Entries() {
276+
for _, wanted := range entries {
293277
message.AddEntry(wanted.Key, wanted.Priority)
294278
}
295279
return bs.sendWantlistMsgToPeers(ctx, message, peers)
@@ -413,14 +397,6 @@ func (bs *Bitswap) ReceiveError(err error) {
413397
// TODO bubble the network error up to the parent context/error logger
414398
}
415399

416-
// send strives to ensure that accounting is always performed when a message is
417-
// sent
418-
func (bs *Bitswap) send(ctx context.Context, p peer.ID, m bsmsg.BitSwapMessage) error {
419-
//defer log.EventBegin(ctx, "sendMessage", p, m).Done()
420-
bs.pm.Send(p, m)
421-
return bs.engine.MessageSent(p, m)
422-
}
423-
424400
func (bs *Bitswap) Close() error {
425401
return bs.process.Close()
426402
}

‎exchange/bitswap/bitswap_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestLargeSwarm(t *testing.T) {
6868
if testing.Short() {
6969
t.SkipNow()
7070
}
71-
numInstances := 300
71+
numInstances := 500
7272
numBlocks := 2
7373
if detectrace.WithRace() {
7474
// when running with the race detector, 500 instances launches

‎exchange/bitswap/decision/engine.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (e *Engine) MessageReceived(p peer.ID, m bsmsg.BitSwapMessage) error {
216216
}
217217

218218
for _, block := range m.Blocks() {
219-
log.Debug("got block %s %d bytes", block.Key(), len(block.Data))
219+
log.Debugf("got block %s %d bytes", block.Key(), len(block.Data))
220220
l.ReceivedBytes(len(block.Data))
221221
for _, l := range e.ledgerMap {
222222
if entry, ok := l.WantListContains(block.Key()); ok {

‎exchange/bitswap/message/message.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ type BitSwapMessage interface {
3131

3232
Empty() bool
3333

34-
// ClearBlocks removes all blocks from this message
35-
ClearBlocks()
36-
3734
// Sets whether or not the contained wantlist represents the entire wantlist
3835
// true = full wantlist
3936
// false = wantlist 'patch'
@@ -56,7 +53,7 @@ type Exportable interface {
5653
type impl struct {
5754
full bool
5855
wantlist map[u.Key]Entry
59-
blocks map[u.Key]*blocks.Block // map to detect duplicates
56+
blocks []*blocks.Block
6057
}
6158

6259
func New() BitSwapMessage {
@@ -65,7 +62,6 @@ func New() BitSwapMessage {
6562

6663
func newMsg() *impl {
6764
return &impl{
68-
blocks: make(map[u.Key]*blocks.Block),
6965
wantlist: make(map[u.Key]Entry),
7066
full: true,
7167
}
@@ -110,15 +106,7 @@ func (m *impl) Wantlist() []Entry {
110106
}
111107

112108
func (m *impl) Blocks() []*blocks.Block {
113-
bs := make([]*blocks.Block, 0)
114-
for _, block := range m.blocks {
115-
bs = append(bs, block)
116-
}
117-
return bs
118-
}
119-
120-
func (m *impl) ClearBlocks() {
121-
m.blocks = make(map[u.Key]*blocks.Block)
109+
return m.blocks
122110
}
123111

124112
func (m *impl) Cancel(k u.Key) {
@@ -147,7 +135,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
147135
}
148136

149137
func (m *impl) AddBlock(b *blocks.Block) {
150-
m.blocks[b.Key()] = b
138+
m.blocks = append(m.blocks, b)
151139
}
152140

153141
func FromNet(r io.Reader) (BitSwapMessage, error) {

‎exchange/bitswap/testutils.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ func (g *SessionGenerator) Instances(n int) []Instance {
5656
instances = append(instances, inst)
5757
}
5858
for i, inst := range instances {
59-
for j, oinst := range instances {
60-
if i == j {
61-
continue
62-
}
59+
for j := i + 1; j < len(instances); j++ {
60+
oinst := instances[j]
6361
inst.Exchange.PeerConnected(oinst.Peer)
6462
}
6563
}

0 commit comments

Comments
 (0)
Please sign in to comment.