This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
config.go
186 lines (150 loc) · 3.81 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package grpc
import (
"errors"
"fmt"
"math"
"net"
"os"
"strings"
"syscall"
"time"
"github.com/spiral/roadrunner"
"github.com/spiral/roadrunner/service"
)
// Config describes GRPC service configuration.
type Config struct {
// Address to listen.
Listen string
// Proto files associated with the service.
Proto []string
// TLS defined authentication method (TLS for now).
TLS TLS
// Workers configures roadrunner grpc and worker pool.
Workers *roadrunner.ServerConfig
// see .rr.yaml for the explanations
MaxSendMsgSize int64
MaxRecvMsgSize int64
MaxConnectionIdle time.Duration
MaxConnectionAge time.Duration
MaxConnectionAgeGrace time.Duration
MaxConcurrentStreams int64
PingTime time.Duration
Timeout time.Duration
}
// TLS defines auth credentials.
type TLS struct {
// Key defined private server key.
Key string
// Cert is https certificate.
Cert string
// Root CA
RootCA string
}
// Hydrate the config and validate it's values.
func (c *Config) Hydrate(cfg service.Config) error {
c.Workers = &roadrunner.ServerConfig{}
err := c.Workers.InitDefaults()
if err != nil {
return err
}
if err := cfg.Unmarshal(c); err != nil {
return err
}
c.Workers.UpscaleDurations()
return c.Valid()
}
// Valid validates the configuration.
func (c *Config) Valid() error {
if len(c.Proto) == 0 && c.Workers.Command != "" {
// only when rr server is set
return errors.New("at least one proto file is required")
}
for _, proto := range c.Proto {
if _, err := os.Stat(proto); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("proto file '%s' does not exists", proto)
}
return err
}
}
if c.Workers.Command != "" {
if err := c.Workers.Pool.Valid(); err != nil {
return err
}
}
if !strings.Contains(c.Listen, ":") {
return errors.New("mailformed grpc grpc address")
}
if c.EnableTLS() {
if _, err := os.Stat(c.TLS.Key); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("key file '%s' does not exists", c.TLS.Key)
}
return err
}
if _, err := os.Stat(c.TLS.Cert); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("cert file '%s' does not exists", c.TLS.Cert)
}
return err
}
// RootCA is optional, but if provided - check it
if c.TLS.RootCA != "" {
if _, err := os.Stat(c.TLS.RootCA); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("root ca path provided, but key file '%s' does not exists", c.TLS.RootCA)
}
return err
}
}
}
// used to set max time
infinity := time.Duration(math.MaxInt64)
if c.PingTime == 0 {
c.PingTime = time.Hour * 2
}
if c.Timeout == 0 {
c.Timeout = time.Second * 20
}
if c.MaxConcurrentStreams == 0 {
c.MaxConcurrentStreams = 10
}
// set default
if c.MaxConnectionAge == 0 {
c.MaxConnectionAge = infinity
}
// set default
if c.MaxConnectionIdle == 0 {
c.MaxConnectionIdle = infinity
}
if c.MaxConnectionAgeGrace == 0 {
c.MaxConnectionAgeGrace = infinity
}
if c.MaxRecvMsgSize == 0 {
c.MaxRecvMsgSize = 1024 * 1024 * 50
} else {
c.MaxRecvMsgSize = 1024 * 1024 * c.MaxRecvMsgSize
}
if c.MaxSendMsgSize == 0 {
c.MaxSendMsgSize = 1024 * 1024 * 50
} else {
c.MaxSendMsgSize = 1024 * 1024 * c.MaxSendMsgSize
}
return nil
}
// Listener creates new rpc socket Listener.
func (c *Config) Listener() (net.Listener, error) {
dsn := strings.Split(c.Listen, "://")
if len(dsn) != 2 {
return nil, errors.New("invalid socket DSN (tcp://:6001, unix://rpc.sock)")
}
if dsn[0] == "unix" {
_ = syscall.Unlink(dsn[1])
}
return net.Listen(dsn[0], dsn[1])
}
// EnableTLS returns true if rr must listen TLS connections.
func (c *Config) EnableTLS() bool {
// Key and Cert OR Key and Cert and RootCA
return (c.TLS.RootCA != "" && c.TLS.Key != "" && c.TLS.Cert != "") || (c.TLS.Key != "" && c.TLS.Cert != "")
}