github.com/ethersphere/bee/v2@v2.2.0/pkg/replicas/putter.go (about) 1 // Copyright 2020 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // the code below implements the integration of dispersed replicas in chunk upload. 6 // using storage.Putter interface. 7 package replicas 8 9 import ( 10 "context" 11 "errors" 12 "sync" 13 14 "github.com/ethersphere/bee/v2/pkg/file/redundancy" 15 "github.com/ethersphere/bee/v2/pkg/soc" 16 "github.com/ethersphere/bee/v2/pkg/storage" 17 "github.com/ethersphere/bee/v2/pkg/swarm" 18 ) 19 20 // putter is the private implementation of the public storage.Putter interface 21 // putter extends the original putter to a concurrent multiputter 22 type putter struct { 23 putter storage.Putter 24 } 25 26 // NewPutter is the putter constructor 27 func NewPutter(p storage.Putter) storage.Putter { 28 return &putter{p} 29 } 30 31 // Put makes the getter satisfy the storage.Getter interface 32 func (p *putter) Put(ctx context.Context, ch swarm.Chunk) (err error) { 33 rlevel := redundancy.GetLevelFromContext(ctx) 34 errs := []error{} 35 if rlevel == 0 { 36 return nil 37 } 38 39 rr := newReplicator(ch.Address(), rlevel) 40 errc := make(chan error, rlevel.GetReplicaCount()) 41 wg := sync.WaitGroup{} 42 for r := range rr.c { 43 r := r 44 wg.Add(1) 45 go func() { 46 defer wg.Done() 47 sch, err := soc.New(r.id, ch).Sign(signer) 48 if err == nil { 49 err = p.putter.Put(ctx, sch) 50 } 51 errc <- err 52 }() 53 } 54 55 wg.Wait() 56 close(errc) 57 for err := range errc { 58 errs = append(errs, err) 59 } 60 return errors.Join(errs...) 61 }