diff options
author | Valery Piashchynski <[email protected]> | 2021-09-16 21:24:13 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-09-16 21:24:13 +0300 |
commit | 337d292dd2d6ff0a555098b1970d8194d8df8bc2 (patch) | |
tree | a2ab31666f95813a592bea2b207f2db0ba188c92 /plugins/grpc/config.go | |
parent | 5d2cd55ab522d4f1e65a833f91146444465a32ac (diff) | |
parent | cc56349f3ad19aa54ae7900c50e018d757305804 (diff) |
[#783]: feat(grpc): update GRPC plugin to RR `v2`
[#783]: feat(grpc): update GRPC plugin to RR `v2`
Diffstat (limited to 'plugins/grpc/config.go')
-rw-r--r-- | plugins/grpc/config.go | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/plugins/grpc/config.go b/plugins/grpc/config.go new file mode 100644 index 00000000..fedd4998 --- /dev/null +++ b/plugins/grpc/config.go @@ -0,0 +1,128 @@ +package grpc + +import ( + "math" + "os" + "strings" + "time" + + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/pkg/pool" +) + +type Config struct { + Listen string `mapstructure:"listen"` + Proto string `mapstructure:"proto"` + + TLS *TLS + + // Env is environment variables passed to the http pool + Env map[string]string `mapstructure:"env"` + + GrpcPool *pool.Config `mapstructure:"pool"` + MaxSendMsgSize int64 `mapstructure:"max_send_msg_size"` + MaxRecvMsgSize int64 `mapstructure:"max_recv_msg_size"` + MaxConnectionIdle time.Duration `mapstructure:"max_connection_idle"` + MaxConnectionAge time.Duration `mapstructure:"max_connection_age"` + MaxConnectionAgeGrace time.Duration `mapstructure:"max_connection_age_grace"` + MaxConcurrentStreams int64 `mapstructure:"max_concurrent_streams"` + PingTime time.Duration `mapstructure:"ping_time"` + Timeout time.Duration `mapstructure:"timeout"` +} + +type TLS struct { + Key string + Cert string + RootCA string +} + +func (c *Config) InitDefaults() error { //nolint:gocognit + const op = errors.Op("grpc_plugin_config") + if c.GrpcPool == nil { + c.GrpcPool = &pool.Config{} + } + + c.GrpcPool.InitDefaults() + + if !strings.Contains(c.Listen, ":") { + return errors.E(op, errors.Errorf("mailformed grpc grpc address, provided: %s", c.Listen)) + } + + if c.EnableTLS() { + if _, err := os.Stat(c.TLS.Key); err != nil { + if os.IsNotExist(err) { + return errors.E(op, errors.Errorf("key file '%s' does not exists", c.TLS.Key)) + } + + return errors.E(op, err) + } + + if _, err := os.Stat(c.TLS.Cert); err != nil { + if os.IsNotExist(err) { + return errors.E(op, errors.Errorf("cert file '%s' does not exists", c.TLS.Cert)) + } + + return errors.E(op, 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 errors.E(op, errors.Errorf("root ca path provided, but key file '%s' does not exists", c.TLS.RootCA)) + } + return errors.E(op, 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 +} + +func (c *Config) EnableTLS() bool { + if c.TLS != nil { + return (c.TLS.RootCA != "" && c.TLS.Key != "" && c.TLS.Cert != "") || (c.TLS.Key != "" && c.TLS.Cert != "") + } + + return false +} |