@cryptotaxi247 / kubo / commits / ce684ab73

refactor: cleanup bitswap metrics collection

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Jan 20, 2017 at 14:13 UTC ce684ab73e15988dd6ecb8073e84c905ce4ed1f9
1 file changed +12 -14
exchange/bitswap/bitswap.go
+12 -14
@@ -368,9 +368,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
368 go func(b blocks.Block) {
369 defer wg.Done()
370
371 - if err := bs.updateReceiveCounters(b); err != nil {
372 - return // ignore error, is either logged previously, or ErrAlreadyHaveBlock
373 - }
371 + bs.updateReceiveCounters(b)
372
373 k := b.Cid()
374 log.Event(ctx, "Bitswap.GetBlockRequest.End", k)
@@ -386,27 +384,27 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
384
385 var ErrAlreadyHaveBlock = errors.New("already have block")
386
389 -func (bs *Bitswap) updateReceiveCounters(b blocks.Block) error {
390 - bs.counterLk.Lock()
391 - defer bs.counterLk.Unlock()
387 +func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
388 blkLen := len(b.RawData())
393 - bs.allMetric.Observe(float64(blkLen))
394 - bs.blocksRecvd++
389 has, err := bs.blockstore.Has(b.Cid())
390 if err != nil {
391 log.Infof("blockstore.Has error: %s", err)
398 - return err
392 + return
393 }
400 - if err == nil && has {
394 +
395 + bs.allMetric.Observe(float64(blkLen))
396 + if has {
397 bs.dupMetric.Observe(float64(blkLen))
402 - bs.dupBlocksRecvd++
403 - bs.dupDataRecvd += uint64(blkLen)
398 }
399
400 + bs.counterLk.Lock()
401 + defer bs.counterLk.Unlock()
402 +
403 + bs.blocksRecvd++
404 if has {
407 - return ErrAlreadyHaveBlock
405 + bs.dupBlocksRecvd++
406 + bs.dupDataRecvd += uint64(blkLen)
407 }
409 - return nil
408 }
409
410 // Connected/Disconnected warns bitswap about peer connections