This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
provider.go
111 lines (95 loc) · 2.66 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Package simple implements structures and methods to provide blocks,
// keep track of which blocks are provided, and to allow those blocks to
// be reprovided.
package simple
import (
"context"
"time"
"github.com/ipfs/go-cid"
q "github.com/ipfs/go-ipfs-provider/queue"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/routing"
)
var logP = logging.Logger("provider.simple")
// Provider announces blocks to the network
type Provider struct {
ctx context.Context
// the CIDs for which provide announcements should be made
queue *q.Queue
// used to announce providing to the network
contentRouting routing.ContentRouting
// how long to wait for announce to complete before giving up
timeout time.Duration
// how many workers concurrently work through thhe queue
workerLimit int
}
// Option defines the functional option type that can be used to configure
// provider instances
type Option func(*Provider)
// WithTimeout is an option to set a timeout on a provider
func WithTimeout(timeout time.Duration) Option {
return func(p *Provider) {
p.timeout = timeout
}
}
// MaxWorkers is an option to set the max workers on a provider
func MaxWorkers(count int) Option {
return func(p *Provider) {
p.workerLimit = count
}
}
// NewProvider creates a provider that announces blocks to the network using a content router
func NewProvider(ctx context.Context, queue *q.Queue, contentRouting routing.ContentRouting, options ...Option) *Provider {
p := &Provider{
ctx: ctx,
queue: queue,
contentRouting: contentRouting,
workerLimit: 8,
}
for _, option := range options {
option(p)
}
return p
}
// Close stops the provider
func (p *Provider) Close() error {
return p.queue.Close()
}
// Run workers to handle provide requests.
func (p *Provider) 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 < p.workerLimit; workers++ {
go func() {
for p.ctx.Err() == nil {
select {
case <-p.ctx.Done():
return
case c := <-p.queue.Dequeue():
p.doProvide(c)
}
}
}()
}
}
func (p *Provider) doProvide(c cid.Cid) {
ctx := p.ctx
if p.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, p.timeout)
defer cancel()
} else {
ctx = p.ctx
}
logP.Info("announce - start - ", c)
if err := p.contentRouting.Provide(ctx, c, true); err != nil {
logP.Warningf("Unable to provide entry: %s, %s", c, err)
}
logP.Info("announce - end - ", c)
}