-
Notifications
You must be signed in to change notification settings - Fork 338
/
steward.go
78 lines (67 loc) · 2.15 KB
/
steward.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
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package stewardess provides convenience methods
// for reseeding content on Swarm.
package steward
import (
"context"
"errors"
"fmt"
"github.com/ethersphere/bee/pkg/pushsync"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/topology"
"github.com/ethersphere/bee/pkg/traversal"
"golang.org/x/sync/errgroup"
)
// how many parallel push operations
const parallelPush = 5
type Reuploader interface {
// Reupload root hash and all of its underlying
// associated chunks to the network.
Reupload(context.Context, swarm.Address) error
}
type steward struct {
getter storage.Getter
push pushsync.PushSyncer
traverser traversal.Traverser
}
func New(getter storage.Getter, t traversal.Traverser, p pushsync.PushSyncer) Reuploader {
return &steward{getter: getter, push: p, traverser: t}
}
// Reupload content with the given root hash to the network.
// The service will automatically dereference and traverse all
// addresses and push every chunk individually to the network.
// It assumes all chunks are available locally. It is therefore
// advisable to pin the content locally before trying to reupload it.
func (s *steward) Reupload(ctx context.Context, root swarm.Address) error {
sem := make(chan struct{}, parallelPush)
eg, _ := errgroup.WithContext(ctx)
fn := func(addr swarm.Address) error {
c, err := s.getter.Get(ctx, storage.ModeGetSync, addr)
if err != nil {
return err
}
sem <- struct{}{}
eg.Go(func() error {
defer func() { <-sem }()
_, err := s.push.PushChunkToClosest(ctx, c)
if err != nil {
if !errors.Is(err, topology.ErrWantSelf) {
return err
}
// swallow the error in case we are the closest node
}
return nil
})
return nil
}
if err := s.traverser.Traverse(ctx, root, fn); err != nil {
return fmt.Errorf("traversal of %s failed: %w", root.String(), err)
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("push error during reupload: %w", err)
}
return nil
}