-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.go
71 lines (62 loc) · 1.68 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
package law
import lf "github.com/shengyanli1982/law/internal/lockfree"
// DefaultBufferSize 默认缓冲区大小
// DefaultBufferSize is the default buffer size
const DefaultBufferSize = 2048
// Config 配置结构体
// Config is the configuration structure
type Config struct {
buffSize int // 缓冲区大小 / buffer size
callback Callback // 回调函数 / callback function
queue Queue // 队列实现 / queue implementation
}
// NewConfig 创建新的配置实例
// NewConfig creates a new configuration instance
func NewConfig() *Config {
return &Config{
buffSize: DefaultBufferSize,
callback: newEmptyCallback(),
queue: lf.NewLockFreeQueue(),
}
}
// DefaultConfig 返回默认配置
// DefaultConfig returns the default configuration
func DefaultConfig() *Config {
return NewConfig()
}
// WithBufferSize 设置缓冲区大小
// WithBufferSize sets the buffer size
func (c *Config) WithBufferSize(size int) *Config {
c.buffSize = size
return c
}
// WithCallback 设置回调函数
// WithCallback sets the callback function
func (c *Config) WithCallback(cb Callback) *Config {
c.callback = cb
return c
}
// WithQueue 设置队列实现
// WithQueue sets the queue implementation
func (c *Config) WithQueue(q Queue) *Config {
c.queue = q
return c
}
// isConfigValid 验证并修正配置
// isConfigValid validates and corrects the configuration
func isConfigValid(conf *Config) *Config {
if conf != nil {
if conf.buffSize <= 0 {
conf.buffSize = DefaultBufferSize
}
if conf.callback == nil {
conf.callback = newEmptyCallback()
}
if conf.queue == nil {
conf.queue = lf.NewLockFreeQueue()
}
} else {
conf = DefaultConfig()
}
return conf
}