-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcurrency.go
81 lines (70 loc) · 1.45 KB
/
concurrency.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
package consistentio
import (
"fmt"
"sync/atomic"
)
type Err struct {
n int
err error
reqID uint64
}
type info struct {
reqID uint64
key string
data []byte
err chan Err
}
// ConcurrentConsistentIO write to io.Writer(s) concurrently.
type ConcurrentConsistentIO struct {
cio *ConsistentIO
chs map[string]chan info
reqID uint64
}
func NewConcurrentConsistentIO(cio *ConsistentIO) (*ConcurrentConsistentIO, error) {
if cio == nil {
return nil, fmt.Errorf("ConsistentIO has not been specified")
}
o := cio.o
if o == nil {
return nil, fmt.Errorf("options has not been specified")
}
routines := len(o.Writers)
if routines == 0 {
return nil, fmt.Errorf("writers have not been specified")
}
ccio := &ConcurrentConsistentIO{
cio: cio,
}
chs := make(map[string]chan info)
for i := 0; i < routines; i++ {
ch := make(chan info, 10) // FIXME: add to options
w := o.Writers[i]
chs[w.key] = ch
go ccio.write(ch)
}
ccio.chs = chs
return ccio, nil
}
func (ccio *ConcurrentConsistentIO) Write(key string, p []byte) (reqID uint64, ch chan Err) {
reqID = atomic.AddUint64(&ccio.reqID, 1)
ch = make(chan Err, 1)
ccio.chs[key] <- info{
reqID: reqID,
key: key,
data: p,
err: ch,
}
return reqID, ch
}
func (ccio *ConcurrentConsistentIO) write(ch chan info) {
for i := range ch {
k := i.key
w := ccio.cio.writers[k]
n, err := w.Write(i.data)
i.err <- Err{
n: n,
err: err,
reqID: i.reqID,
}
}
}