-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
64 lines (49 loc) · 1.14 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
package tcp
import (
"unsafe"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/pool"
)
type Srv struct {
Addr string `mapstructure:"addr"`
Delimiter string `mapstructure:"delimiter"`
delimBytes []byte
}
type Config struct {
Servers map[string]*Srv `mapstructure:"servers"`
ReadBufferSize int `mapstructure:"read_buf_size"`
Pool *pool.Config `mapstructure:"pool"`
}
func (c *Config) InitDefault() error {
if len(c.Servers) == 0 {
return errors.Str("no servers registered")
}
for k, v := range c.Servers {
if v.Delimiter == "" {
v.Delimiter = "\r\n"
v.delimBytes = []byte{'\r', '\n'}
}
if v.Addr == "" {
return errors.Errorf("empty address for the server: %s", k)
}
// already written
if len(v.delimBytes) > 0 {
continue
}
v.delimBytes = strToBytes(v.Delimiter)
}
if c.Pool == nil {
c.Pool = &pool.Config{}
}
if c.ReadBufferSize == 0 {
c.ReadBufferSize = 1024 * 1024 * 1
}
c.Pool.InitDefaults()
return nil
}
func strToBytes(data string) []byte {
if data == "" {
return nil
}
return unsafe.Slice(unsafe.StringData(data), len(data))
}