Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grpc): grpc keepalive configuration #2621

Merged
merged 7 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/flipt.schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ import "strings"
grpc_port?: int | *9000
cert_file?: string
cert_key?: string
grpc_conn_max_idle_time?: =~#duration
grpc_conn_max_age?: =~#duration
grpc_conn_max_age_grace?: =~#duration
}

#tracing: {
Expand Down
12 changes: 12 additions & 0 deletions config/flipt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,18 @@
},
"cert_key": {
"type": "string"
},
"grpc_conn_max_idle_time": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$"
},
"grpc_conn_max_age": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$"
},
"grpc_conn_max_age_grace": {
"type": "string",
"pattern": "^([0-9]+(ns|us|µs|ms|s|m|h))+$"
}
},
"required": [],
Expand Down
10 changes: 9 additions & 1 deletion internal/cmd/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -358,7 +359,14 @@ func NewGRPCServer(
otel.SetTracerProvider(tracingProvider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))

grpcOpts := []grpc.ServerOption{grpc.ChainUnaryInterceptor(interceptors...)}
grpcOpts := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(interceptors...),
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: cfg.Server.GRPCConnectionMaxIdleTime,
MaxConnectionAge: cfg.Server.GRPCConnectionMaxAge,
MaxConnectionAgeGrace: cfg.Server.GRPCConnectionMaxAgeGrace,
}),
}

if cfg.Server.Protocol == config.HTTPS {
creds, err := credentials.NewServerTLSFromFile(cfg.Server.CertFile, cfg.Server.CertKey)
Expand Down
13 changes: 13 additions & 0 deletions internal/cmd/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ package cmd
import (
"context"
"errors"
"fmt"
"path/filepath"
"sync"
"testing"

"github.com/stretchr/testify/assert"
"go.flipt.io/flipt/internal/config"
"go.flipt.io/flipt/internal/info"
"go.uber.org/zap/zaptest"
)

func TestGetTraceExporter(t *testing.T) {
Expand Down Expand Up @@ -110,3 +114,12 @@ func TestGetTraceExporter(t *testing.T) {
})
}
}

func TestNewGRPCServer(t *testing.T) {
tmp := t.TempDir()
cfg := &config.Config{}
cfg.Database.URL = fmt.Sprintf("file:%s", filepath.Join(tmp, "flipt.db"))
s, err := NewGRPCServer(context.Background(), zaptest.NewLogger(t), cfg, info.Flipt{}, false)
assert.NoError(t, err)
assert.NotEmpty(t, s.Server.GetServiceInfo())
}
11 changes: 11 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,17 @@ func TestLoad(t *testing.T) {
return cfg
},
},
{
name: "grpc keepalive config provided",
path: "./testdata/server/grpc_keepalive.yml",
expected: func() *Config {
cfg := Default()
cfg.Server.GRPCConnectionMaxIdleTime = 1 * time.Hour
cfg.Server.GRPCConnectionMaxAge = 30 * time.Second
cfg.Server.GRPCConnectionMaxAgeGrace = 10 * time.Second
return cfg
},
},
}

for _, tt := range tests {
Expand Down
18 changes: 11 additions & 7 deletions internal/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"encoding/json"
"os"
"time"

"github.com/spf13/viper"
)
Expand All @@ -13,13 +14,16 @@ var _ defaulter = (*ServerConfig)(nil)
// ServerConfig contains fields, which configure both HTTP and gRPC
// API serving.
type ServerConfig struct {
Host string `json:"host,omitempty" mapstructure:"host" yaml:"host,omitempty"`
Protocol Scheme `json:"protocol,omitempty" mapstructure:"protocol" yaml:"protocol,omitempty"`
HTTPPort int `json:"httpPort,omitempty" mapstructure:"http_port" yaml:"http_port,omitempty"`
HTTPSPort int `json:"httpsPort,omitempty" mapstructure:"https_port" yaml:"https_port,omitempty"`
GRPCPort int `json:"grpcPort,omitempty" mapstructure:"grpc_port" yaml:"grpc_port,omitempty"`
CertFile string `json:"-" mapstructure:"cert_file" yaml:"-"`
CertKey string `json:"-" mapstructure:"cert_key" yaml:"-"`
Host string `json:"host,omitempty" mapstructure:"host" yaml:"host,omitempty"`
Protocol Scheme `json:"protocol,omitempty" mapstructure:"protocol" yaml:"protocol,omitempty"`
HTTPPort int `json:"httpPort,omitempty" mapstructure:"http_port" yaml:"http_port,omitempty"`
HTTPSPort int `json:"httpsPort,omitempty" mapstructure:"https_port" yaml:"https_port,omitempty"`
GRPCPort int `json:"grpcPort,omitempty" mapstructure:"grpc_port" yaml:"grpc_port,omitempty"`
CertFile string `json:"-" mapstructure:"cert_file" yaml:"-"`
CertKey string `json:"-" mapstructure:"cert_key" yaml:"-"`
GRPCConnectionMaxIdleTime time.Duration `json:"-" mapstructure:"grpc_conn_max_idle_time" yaml:"-"`
GRPCConnectionMaxAge time.Duration `json:"-" mapstructure:"grpc_conn_max_age" yaml:"-"`
GRPCConnectionMaxAgeGrace time.Duration `json:"-" mapstructure:"grpc_conn_max_age_grace" yaml:"-"`
}

func (c *ServerConfig) setDefaults(v *viper.Viper) error {
Expand Down
30 changes: 30 additions & 0 deletions internal/config/testdata/server/grpc_keepalive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# log:
# level: INFO
# grpc_level: ERROR

# ui:
# enabled: true

# cors:
# enabled: false
# allowed_origins: "*"

# cache:
# enabled: false
# backend: memory
# ttl: 60s
# memory:
# eviction_interval: 5m # Evict Expired Items Every 5m

server:
# protocol: https
# host: 0.0.0.0
# https_port: 443
# http_port: 8080
# grpc_port: 9000
grpc_conn_max_idle_time: 1h
grpc_conn_max_age: 30s
grpc_conn_max_age_grace: 10s

# db:
# url: file:/var/opt/flipt/flipt.db
Loading