-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
123 lines (106 loc) · 2 KB
/
options.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
package gstream
import (
"github.com/KumKeeHyun/gstream/options/pipe"
"github.com/KumKeeHyun/gstream/options/sink"
"github.com/KumKeeHyun/gstream/options/source"
"sync"
"time"
)
type options interface {
SetWorkerPool(pool int)
SetBufferedChan(cap int)
SetTimeout(t time.Duration)
}
type optionsImpl struct {
workerPool int
isBuffer bool
buffer int
timeout time.Duration
}
func (o *optionsImpl) SetWorkerPool(pool int) {
o.workerPool = pool
}
func (o *optionsImpl) SetBufferedChan(cap int) {
o.isBuffer = true
o.buffer = cap
}
func (o *optionsImpl) SetTimeout(t time.Duration) {
o.timeout = t
}
type sourceOption struct {
optionsImpl
}
func (o *sourceOption) WorkerPool() int {
return o.workerPool
}
func newSourceOption(opts ...source.Option) *sourceOption {
srcOpt := &sourceOption{
optionsImpl: optionsImpl{
workerPool: 1,
},
}
for _, opt := range opts {
opt(srcOpt)
}
return srcOpt
}
type pipeOption[T any] struct {
optionsImpl
once sync.Once
pipe chan T
}
func (o *pipeOption[T]) WorkerPool() int {
return o.workerPool
}
func (o *pipeOption[T]) BuildPipe() chan T {
o.once.Do(func() {
if o.isBuffer {
o.pipe = make(chan T, o.buffer)
} else {
o.pipe = make(chan T)
}
})
return o.pipe
}
func newPipeOption[T any](opts ...pipe.Option) *pipeOption[T] {
pipeOpt := &pipeOption[T]{
optionsImpl: optionsImpl{
workerPool: 1,
isBuffer: false,
},
}
for _, opt := range opts {
opt(pipeOpt)
}
return pipeOpt
}
type sinkOption[T any] struct {
optionsImpl
once sync.Once
pipe chan T
}
func (o *sinkOption[T]) BuildPipe() chan T {
o.once.Do(func() {
if o.isBuffer {
o.pipe = make(chan T, o.buffer)
} else {
o.pipe = make(chan T)
}
})
return o.pipe
}
func (o *sinkOption[T]) Timeout() time.Duration {
return o.timeout
}
func newSinkOption[T any](opts ...sink.Option) *sinkOption[T] {
sinkOpt := &sinkOption[T]{
optionsImpl: optionsImpl{
isBuffer: false,
timeout: -1,
},
}
for _, opt := range opts {
opt(sinkOpt)
}
return sinkOpt
}