Commit ead5a9dc authored by Jeromy's avatar Jeromy

allow bitswap stat to output wasted bytes

bitswap stat can now track bytes that are wasted by receiving duplicate
blocks.

ps, gitcop smells

License: MIT
Signed-off-by: default avatarJeromy <jeromyj@gmail.com>
parent afee5e33
......@@ -131,6 +131,7 @@ type Bitswap struct {
counterLk sync.Mutex
blocksRecvd int
dupBlocksRecvd int
dupDataRecvd uint64
}
type blockRequest struct {
......@@ -320,7 +321,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
go func(b *blocks.Block) {
defer wg.Done()
if err := bs.updateReceiveCounters(b.Key()); err != nil {
if err := bs.updateReceiveCounters(b); err != nil {
return // ignore error, is either logged previously, or ErrAlreadyHaveBlock
}
......@@ -338,17 +339,18 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
var ErrAlreadyHaveBlock = errors.New("already have block")
func (bs *Bitswap) updateReceiveCounters(k key.Key) error {
func (bs *Bitswap) updateReceiveCounters(b *blocks.Block) error {
bs.counterLk.Lock()
defer bs.counterLk.Unlock()
bs.blocksRecvd++
has, err := bs.blockstore.Has(k)
has, err := bs.blockstore.Has(b.Key())
if err != nil {
log.Infof("blockstore.Has error: %s", err)
return err
}
if err == nil && has {
bs.dupBlocksRecvd++
bs.dupDataRecvd += uint64(len(b.Data))
}
if has {
......
......@@ -11,6 +11,7 @@ type Stat struct {
Peers []string
BlocksReceived int
DupBlksReceived int
DupDataReceived uint64
}
func (bs *Bitswap) Stat() (*Stat, error) {
......@@ -20,6 +21,7 @@ func (bs *Bitswap) Stat() (*Stat, error) {
bs.counterLk.Lock()
st.BlocksReceived = bs.blocksRecvd
st.DupBlksReceived = bs.dupBlocksRecvd
st.DupDataReceived = bs.dupDataRecvd
bs.counterLk.Unlock()
for _, p := range bs.engine.Peers() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment