Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement a worker to consolidate HasBlock provide calls into one #860

Merged
merged 3 commits into from
Mar 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
batchRequests: make(chan *blockRequest, sizeBatchRequestChan),
process: px,
newBlocks: make(chan *blocks.Block, HasBlockBufferSize),
provideKeys: make(chan u.Key),
}
network.SetDelegate(bs)

Expand Down Expand Up @@ -124,6 +125,8 @@ type Bitswap struct {
process process.Process

newBlocks chan *blocks.Block

provideKeys chan u.Key
}

type blockRequest struct {
Expand Down
43 changes: 40 additions & 3 deletions exchange/bitswap/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
inflect "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/chuckpreslar/inflect"
process "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
u "github.com/jbenet/go-ipfs/util"
)

func (bs *Bitswap) startWorkers(px process.Process, ctx context.Context) {
Expand All @@ -24,6 +25,10 @@ func (bs *Bitswap) startWorkers(px process.Process, ctx context.Context) {
bs.rebroadcastWorker(ctx)
})

px.Go(func(px process.Process) {
bs.provideCollector(ctx)
})

// Spawn up multiple workers to handle incoming blocks
// consider increasing number if providing blocks bottlenecks
// file transfers
Expand Down Expand Up @@ -58,13 +63,13 @@ func (bs *Bitswap) taskWorker(ctx context.Context) {
func (bs *Bitswap) provideWorker(ctx context.Context) {
for {
select {
case blk, ok := <-bs.newBlocks:
case k, ok := <-bs.provideKeys:
if !ok {
log.Debug("newBlocks channel closed")
log.Debug("provideKeys channel closed")
return
}
ctx, _ := context.WithTimeout(ctx, provideTimeout)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm i wonder if this cancel func must also be called-- cc @cryptix ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Missed that one... we should add this one to #822.

err := bs.network.Provide(ctx, blk.Key())
err := bs.network.Provide(ctx, k)
if err != nil {
log.Error(err)
}
Expand All @@ -74,6 +79,38 @@ func (bs *Bitswap) provideWorker(ctx context.Context) {
}
}

func (bs *Bitswap) provideCollector(ctx context.Context) {
defer close(bs.provideKeys)
var toProvide []u.Key
var nextKey u.Key
var keysOut chan u.Key

for {
select {
case blk, ok := <-bs.newBlocks:
if !ok {
log.Debug("newBlocks channel closed")
return
}
if keysOut == nil {
nextKey = blk.Key()
keysOut = bs.provideKeys
} else {
toProvide = append(toProvide, blk.Key())
}
case keysOut <- nextKey:
if len(toProvide) > 0 {
nextKey = toProvide[0]
toProvide = toProvide[1:]
} else {
keysOut = nil
}
case <-ctx.Done():
return
}
}
}

// TODO ensure only one active request per key
func (bs *Bitswap) clientWorker(parent context.Context) {
defer log.Info("bitswap client worker shutting down...")
Expand Down