-
Notifications
You must be signed in to change notification settings - Fork 212
/
wireup.go
103 lines (82 loc) · 2.74 KB
/
wireup.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
package disruptor
import "errors"
type Wireup struct {
waiter WaitStrategy
capacity int64
consumerGroups [][]Consumer
}
type Option func(*Wireup)
func New(options ...Option) Disruptor {
if this, err := NewWireup(options...); err != nil {
panic(err)
} else {
return NewDisruptor(this.Build())
}
}
func NewWireup(options ...Option) (*Wireup, error) {
this := &Wireup{}
WithWaitStrategy(NewWaitStrategy())(this)
for _, option := range options {
option(this)
}
if err := this.validate(); err != nil {
return nil, err
}
return this, nil
}
func (this *Wireup) validate() error {
if this.waiter == nil {
return errMissingWaitStrategy
}
if this.capacity <= 0 {
return errCapacityTooSmall
}
if this.capacity&(this.capacity-1) != 0 {
return errCapacityPowerOfTwo
}
if len(this.consumerGroups) == 0 {
return errMissingConsumers
}
for _, consumerGroup := range this.consumerGroups {
if len(consumerGroup) == 0 {
return errMissingConsumersInGroup
}
for _, consumer := range consumerGroup {
if consumer == nil {
return errEmptyConsumer
}
}
}
return nil
}
func (this *Wireup) Build() (Writer, Reader) {
var writerSequence = NewCursor()
readers, readBarrier := this.buildReaders(writerSequence)
return NewWriter(writerSequence, readBarrier, this.capacity), compositeReader(readers)
}
func (this *Wireup) buildReaders(writerSequence *Cursor) (readers []Reader, upstream Barrier) {
upstream = writerSequence
for _, consumerGroup := range this.consumerGroups {
var consumerGroupSequences []*Cursor
for _, consumer := range consumerGroup {
currentSequence := NewCursor()
readers = append(readers, NewReader(currentSequence, writerSequence, upstream, this.waiter, consumer))
consumerGroupSequences = append(consumerGroupSequences, currentSequence)
}
upstream = NewCompositeBarrier(consumerGroupSequences...)
}
return readers, upstream
}
func WithWaitStrategy(value WaitStrategy) Option { return func(this *Wireup) { this.waiter = value } }
func WithCapacity(value int64) Option { return func(this *Wireup) { this.capacity = value } }
func WithConsumerGroup(value ...Consumer) Option {
return func(this *Wireup) { this.consumerGroups = append(this.consumerGroups, value) }
}
var (
errMissingWaitStrategy = errors.New("a wait strategy must be provided")
errCapacityTooSmall = errors.New("the capacity must be at least 1")
errCapacityPowerOfTwo = errors.New("the capacity be a power of two, e.g. 2, 4, 8, 16")
errMissingConsumers = errors.New("no consumers have been provided")
errMissingConsumersInGroup = errors.New("the consumer group does not have any consumers")
errEmptyConsumer = errors.New("an empty consumer was specified in the consumer group")
)