-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
85 lines (70 loc) · 2.28 KB
/
config.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
package delayed
import "io"
func New(options ...option) Writer {
var config configuration
Options.apply(options...)(&config)
if config.Target == nil {
return &nopWriter{}
} else if _, ok := config.Target.(*nop); ok {
return &nopWriter{}
}
return newWriter(config)
}
func (singleton) Source(value func() Message) option {
return func(this *configuration) { this.Source = value }
}
func (singleton) Target(value io.WriteCloser) option {
return func(this *configuration) { this.Target = value }
}
func (singleton) PoolSize(value int) option {
return func(this *configuration) { this.PoolSize = value }
}
func (singleton) ChannelSize(value int) option {
return func(this *configuration) { this.ChannelSize = value }
}
func (singleton) Sequence(value uint64) option {
return func(this *configuration) { this.Sequence = value }
}
func (singleton) Monitor(value Monitor) option {
return func(this *configuration) { this.Monitor = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, item := range Options.defaults(options...) {
item(this)
}
}
}
func (singleton) defaults(options ...option) []option {
empty := &nop{}
return append([]option{
Options.Source(func() Message { return empty }),
Options.Target(empty),
Options.PoolSize(1024),
Options.ChannelSize(128),
Options.Sequence(0),
Options.Monitor(empty),
}, options...)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type configuration struct {
Source func() Message
Target io.WriteCloser
PoolSize int
ChannelSize int
Sequence uint64
Monitor Monitor
}
type option func(*configuration)
type singleton struct{}
var Options singleton
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type nop struct{}
func (*nop) Buffered() {}
func (*nop) Discarded(_ Message) {}
func (*nop) Written() {}
func (*nop) WriteFailed(_ Message, _ error) {}
func (*nop) Sequence(uint64) {}
func (*nop) WriteTo(io.Writer) (int64, error) { return 0, nil }
func (*nop) Write([]byte) (int, error) { return 0, nil }
func (*nop) Close() error { return nil }