-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide root node immediately when add and pin add
License: MIT Signed-off-by: Michael Avila <[email protected]>
- Loading branch information
1 parent
245c40b
commit 9301556
Showing
8 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package coreapi | ||
|
||
import ( | ||
cid "github.com/ipfs/go-cid" | ||
) | ||
|
||
type ProviderAPI CoreAPI | ||
|
||
func (api *ProviderAPI) Provide(root cid.Cid) error { | ||
return api.provider.Provide(root) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Package provider implements structures and methods to provide blocks, | ||
// keep track of which blocks are provided, and to allow those blocks to | ||
// be reprovided. | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"github.com/ipfs/go-cid" | ||
logging "github.com/ipfs/go-log" | ||
"github.com/libp2p/go-libp2p-routing" | ||
"time" | ||
) | ||
|
||
var ( | ||
log = logging.Logger("provider") | ||
) | ||
|
||
const ( | ||
provideOutgoingWorkerLimit = 8 | ||
provideOutgoingTimeout = 15 * time.Second | ||
) | ||
|
||
type Strategy func(context.Context, cid.Cid) <-chan cid.Cid | ||
|
||
// Provider announces blocks to the network, tracks which blocks are | ||
// being provided, and untracks blocks when they're no longer in the blockstore. | ||
type Provider struct { | ||
ctx context.Context | ||
// the CIDs for which provide announcements should be made | ||
queue *Queue | ||
// used to announce providing to the network | ||
contentRouting routing.ContentRouting | ||
} | ||
|
||
func NewProvider(ctx context.Context, queue *Queue, contentRouting routing.ContentRouting) *Provider { | ||
return &Provider{ | ||
ctx: ctx, | ||
queue: queue, | ||
contentRouting: contentRouting, | ||
} | ||
} | ||
|
||
// Start workers to handle provide requests. | ||
func (p *Provider) Run() { | ||
p.queue.Run() | ||
p.handleAnnouncements() | ||
} | ||
|
||
// Provide the given cid using specified strategy. | ||
func (p *Provider) Provide(root cid.Cid) error { | ||
return p.queue.Enqueue(root) | ||
} | ||
|
||
// Handle all outgoing cids by providing (announcing) them | ||
func (p *Provider) handleAnnouncements() { | ||
for workers := 0; workers < provideOutgoingWorkerLimit; workers++ { | ||
go func() { | ||
for { | ||
select { | ||
case <-p.ctx.Done(): | ||
return | ||
case entry := <-p.queue.Dequeue(): | ||
if err := doProvide(p.ctx, p.contentRouting, entry.cid); err != nil { | ||
log.Warningf("Unable to provide entry: %s, %s", entry.cid, err) | ||
} | ||
|
||
if err := entry.Complete(); err != nil { | ||
log.Warningf("Unable to complete queue entry when providing: %s, %s", entry.cid, err) | ||
} | ||
} | ||
} | ||
}() | ||
} | ||
} | ||
|
||
// TODO: better document this provide logic | ||
func doProvide(ctx context.Context, contentRouting routing.ContentRouting, key cid.Cid) error { | ||
// announce | ||
log.Info("announce - start - ", key) | ||
ctx, cancel := context.WithTimeout(ctx, provideOutgoingTimeout) | ||
if err := contentRouting.Provide(ctx, key, true); err != nil { | ||
log.Warningf("Failed to provide cid: %s", err) | ||
// TODO: Maybe put these failures onto a failures queue? | ||
cancel() | ||
return err | ||
} | ||
cancel() | ||
log.Info("announce - end - ", key) | ||
return nil | ||
} |
Oops, something went wrong.