-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (109 loc) · 2.88 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"time"
multierror "github.com/hashicorp/go-multierror"
)
func main() {
ctx := context.Background()
blobName := "blob"
blobReader := bytes.NewReader([]byte("test content"))
storages := getStorages()
fmt.Println(uploadBlob(ctx, blobName, blobReader, storages))
}
func uploadBlob(ctx context.Context, blobName string, blobReader io.Reader, storages []*Storage) error {
var results = make(chan error, len(storages))
defer close(results)
readers, closer := getReaders(blobReader, len(storages))
// compose reader for each backend
for i, store := range storages {
go func(s *Storage, rdr io.Reader) {
select {
case <-ctx.Done(): // something failed
break
case results <- s.Save(ctx, blobName, rdr):
}
}(store, readers[i])
}
var errs error
for i := 0; i < len(storages); i++ {
if r := <-results; r != nil {
errs = multierror.Append(errs, r)
}
if i == 0 {
closer.Close()
}
}
return errs
}
func getStorages() []*Storage {
var storages []*Storage
storages = append(storages, NewStorage("aa"))
storages = append(storages, NewStorage("bb"))
storages = append(storages, NewStorage("cc"))
return storages
}
func getReaders(source io.Reader, count int) ([]io.Reader, io.Closer) {
readers := make([]io.Reader, 0, count)
pipeWriters := make([]io.Writer, 0, count)
pipeClosers := make([]io.Closer, 0, count)
for i := 0; i < count-1; i++ {
pr, pw := io.Pipe()
readers = append(readers, pr)
pipeWriters = append(pipeWriters, pw)
pipeClosers = append(pipeClosers, pw)
}
multiWriter := io.MultiWriter(pipeWriters...)
teeReader := io.TeeReader(source, multiWriter)
// append teereader so it populates data to the rest of the readers
readers = append([]io.Reader{teeReader}, readers...)
return readers, NewMultiCloser(pipeClosers)
}
type Storage struct {
rootDir string
}
func NewStorage(rootDir string) *Storage {
return &Storage{
rootDir: rootDir,
}
}
func (s *Storage) Save(c context.Context, blob string, blobReader io.Reader) error {
seed := rand.NewSource(time.Now().UnixNano())
rg := rand.New(seed)
r := rg.Int() % 1000
content, _ := ioutil.ReadAll(blobReader)
fmt.Printf("storage '%s' wrote a file '%s'\n", s.rootDir, content)
select {
case <-time.After(time.Duration(r) * time.Microsecond):
fmt.Println(r)
case <-c.Done():
return nil
}
if r%2 == 0 {
return errors.New("oh")
}
return nil
}
type MultiCloser struct {
closers []io.Closer
}
func NewMultiCloser(closers []io.Closer) *MultiCloser {
return &MultiCloser{
closers: closers,
}
}
func (m *MultiCloser) Close() error {
var err error
for _, c := range m.closers {
if e := c.Close(); e != nil {
err = multierror.Append(err, e)
}
}
return err
}