Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Make worker count configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelavila committed Jun 12, 2019
1 parent 8e30fff commit 94eb43f
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions simple/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (

var logP = logging.Logger("provider.simple")

const (
provideOutgoingWorkerLimit = 8
)

// Provider announces blocks to the network
type Provider struct {
ctx context.Context
Expand All @@ -28,6 +24,8 @@ type Provider struct {
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
}

type Option func(*Provider)
Expand All @@ -38,12 +36,19 @@ func WithTimeout(timeout time.Duration) Option {
}
}

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 {
Expand Down Expand Up @@ -72,7 +77,7 @@ func (p *Provider) Provide(root cid.Cid) error {

// Handle all outgoing cids by providing (announcing) them
func (p *Provider) handleAnnouncements() {
for workers := 0; workers < provideOutgoingWorkerLimit; workers++ {
for workers := 0; workers < p.workerLimit; workers++ {
go func() {
for p.ctx.Err() == nil {
select {
Expand Down

0 comments on commit 94eb43f

Please sign in to comment.